mybatis-一对多分页查询

  • 问题
    在用mybatis做一对多查询时候,常用collection配合完成结果查询。在不涉及分页查询情况下,查询结果是没有问题的。但当涉及分页查询时,就会出现问题,即结果总数量total多于实际数量。

  • 演示示例如下

  • 实体类

@Data
public class JudgePicPointSatatusResVo {
    private Integer id;
    private String judgePicPoint;
    private String productNum;
    private String judgePicStation;
    private String stationName;
    private List<String> stationNameList;
    private Integer status;
}
  • Dao层
List<JudgePicPointSatatusResVo> getJudgePicPointListByJudgePicStationId(JudgePicPointSatatusReqVo judgePicPointSatatusReqVo);
  • 查询
    <resultMap id="judgePicPointListMap" type="com.qxmz.vo.judgePic.JudgePicPointSatatusResVo">
        <id column="id" property="id"/>
        <result column="judgePicPoint" property="judgePicPoint"/>
        <result column="productNum" property="productNum"/>
        <result column="judgePicStation" property="judgePicStation"/>
        <result column="status" property="status"/>
        <collection property="stationNameList" ofType="String">
            <id column="stationName"/>
        </collection>
    </resultMap>
    <select id="getJudgePicPointListByJudgePicStationId" parameterType="com.qxmz.vo.judgePic.JudgePicPointSatatusReqVo"
            resultMap="judgePicPointListMap">
        SELECT cj.`id`,cj.`productNum`,cj.`status`, jpp.`name` judgePicPoint,jps.`name`
        judgePicStation,st.station_name stationName FROM client_judgePicPoint cj
        LEFT JOIN judge_pic_point jpp ON cj.`judgePicPointId`=jpp.`id`
        LEFT JOIN judge_pic_station jps ON jpp.`judgePicStationId`=jps.`id`
        LEFT JOIN station st ON jps.`id`=st.`judgePicStationId`
        <where>
            cj.isAdd=1
            <if test="judgePicStationIdList!=null">
                and jps.id in
                <foreach item="judgePicStationId" collection="judgePicStationIdList" open="(" separator="," close=")">
                    #{judgePicStationId}
                </foreach>
            </if>
            <if test="status != null and status != 0">
                and cj.status = #{status}
            </if>
        </where>
    </select>
  • 结果
{
    "code": 200,
    "message": "success",
    "data": {
        "total": 2,
        "rows": [
            {
                "id": 2,
                "judgePicPoint": "判图点3",
                "productNum": "002",
                "judgePicStation": "判图站B",
                "stationName": "郑州西站、南阳寨站",
                "stationNameList": [
                    "郑州西站",
                    "南阳寨站"
                ],
                "status": 2
            }
        ]
    }
}

从结果中可以看到,实际只查询出来一条数据,但总数却是2。这就是一对多分页查询会出现的问题。原因也很简单,以上面数据为例。在mysql客户端执行一对多SQL命令时,结果就是两条。一对多情况下,mybatis会自动将结果装备到collection中,但是在分页情况下,mybatis会认为查询的两台数据就是总的数据。

  • 解决方法
    采用父子查询来实现,具体代码如下:
  • 父查询
    <resultMap id="judgePicPointListMap" type="com.qxmz.vo.judgePic.JudgePicPointSatatusResVo">
        <id column="id" property="id"/>
        <result column="judgePicPoint" property="judgePicPoint"/>
        <result column="productNum" property="productNum"/>
        <result column="judgePicStation" property="judgePicStation"/>
        <result column="status" property="status"/>
        <collection property="stationNameList" ofType="String" column="judgePicStationId"
                    select="selectStationNameByJudgePicStationId">
        </collection>
    </resultMap>
    <select id="getJudgePicPointListByJudgePicStationId" parameterType="com.qxmz.vo.judgePic.JudgePicPointSatatusReqVo"
            resultMap="judgePicPointListMap">
        SELECT cj.`id`,cj.`productNum`,cj.`status`, jpp.`name` judgePicPoint,jps.`name`
        judgePicStation,jps.id judgePicStationId FROM client_judgePicPoint cj
        LEFT JOIN judge_pic_point jpp ON cj.`judgePicPointId`=jpp.`id`
        LEFT JOIN judge_pic_station jps ON jpp.`judgePicStationId`=jps.`id`
        <where>
            cj.isAdd=1
            <if test="judgePicStationIdList!=null">
                and jps.id in
                <foreach item="judgePicStationId" collection="judgePicStationIdList" open="(" separator="," close=")">
                    #{judgePicStationId}
                </foreach>
            </if>
            <if test="status != null and status != 0">
                and cj.status = #{status}
            </if>
        </where>
    </select>
  • 子查询
<select id="selectStationNameByJudgePicStationId" resultType="String">
        SELECT  station_name FROM station WHERE judgePicStationId=#{judgePicStationId}
    </select>
  • 正确结果
{
    "code": 200,
    "message": "success",
    "data": {
        "total": 1,
        "rows": [
            {
                "id": 2,
                "judgePicPoint": "判图点3",
                "productNum": "002",
                "judgePicStation": "判图站B",
                "stationName": "郑州西站、南阳寨站",
                "stationNameList": [
                    "郑州西站",
                    "南阳寨站"
                ],
                "status": 2
            }
        ]
    }
}
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值