【MyBatis学习笔记】7. MyBatis的多表关联查询


一对多查询

GoodsDetail.java

public class GoodsDetail{
    private Integer gdId;
    private Integer goodsId;
    private String gdPicUrl;
    private Integer gdOrder;
    ... setter和getter
}

Goods.java

public class Goods{
    private Integer goodsId;
    private String title;
    private String subTitle;
    private Float originalCost;
    private Float currentPrice;
    private Float discount;
    private Integer isFreeDelivery;
    private Integer categoryId;
    private List<GoodsDetail> goodsDetails;
    ...setter getter
}

goods_detail.xml

<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>

goods.xml

<!-- 
resultMap可用于说明一对多或者多对一的映射逻辑
id是resultMap属性应用的标志
type指向One的实体(Goods)
-->
<resultMap id="rmGoods1" type="com.imooc.mybatis.entity.Goods" >
    <!-- 映射goods对象的主键到goods_id字段 -->
    <id column="goods_id" property="goodsId"></id>
    <!-- 
    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>
List<Goods> list=session.selectList("goods.selectOneToMany");

多对一查询

GoodsDetail.java

public class GoodsDetail{
    private Integer gdId;
    private Integer goodsId;
    private String gdPicUrl;
    private Integer gdOrder;
    private Goods goods;
    ... setter和getter
}

Goods.java

public class Goods{
    private Integer goodsId;
    private String title;
    private String subTitle;
    private Float originalCost;
    private Float currentPrice;
    private Float discount;
    private Integer isFreeDelivery;
    private Integer categoryId;
    private List<GoodsDetail> goodsDetails;
    ...setter getter
}

goods_detail.xml

<mapper namespace="goodsDetail">
    <resultMap id="rmGoodsDetail" type="com.imooc.mybatis.entity.GoodsDetail">
        <id column="gd_id" property="gdId"/>
        <association property="goods" select="goods.selectById" column="gd_id"/>
    </resultMap>
    <select id="selectManyToOne" resultMap=""> 
        select * from t_goods_detail limit 0,1
    </select>
</mapper>

goods.xml

<select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.Goods">
    select * from t_goods where goods_id=#{value}
</select> 
List<GoodsDetail> list=session.selectList("goodsDetail.selectManyToOne");

延迟加载 fetchType

  • false 取消延迟加载
  • true 支持延迟加载
    association标签collection标签中可配置fetchType
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值