MyBatis案例三:商品的类别(一对一,一对多查询)

您所在的位置:网站首页 mybatis中的一对多 MyBatis案例三:商品的类别(一对一,一对多查询)

MyBatis案例三:商品的类别(一对一,一对多查询)

2023-08-08 15:48| 来源: 网络整理| 查看: 265

在数据库分别创建一个product、category表:

商品表(product) 商品编号(id)商品名称(goodsname)商品单价(price)商品类别(typeid)1电视机500012冰箱400023空调300024洗衣机20002 商品类别表(category) 商品类别编号(id)商品类别名称(typename)1黑色家电2白色家电

要求:

1.查询商品类别为白色家电的所有商品信息。

2.实现单个商品和单个类别的一对一关系查询。

步骤:

1.创建持久化类Product、Category

package com.xiao.pojo; import java.util.List; /** * @ClassName Category * @Author 86155 * @Description TODO * @createTime 2022年02月27日 13:35:00 */ public class Category { private Integer id; private String typename; private List productList;//用于(一对多) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTypename() { return typename; } public void setTypename(String typename) { this.typename = typename; } public List getProductList() { return productList; } public void setProductList(List productList) { this.productList = productList; } @Override public String toString() { return "Category[" + "id=" + id + ", typename='" + typename + '\'' + ", productList=" + productList + ']'; } } package com.xiao.pojo; import java.lang.reflect.Type; /** * @ClassName Product * @Author 86155 * @Description TODO * @createTime 2022年02月27日 13:30:00 */ public class Product { private Integer id; private String goodsname; private Integer price; private Category typeid; 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 Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Category getTypeid() { return typeid; } public void setTypeid(Category typeid) { this.typeid = typeid; } @Override public String toString() { return "Product[" + "id=" + id + ", goodsname='" + goodsname + '\'' + ", price=" + price + ",Category=" + typeid + ']'; } }

2.创建映射文件ProductMapper.xml、CategoryMapper.xml

select p.*,c.typename from product p ,category c where p.typeid = c.id and p.id = #{id} select c.*,p.id as product_id,p.goodsname,p.price from product p ,category c where p.typeid = c.id and c.id = #{id}

3.在核心配置文件mybatis-config.xml中配置映射文件的路径

4.创建测试类

//一对多查询所有白色家电的商品信息(嵌套结果) @Test public void findCategoryById(){ SqlSession sqlSession = MyBatisUtils.getSession(); Category category = sqlSession.selectOne("findCategoryById",2); System.out.println(category); sqlSession.close(); } //一对一查询商品的类别信息(嵌套结果) @Test public void findProductById(){ SqlSession sqlSession = MyBatisUtils.getSession(); Product product = sqlSession.selectOne("findProductById",2); System.out.println(product); sqlSession.close(); }

注意:

1.创建持久化类时

private Category typeid; private List productList;

2.嵌套结果方式,一对一时,去含外键的表相关的映射文件写元素;

一对多时,去不含外键的表相关的映射文件写元素;

嵌套查询方式,一对一时,去不含外键的表相关的映射文件写元素,再去含外键的表相关的映射文件写元素;

一对多时,去不含外键的表相关的映射文件写元素,再去含外键的表相关的映射文件写元素;

3.不要忘记配置映射文件的路径;



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3