mybatis + PageHelper 一对多分页查询

1 .直接使用collection标签

对象
@Data 
public class TaskAndResponseVO extends ProdTaskPo {
 private List<ProdAgvResponsePO> responsePOS;
 }

<resultMap id="selectResultMap" type="com.uspring.sabs.agv.entity.vo.TaskAndResponseVO">
        <id column="task_id" jdbcType="INTEGER" property="taskId" />
        <result column="task_code" jdbcType="VARCHAR" property="taskCode"/>
        <collection property="responsePOS" ofType="com.uspring.sabs.agv.entity.po.ProdAgvResponsePO">
            <id column="ar_id" jdbcType="INTEGER" property="arId" />
            <result column="ar_point" jdbcType="VARCHAR" property="arPoint" />
            <result column="create_time1" jdbcType="TIMESTAMP" property="createTime" />
            <result column="ar_method" jdbcType="VARCHAR" property="arMethod" />

        </collection>
    </resultMap>
    
    <select id="taskAndResponses" resultMap="selectResultMap">
        select pt.* ,par.ar_id,par.ar_method,par.create_time create_time1 from prod_task pt left join prod_agv_response par
            on pt.task_code=par.task_code
            <where>
                pt.task_status='COMPLETE'
                <if test="taskCode != null and taskCode != ''">
                    and pt.task_code like concat('%',#{taskCode},'%')
                </if>
                <if test="taskType != null and taskType != ''">
                    and pt.task_type like concat('%',#{taskType},'%')
                </if>
                <if test="agvCode != null and agvCode != ''">
                    and pt.agv_code like concat('%',#{agvCode},'%')
                </if>
            </where>


    </select>


当使用mybatis + PageHelper 一对多分页查询时直接使用collection标签时 数据的pageSize的数量为一对多的关联查询数量也就是responsePOS数量,此时需要重写一个分页方法 如下
2.重写分页方法
<resultMap id="selectResultMap" type="com.uspring.sabs.agv.entity.vo.TaskAndResponseVO">
    <id column="task_id" jdbcType="INTEGER" property="taskId"/>
    <result column="task_code" jdbcType="VARCHAR" property="taskCode"/>
    <result column="task_type" jdbcType="VARCHAR" property="taskType"/>
    <result column="agv_code" jdbcType="VARCHAR" property="agvCode"/>
    <result column="tc_code" jdbcType="VARCHAR" property="tcCode"/>
    <collection property="responsePOS" ofType="com.uspring.sabs.agv.entity.po.ProdAgvResponsePO"
                select="selectResponse" column="task_code">
    </collection>
</resultMap>
<resultMap id="responseMap" type="com.uspring.sabs.agv.entity.po.ProdAgvResponsePO">
    <id column="ar_id" jdbcType="INTEGER" property="arId"/>
    <result column="ar_point" jdbcType="VARCHAR" property="arPoint"/>
    <result column="create_time1" jdbcType="TIMESTAMP" property="createTime"/>
    <result column="ar_method" jdbcType="VARCHAR" property="arMethod"/>
</resultMap>
<select id="taskAndResponses" resultMap="selectResultMap">
    select pt.* from prod_task pt
    <where>
        pt.task_status='COMPLETE' and create_time between #{startCreateTime} and #{endCreateTime}
        <if test="taskCode != null and taskCode != ''">
            and pt.task_code like concat('%',#{taskCode},'%')
        </if>
        <if test="taskType != null and taskType != ''">
            and pt.task_type like concat('%',#{taskType},'%')
        </if>
        <if test="agvCode != null and agvCode != ''">
            and pt.agv_code like concat('%',#{agvCode},'%')
        </if>
    </where>
</select>

<select id="selectResponse" resultMap="responseMap">
    select par.ar_id, par.ar_method, par.create_time create_time1
    from prod_agv_response par
    where par.task_code = #{task_code}
</select>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值