1.案例分析
1.班级(1)---> 学生 (n) 学生必须持有班级的外键 2.1对1 通过主键关联 3.多对多必须单独抽象出一张中间表
2.商品和详情对象关联查询(一对多)
2.1 创建商品描述详情的实体类
package com.imooc.mybatis.entity; public class GoodsDetail { private Integer gdId; private Integer goodsId; private String gdPicUrl; private Integer gdOrder; public Integer getGdId() { return gdId; } public void setGdId(Integer gdId) { this.gdId = gdId; } public Integer getGoodsId() { return goodsId; } public void setGoodsId(Integer goodsId) { this.goodsId = goodsId; } public String getGdPicUrl() { return gdPicUrl; } public void setGdPicUrl(String gdPicUrl) { this.gdPicUrl = gdPicUrl; } public Integer getGdOrder() { return gdOrder; } public void setGdOrder(Integer gdOrder) { this.gdOrder = gdOrder; } }
2.2 增加goodsDetail对应的mapper文件(对象关联)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="goodsDetail"> <select id="selectByGoodsId" parameterType="Integer" resultType="com.imooc.mybatis.entity.GoodsDetail"> select * from t_goods_detail where goods_id = #{value} </select> </mapper>
2.3 一的一方如何持有多的一方 private List<GoodsDetail> goodsDetails;
package com.imooc.mybatis.entity; import java.util.List; public class Goods { private Integer goodsId;//商品编号 private String title;//标题 private String subTitle;//子标题 private Float originalCost;//原始价格 private Float currentPrice;//当前价格 private Float discount;//折扣率 private Integer isFreeDelivery;//是否包邮 ,1-包邮 0-不包邮 private Integer categoryId;//分类编号 private List<GoodsDetail> goodsDetails; public List<GoodsDetail> getGoodsDetails() { return goodsDetails; } public void setGoodsDetails(List<GoodsDetail> goodsDetails) { this.goodsDetails = goodsDetails; } public Integer getGoodsId() { return goodsId; } public void setGoodsId(Integer goodsId) { this.goodsId = goodsId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSubTitle() { return subTitle; } public void setSubTitle(String subTitle) { this.subTitle = subTitle; } public Float getOriginalCost() { return originalCost; } public void setOriginalCost(Float originalCost) { this.originalCost = originalCost; } public Float getCurrentPrice() { return currentPrice; } public void setCurrentPrice(Float currentPrice) { this.currentPrice = currentPrice; } public Float getDiscount() { return discount; } public void setDiscount(Float discount) { this.discount = discount; } public Integer getIsFreeDelivery() { return isFreeDelivery; } public void setIsFreeDelivery(Integer isFreeDelivery) { this.isFreeDelivery = isFreeDelivery; } public Integer getCategoryId() { return categoryId; } public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } }
2.4 goods.xml (对2.3中添加的list数据进行说明)
<!-- resultMap可用于说明一对多或者多对一的映射逻辑 id 是resultMap属性引用的标志 type 指向One的实体(Goods) --> <resultMap id="rmGoods1" type="com.imooc.mybatis.entity.Goods"> <!-- 映射goods对象的主键到goods_id字段 --> <id column="goods_id" property="goodsId"/> <!-- collection的含义是,在 select * from t_goods limit 0,1 得到结果后,对所有Goods对象遍历得到goods_id字段值, 并代入到goodsDetail命名空间的findByGoodsId的SQL中执行查询, 将得到的"商品详情"集合赋值给goodsDetails List对象. --> <collection property="goodsDetails" select="goodsDetail.selectByGoodsId" column="goods_id"/> </resultMap> <select id="selectOneToMany" resultMap="rmGoods1"> select * from t_goods limit 0,10 </select>
2.5 核心配置文件中进行注册mybatis-config.xml
<!--声明mapper的配置文件--> <mappers> <mapper resource="mappers/goods_detail.xml"/> </mappers>
2.6 测试用例
/** * 一对多对象关联查询 * @throws Exception */ @Test public void testOneToMany() throws Exception { SqlSession session = null; try { session = MyBatisUtils.openSession(); List<Goods> list = session.selectList("goods.selectOneToMany"); for(Goods goods:list) { System.out.println(goods.getTitle() + ":" + goods.getGoodsDetails().size()); } } catch (Exception e) { throw e; } finally { MyBatisUtils.closeSession(session); } }
标签:多表,OneToMany,void,private,查询,return,goodsId,Integer,public From: https://www.cnblogs.com/LLL0617/p/16887780.html