Java EE Enterprise Application Development - Querying Product Categories with Source Code

  1. Create database tables and insert data into cateogry and product tables
USE mybatis;
CREATE table product(
   id INT PRIMARY KEY AUTO_INCREMENT,
	 goodsname VARCHAR(32),
	 price DOUBLE,
	 typeid  int(32)
);
INSERT INTO product VALUES('1','电视机','5000','1');
INSERT INTO product VALUES('2','冰箱','4000','2');
INSERT INTO product VALUES('3','空调','3000','2');
INSERT INTO product VALUES('4','洗衣机','2000','2');


USE mybatis;
CREATE TABLE category(
      id int(32)  PRIMARY KEY AUTO_INCREMENT,
			typename VARCHAR(32)
);
insert into category VALUES('1','黑色家电');
insert into category VALUES('2','白色家电');

  1. Configure the core configuration file (omitted here)
  2. Create POJO classes for Category and OneProduct

(1) Category POJO

package com.itheima.pojo;

import java.util.List;

public class Category {
    private int id;
    private String typename;
    private List<OneProduct> OneproductList;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTypename() {
        return typename;
    }

    public void setTypename(String typename) {
        this.typename = typename;
    }

    public List<OneProduct> getOneproductList() {
        return OneproductList;
    }

    public void setOneproductList(List<OneProduct> oneproductList) {
        OneproductList = oneproductList;
    }

@Override
    public String toString(){
        return "category [id=" + id + ",typename=" + typename +",OneproductList=" + OneproductList +"]";
}
}


(2) OneProduct POJO class

package com.itheima.pojo;

import java.util.List;

public class OneProduct {
    private Integer id;                //Product ID
    private String goodsname;                //Product name
    private Double price;//Unit price
    private int typeid;        //Associated category identifier

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getGoodsname() {
        return goodsname;
    }

    public void setGoodsname(String goodsname) {
        this.goodsname = goodsname;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public int getTypeid() {
        return typeid;
    }

    public void setTypeid (int typeid){
        this.typeid = typeid;
    }

    @Override
    public String toString() {
        return "Product [id=" + id + ", goodsname=" + goodsname
                + ", price=" + price + ",typeid="+ typeid + "]";
    }
}




  1. Create Category Mapper XML mapping file (Many-to-one relationship between product and category tables; create mapper for the 'one' side)

<mapper namespace="com.itheima.mapper.CategoryMapper">
    <select id="findCategoryWithOneProduct" parameterType="Integer"
            resultMap="categoryWithProduct">
        select
            p.id pid,
            p.goodsname,
            p.price,
            c.id cid,
            c.typename
        from
            category c,
            product p
        where
            c.id = p.typeid
          and c.id=#{cid}
    </select>
    <resultMap id="categoryWithProduct" type="Category">
        <id column="cid" property="id"></id>
        <result column="typename" property="typename"></result>
        <collection property="OneproductList" javaType="list" ofType="OneProduct">
            <id column="pid" property="id"></id>
            <result column="goodsname" property="goodsname"></result>
            <result column="price" property="price"></result>
        </collection>
    </resultMap>
</mapper>


  1. In the mybatis-config.xml file's <mappers> element, reference the CategoryMapper.xml mapping file
 <mapper resource="com/itheima/mapper/CategoryMapper.xml"/>

  1. Create a test class under test/java directory
package com.itheima;

import com.itheima.pojo.Category;
import com.itheima.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class CategoryTest {
    @Test
    public void findCategoryWithOneProductTest(){
        SqlSession session = MyBatisUtils.getSession();
        Category category = session.selectOne("com.itheima.mapper.CategoryMapper.findCategoryWithOneProduct",2);
        System.out.println(category);
        session.commit();
        session.close();

    }
}


  1. Sample execution output

Tags: java MyBatis sql JavaBeans enterprise-application

Posted on Wed, 14 Oct 2026 16:17:59 +0000 by Gary Kambic