记 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
记 MyBatis⼀对多关联查询导致的Pagehelper分页total错误的问题解决方案_第1张图片
但是我想要的效果是: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>

你可能感兴趣的:(问题总结,mybatis,java,pagehelper,关联查询,子查询)