美文网首页
mybatis-plus分页插件-分页的total总数不对

mybatis-plus分页插件-分页的total总数不对

作者: lclandld | 来源:发表于2022-08-04 16:04 被阅读0次

问题描述:有一个SQL关联了很多的leftJoin,在分页的时候,total总数一直为1,数据明明就很多


1659685470597.png

根据打断点,通过MyBatisLog插件,看了下具体的sql日志,可以看到获取count的数的时候,居然没有left join那些条件了,只剩下了主表


1659685832648.png

然后网上百度了下分页插件的坑,找到了解决方案。因为我们的这几张关联表中,都有一个相同的字段,所以我们就随便在一个LeftJoin表中加入了一个参数,用来破坏源码的规则,这样子总数就对了。参考的解决方案连接是

之前的sql(不知道为何要写这么多leftJoin)

 SELECT
        scp.id,
        scpts.site_id siteId,
        scl.`name` lineName,
        scs.`name` siteName,
        scp.plan_date planDate,
        scp.start_time startTime,
        scp.end_time endTime,
        scr.complete_time completeTime,
        scr.workers,
        scr.except_count exceptCount
        FROM
        ops_check_plan scp
        LEFT JOIN ops_check_line scl ON scl.id = scp.line_id
        LEFT JOIN ops_check_plan_to_site scpts ON scpts.plan_id = scp.id
        LEFT JOIN ops_check_site scs ON scpts.site_id = scs.id
        LEFT JOIN ops_check_record scr ON scpts.plan_id = scr.plan_id and scr.site_id = scpts.site_id
        where
            scp.id = #{id}
        order by scr.complete_time desc
    </select>

总数正确过后的sql

 SELECT
        scp.id,
        scpts.site_id siteId,
        scl.`name` lineName,
        scs.`name` siteName,
        scp.plan_date planDate,
        scp.start_time startTime,
        scp.end_time endTime,
        scr.complete_time completeTime,
        scr.workers,
        scr.except_count exceptCount
        FROM
        ops_check_plan scp
        LEFT JOIN ops_check_line scl ON scl.id = scp.line_id
        LEFT JOIN ops_check_plan_to_site scpts ON scpts.plan_id = scp.id
        LEFT JOIN ops_check_site scs ON scpts.site_id = scs.id
        LEFT JOIN ops_check_record scr ON scpts.plan_id = scr.plan_id and scr.site_id = scpts.site_id
        where
            scp.id = #{id}
            <if test="projectId != null and projectId != ''">
            and scl.project_id =#{projectId}
            </if>
        order by scr.complete_time desc
    </select>

相关文章

网友评论

      本文标题:mybatis-plus分页插件-分页的total总数不对

      本文链接:https://www.haomeiwen.com/subject/wutpwrtx.html