mybatis 一对多 映射的用法

1、只用一条SQL 这个分页以join后的条数为主,分页会错乱

public class CouponGoodsTypeVO {
      
    private Long typeId;
    
    private String typeName;
    
    private String channelType;

    List<CouponGoodsInfoVO> goodsList;
}
	<resultMap id="GoodsTypeResultMap" type="com.vo.CouponGoodsTypeVO">
		<id column="type_id" property="typeId"/>
		<result column="type_name" property="typeName"/>
		<result column="channel_type" property="channelType"/>

		<collection column="type_id" property="goodsList" ofType="com.vo.CouponGoodsInfoVO">
			<id column="goods_id" property="goodsId" />
			<result column="goods_name" property="goodsName" />
		</collection>

	</resultMap>
	
	<select id="selectCouponGoodsType" parameterType="com.vo.CouponGoodsTypeVO" resultMap="GoodsTypeResultMap">
		select 
			ty.type_id ,
			ty.type_name ,
			ty.channel_type ,
			info.goods_id ,
			info.goods_name
		from t_scte_goods_type ty
		join t_scte_goods_type_relate tr on tr.type_id = ty.type_id
		join t_scte_goods_info info on info.goods_id = tr.goods_id
		where ty.mchnt_cd = #{mchntCd} and ty.channel_type = #{channelType} and info.is_delete='0'
	</select>

2 、用两条sql 这个分页以主SQL的条数为准,分页正常

public class TCloudcrmCouponTask {
	
    /** 主键 */
    private Integer taskId;

    /** 任务名称 */
    private String taskName;

    /** 任务类型, 00:自动,01:手动,  02: 自定义规则', */
    private String taskType;

    /** 关联的 任务优惠券关联表 */
    private List<TCloudcrmCouponTaskRelate> couponTaskRelates;

}
  <resultMap id="PageResultMap" type="com.model.TCloudcrmCouponTask" >
    <id column="task_id" property="taskId" jdbcType="INTEGER" />
    <result column="task_name" property="taskName" jdbcType="VARCHAR" />
    <result column="task_type" property="taskType" jdbcType="CHAR" />

    <!-- PS:从表里查询出的字段名不能与主表里的一致  否则 当从表为空的时候 根据property自动将property相同的主表里的值 赋给  从表 -->
    <!-- PS:从表里的where条件放在join之前  否则当从表为空的时候  主表记录也会被过滤掉 -->
    <collection property="couponTaskRelates" ofType="com.model.TCloudcrmCouponTaskRelate" select="getTaskRelate" column="task_id">
    </collection>
    
  </resultMap>

  <select id="selectListOrderCrtTimePage" resultMap="PageResultMap" parameterType="com.model.TCloudcrmCouponTask">
	 select t.task_id,t.task_name, t.task_type
   	 from t_cloudcrm_coupon_task t
  </select>
   <resultMap id="TaskRelateResult" type="com.model.TCloudcrmCouponTaskRelate">  
	   	<id column="id" property="id" jdbcType="INTEGER" />
	    <result column="coupon_id" property="couponId" jdbcType="INTEGER" />
	    <result column="coupon_name" property="couponName" jdbcType="VARCHAR" />
	    <result column="state" property="state" jdbcType="CHAR" />  
   </resultMap>
  
  <select id="getTaskRelate" parameterType="int" resultMap="TaskRelateResult">
  	select r.id, r.coupon_id, r.state, i.coupon_name
  	from (
  		select id, task_id, coupon_id, state 
  		from t_cloudcrm_coupon_task_relate where task_id = #{taskId} 
  	) r 
  	left join t_cloudcrm_coupon_info i on i.coupon_id = r.coupon_id
  </select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值