目录
前言
最近有用到分页,使用的是PageHelper。但是发现在一对多查询会出现数量不一致的问题,这是因为PageHelper默认对整个sql语句进行分页,所以我们只需要对分页的位置进行调整即可。本文记录一下解决过程。
提示:以下是本篇文章正文内容,下面案例可供参考
一、自定义limit的位置
- 继承MySqlDialect,实现getPageSql和processPageParameter方法。
import com.github.pagehelper.Page; import com.github.pagehelper.dialect.helper.MySqlDialect; import com.github.pagehelper.util.MetaObjectUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.reflection.MetaObject; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @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.compile(pattern, Pattern.CASE_INSENSITIVE); /** * 把limit语句放到 MAPPINGLIMIT标记所在的位置,也就是主表的位置,对主表进行分页 * * @return 加limit后的sql */ @Override public String getPageSql(String sql, Page page, CacheKey pageKey) { //如果不匹配正则,走原始的sql if (!Pattern.matches(pattern, sql)) { return super.getPageSql(sql, page, pageKey); } String beforeLimitSql = ""; String afterLimitsql = ""; Matcher m = PATTERN.matcher(sql); if (m.find()) { //MAPPINGLIMIT标记前的sql语句 beforeLimitSql = m.group(1); //MAPPINGLIMIT标记后的sql语句 afterLimitsql = m.group(2); } String limitSql = ""; if (page.getStartRow() == 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.matches(pattern, boundSql.getSql())) { return super.processPageParameter(ms, paramMap, page, boundSql, pageKey); } //设置参数 paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow()); paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize()); pageKey.update(page.getStartRow()); pageKey.update(page.getPageSize()); //设置参数 因为limit放到中间位置,所以要计算出来分页数据的放置位置 Matcher m = PATTERN.matcher(boundSql.getSql()); String beforeLimitSql = null; int limitIndex; if (m.find()) { //MAPPINGLIMIT标记前的sql语句 beforeLimitSql = m.group(1); } //计算sql里有几个参数,按数据位置添加page limitIndex = StringUtils.countMatches(beforeLimitSql, "?"); if (boundSql.getParameterMappings() != null) { List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings()); if (page.getStartRow() == 0) { newParameterMappings.add(limitIndex,new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, int.class).build()); } else { newParameterMappings.add(limitIndex,new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, long.class).build()); newParameterMappings.add(limitIndex+1,new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, int.class).build()); } MetaObject metaObject = MetaObjectUtil.forObject(boundSql); metaObject.setValue("parameterMappings", newParameterMappings); } return paramMap; } }
- 在application.yml配置里加上PageHelper的配置。其中helperDialect属性的值是我们刚刚自定义的MySqlDialectPlus的路径。
#pagehelper分页插件配置 pagehelper: helperDialect: com.zs.config.PageMySqlDialectPlus reasonable: true supportMethodsArguments: true params: count=countSql
- xml文件的查询语句。其中/* MAPPINGLIMIT */是我们想要分页的位置,即 limit ?,? 的位置。 该代码查询班级列表,其中班级包括多个学生,我们需要在班级后面分页。
<resultMap id="map1" type="com.zs.entity.dto.ClassDto"> <id column="class_id" property="classId"/> <result column="class_name" property="className"/> <collection property="students" ofType="com.zs.entity.Student"> <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 id="findAll" resultMap="map1"> select c.class_id, c.class_name, s.id, s.student_name, s.age 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 id="findAll_COUNT" resultType="java.lang.Long">
select count(0) from class
</select>
总结
使用PageHelper时,对于一对多的分页查询,我们需要对limit的位置和count进行自定义。