记 MyBatis⼀对多关联查询导致的Pagehelper分页total错误的问题解决方案

前言:

该方法本质上还是将⼀对多关联查询拆分为子查询,因为Pagehelper只针对第一条SQL分页,因此两次查询会解决这个问题。
但是我写关联查询就是想减少查询次数,这种方法也完全可以写两个selectList用代码处理数据去代替,可能也就是少写点代码,业务里面好看点吧。

印象中以前使用分页并没有出现关联查询引起total错误的问题,不知道是不是用的其他分页插件,或者手动分页,或者是Pagehelper但是某个版本解决了这个问题,如果有知道的大佬请评论区不吝赐教,谢谢!

关联查询

<resultMap id="pageListResult" type="com.investment.entity.vo.DirectFundFoundContributorPageVo">
        <id property="id" column="id"/>
        <result property="fundId" column="fundId"/>
        <result property="foundId" column="foundId" />
        ......
        <collection property="files" ofType="com.investment.entity.system.SysFile">
            <result property="realName" column="realName" />
            <result property="accessName" column="accessName" />
            <result property="convertName" column="convertName" />
            <result property="suffix" column="suffix" />
            <result property="url" column="url" />
            <result property="pdfUrl" column="pdfUrl" />
        </collection>
    </resultMap>

	<select id="selectPageList" resultMap="pageListResult" resultType="com.investment.entity.vo.DirectFundFoundContributorPageVo">
        select dffc.xxx, sf.xxx
        from direct_fund_found_contributor dffc
        left join direct_fund df on dffc.fundId = df.id
        left join direct_fund_found dff on dffc.foundId = dff.id
        left join (select * from sys_file  where headType = 3 and delFlag = 1 ) sf on sf.tableId = dffc.id
        <where>
            <if test="fundId != null">
                and dffc.fundId = #{fundId}
            </if>
            and dff.delFlag = 1
            and dffc.delFlag = 1
        </where>
        order by dffc.id desc
    </select>

使用查询工具查询结果如图,因此Pagehelper也识别出total=3
在这里插入图片描述
但是我想要的效果是:List有两个,id=1内部文件list有两条数据。

子查询

其实就是查询两次
改造也很简单,SQL拆分一下,resultMap微调

  1. collection中添加 select=“selectFiles”,代表子查询的sql;
  2. column="id"代表关联的字段。
<resultMap id="pageListResult" type="com.investment.entity.vo.DirectFundFoundContributorPageVo">
        <id property="id" column="id"/>
        <result property="fundId" column="fundId"/>
        <result property="foundId" column="foundId" />
        ......
        <collection property="files" ofType="com.investment.entity.system.SysFile" select="selectFiles" column="id" >
            <result property="realName" column="realName" />
            <result property="accessName" column="accessName" />
            <result property="convertName" column="convertName" />
            <result property="suffix" column="suffix" />
            <result property="url" column="url" />
            <result property="pdfUrl" column="pdfUrl" />
        </collection>
    </resultMap>

新增一个查询语句,id等于上面定义的selectFiles,关联字段用的column="id"的字段

<select id="selectFiles" resultType="com.zkjg.investment.entity.system.SysFile">
    select xxx
    from sys_file
    where headType = 3 and delFlag = 1 and tableId = #{id}
</select>

<select id="selectPageList" resultMap="pageListResult" resultType="com.investment.entity.vo.DirectFundFoundContributorPageVo">
    select dffc.xxx
    from direct_fund_found_contributor dffc
    left join direct_fund df on dffc.fundId = df.id
    left join direct_fund_found dff on dffc.foundId = dff.id
    <where>
        <if test="fundId != null">
            and dffc.fundId = #{fundId}
        </if>
        and dff.delFlag = 1
        and dffc.delFlag = 1
    </where>
    order by dffc.id desc
</select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值