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>