package com.template.common.utils; 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 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 newParameterMappings = new ArrayList(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; } }