PageHelper自定义limit和自定义count

时间:2025-03-17 07:20:21

 目录

前言

一、自定义limit的位置

二、自定义count

总结


前言

最近有用到分页,使用的是PageHelper。但是发现在一对多查询会出现数量不一致的问题,这是因为PageHelper默认对整个sql语句进行分页,所以我们只需要对分页的位置进行调整即可。本文记录一下解决过程。


提示:以下是本篇文章正文内容,下面案例可供参考

一、自定义limit的位置

  • 继承MySqlDialect,实现getPageSql和processPageParameter方法。
    import ;
    import ;
    import ;
    import .slf4j.Slf4j;
    import .;
    import ;
    import ;
    import ;
    import ;
    import ;
    
    import ;
    import ;
    import ;
    import ;
    import ;
    
    @Slf4j
    public class PageMySqlDialectPlus extends MySqlDialect {
    
        //正则表达式
        private static final String pattern = "([\\s|\\S]*?)/\\*\\s*MAPPINGLIMIT\\s*\\*/\\s*([\\s|\\S]*)";
        private static final Pattern PATTERN = (pattern, Pattern.CASE_INSENSITIVE);
    
        /**
         * 把limit语句放到 MAPPINGLIMIT标记所在的位置,也就是主表的位置,对主表进行分页
         *
         * @return 加limit后的sql
         */
        @Override
        public String getPageSql(String sql, Page page, CacheKey pageKey) {
            //如果不匹配正则,走原始的sql
            if (!(pattern, sql)) {
                return (sql, page, pageKey);
            }
    
            String beforeLimitSql = "";
            String afterLimitsql = "";
            Matcher m = (sql);
            if (()) {
                //MAPPINGLIMIT标记前的sql语句
                beforeLimitSql = (1);
                //MAPPINGLIMIT标记后的sql语句
                afterLimitsql = (2);
            }
    
            String limitSql = "";
            if (() == 0) {
                limitSql = " LIMIT ? ";
            } else {
                limitSql = " LIMIT ?, ? ";
            }
            String sqlString = beforeLimitSql + " " + limitSql + " " + afterLimitsql;
    
            return sqlString;
        }
    
        /**
         * 把分页参数放到参数列表里
         *
         * @return
         */
        @Override
        public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
            //如果不匹配正则,走原始的sql设置
            if (!(pattern, ())) {
                return (ms, paramMap, page, boundSql, pageKey);
            }
            //设置参数
            (PAGEPARAMETER_FIRST, ());
            (PAGEPARAMETER_SECOND, ());
            (());
            (());
    
            //设置参数 因为limit放到中间位置,所以要计算出来分页数据的放置位置
            Matcher m = (());
            String beforeLimitSql = null;
            int limitIndex;
            if (()) {
                //MAPPINGLIMIT标记前的sql语句
                beforeLimitSql = (1);
            }
    
            //计算sql里有几个参数,按数据位置添加page
            limitIndex = (beforeLimitSql, "?");
            if (() != null) {
                List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(());
                if (() == 0) {
                    (limitIndex,new ((), PAGEPARAMETER_SECOND, ).build());
                } else {
                    (limitIndex,new ((), PAGEPARAMETER_FIRST, ).build());
                    (limitIndex+1,new ((), PAGEPARAMETER_SECOND, ).build());
                }
                MetaObject metaObject = (boundSql);
                ("parameterMappings", newParameterMappings);
            }
            return paramMap;
        }
    }
  • 在配置里加上PageHelper的配置。其中helperDialect属性的值是我们刚刚自定义的MySqlDialectPlus的路径。
    #pagehelper分页插件配置
    pagehelper:
      helperDialect: 
      reasonable: true
      supportMethodsArguments: true
      params: count=countSql
  • xml文件的查询语句。其中/* MAPPINGLIMIT */是我们想要分页的位置,即 limit ?,? 的位置。 该代码查询班级列表,其中班级包括多个学生,我们需要在班级后面分页。
    <resultMap  type="">
        <id column="class_id" property="classId"/>
        <result column="class_name" property="className"/>
        <collection property="students" ofType="">
            <id column="id" property="id"/>
            <result column="student_name" property="studentName"/>
            <result column="age" property="age"/>
            <result column="class_id" property="classId"/>
        </collection>
    </resultMap>
    
    <select  resultMap="map1">
        select c.class_id, c.class_name, , s.student_name, 
        from (select * from class /* MAPPINGLIMIT */) c
                 left join student s on c.class_id = s.class_id
        where s.student_name = #{studentName}
    </select>

二、自定义count

自定义完limit的位置之后,查询还是会有问题。因为PageHelper默认查询的count也是对于整个sql语句的,我们需要同时自定义count。其中select的id是原查询代码id后面加上 "_COUNT"。

<select  resultType="">
    select count(0) from class
</select>

总结

使用PageHelper时,对于一对多的分页查询,我们需要对limit的位置和count进行自定义。

参考

pagehelper一对多分页查询问题优化 - Ta夏了夏天 - 博客园 ()