mybatis+mybatisplus源码学习(七)分页插件PaginationInterceptor
目录前言一、分页插件二、分页功能总结前言Mybatisplus中的PaginationInterceptor主要用于处理数据库的物理分页,避免内存分页。一、分页插件PaginationInterceptor 实现了Interceptor ,用于拦截StatementHandler的prepare方法@Setter@Accessors(chain = true)@Intercepts({@Signa
·
前言
Mybatisplus中的 PaginationInterceptor 主要用于处理数据库的物理分页,避免内存分页。
一、分页插件
PaginationInterceptor 实现了Interceptor ,用于拦截StatementHandler的prepare方法
@Setter
@Accessors(chain = true)
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class PaginationInterceptor extends AbstractSqlParserHandler implements Interceptor {
/**
* COUNT SQL 解析
*/
protected ISqlParser countSqlParser;
/**
* 溢出总页数后是否进行处理
*/
protected boolean overflow = false;
/**
* 单页限制 500 条,小于 0 如 -1 不受限制
*/
protected long limit = 500L;
/**
* 方言类型(数据库名,全小写) <br>
* 如果用的我们支持分页的数据库但获取数据库类型不正确则可以配置该值进行校正
*/
protected String dialectType;
/**
* 方言实现类<br>
* 注意!实现 com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect 接口的子类
*/
protected String dialectClazz;
//创建代理
@Override
public Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
}
return target;
}
//获取参数,获取方言类型、方言类
@Override
public void setProperties(Properties prop) {
String dialectType = prop.getProperty("dialectType");
String dialectClazz = prop.getProperty("dialectClazz");
if (StringUtils.isNotBlank(dialectType)) {
this.dialectType = dialectType;
}
if (StringUtils.isNotBlank(dialectClazz)) {
this.dialectClazz = dialectClazz;
}
}
}
二、分页功能
主要通过获取参数中的IPage对象,获取出分页参数,通过数据库方言修改成分页的sql语句
(1)分页拦截方法
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
// SQL 解析
this.sqlParser(metaObject);
// 先判断是不是SELECT操作 (2019-04-10 00:37:31 跳过存储过程)
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
if (SqlCommandType.SELECT != mappedStatement.getSqlCommandType()
|| StatementType.CALLABLE == mappedStatement.getStatementType()) {
return invocation.proceed();
}
// 针对定义了rowBounds,做为mapper接口方法的参数
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
Object paramObj = boundSql.getParameterObject();
// 判断参数里是否有page对象
IPage<?> page = null;
if (paramObj instanceof IPage) {
page = (IPage<?>) paramObj;
} else if (paramObj instanceof Map) {
for (Object arg : ((Map<?, ?>) paramObj).values()) {
if (arg instanceof IPage) {
page = (IPage<?>) arg;
break;
}
}
}
/*
* 不需要分页的场合,如果 size 小于 0 返回结果集
*/
if (null == page || page.getSize() < 0) {
return invocation.proceed();
}
if (this.limit > 0 && this.limit <= page.getSize()) {
//处理单页条数限制
handlerLimit(page);
}
//原始sql
String originalSql = boundSql.getSql();
//数据库连接
Connection connection = (Connection) invocation.getArgs()[0];
//数据库类型,通过dialectType获取或通过连接中url中获取
DbType dbType = StringUtils.isNotBlank(dialectType) ? DbType.getDbType(dialectType)
: JdbcUtils.getDbType(connection.getMetaData().getURL());
if (page.isSearchCount()) {
//对select count 进行优化
SqlInfo sqlInfo = SqlParserUtils.getOptimizeCountSql(page.optimizeCountSql(), countSqlParser, originalSql);
//获取查询总数total
this.queryTotal(sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
if (page.getTotal() <= 0) {
return null;
}
}
//sql中加入orderBy
String buildSql = concatOrderBy(originalSql, page);
//构建出分页sql
DialectModel model = DialectFactory.buildPaginationSql(page, buildSql, dbType, dialectClazz);
Configuration configuration = mappedStatement.getConfiguration();
List<ParameterMapping> mappings = new ArrayList<>(boundSql.getParameterMappings());
Map<String, Object> additionalParameters = (Map<String, Object>) metaObject.getValue("delegate.boundSql.additionalParameters");
model.consumers(mappings, configuration, additionalParameters);
metaObject.setValue("delegate.boundSql.sql", model.getDialectSql());
metaObject.setValue("delegate.boundSql.parameterMappings", mappings);
return invocation.proceed();
}
(2)获取查询total的sql
SqlParserUtils.getOptimizeCountSql( )
/**
* 获取CountOptimize
*
* @param optimizeCountSql 是否优化 Count SQL
* @param sqlParser Count SQL 解析类
* @param originalSql 需要计算Count SQL
* @return SqlInfo
*/
public static SqlInfo getOptimizeCountSql(boolean optimizeCountSql, ISqlParser sqlParser, String originalSql) {
if (!optimizeCountSql) {
return SqlInfo.newInstance().setSql(getOriginalCountSql(originalSql));
}
// COUNT SQL 解析器
if (null == COUNT_SQL_PARSER) {
if (null != sqlParser) {
// 用户自定义 COUNT SQL 解析
COUNT_SQL_PARSER = sqlParser;
} else {
// 默认 JsqlParser 优化 COUNT
COUNT_SQL_PARSER = new JsqlParserCountOptimize();
}
}
return COUNT_SQL_PARSER.parser(null, originalSql);
}
/**
* 获取 COUNT 原生 SQL 包装
*
* @param originalSql ignore
* @return ignore
*/
public static String getOriginalCountSql(String originalSql) {
return String.format("SELECT COUNT(1) FROM ( %s ) TOTAL", originalSql);
}
public SqlInfo parser(MetaObject metaObject, String sql) {
if (logger.isDebugEnabled()) {
logger.debug("JsqlParserCountOptimize sql=" + sql);
}
SqlInfo sqlInfo = SqlInfo.newInstance();
try {
//解析sql
Select selectStatement = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
//distinct
Distinct distinct = plainSelect.getDistinct();
//groupBy
GroupByElement groupBy = plainSelect.getGroupBy();
//orderBy
List<OrderByElement> orderBy = plainSelect.getOrderByElements();
// 添加包含groupBy 不去除orderBy
if (null == groupBy && CollectionUtils.isNotEmpty(orderBy)) {
plainSelect.setOrderByElements(null);
sqlInfo.setOrderBy(false);
}
//#95 Github, selectItems contains #{} ${}, which will be translated to ?, and it may be in a function: power(#{myInt},2)
//存在函数计算时,不进行优化
for (SelectItem item : plainSelect.getSelectItems()) {
if (item.toString().contains(StringPool.QUESTION_MARK)) {
return sqlInfo.setSql(SqlParserUtils.getOriginalCountSql(selectStatement.toString()));
}
}
// 包含 distinct、groupBy不优化
if (distinct != null || null != groupBy) {
return sqlInfo.setSql(SqlParserUtils.getOriginalCountSql(selectStatement.toString()));
}
// 包含 join 连表,进行判断是否移除 join 连表
List<Join> joins = plainSelect.getJoins();
if (optimizeJoin && CollectionUtils.isNotEmpty(joins)) {
boolean canRemoveJoin = true;
String whereS = Optional.ofNullable(plainSelect.getWhere()).map(Expression::toString).orElse(StringPool.EMPTY);
for (Join join : joins) {
//存在不是左连接的,不优化join
if (!join.isLeft()) {
canRemoveJoin = false;
break;
}
Table table = (Table) join.getRightItem();
String str = Optional.ofNullable(table.getAlias()).map(Alias::getName).orElse(table.getName()) + StringPool.DOT;
String onExpressionS = join.getOnExpression().toString();
/* 如果 join 里包含 ?(代表有入参) 或者 where 条件里包含使用 join 的表的字段作条件,就不移除 join */
if (onExpressionS.contains(StringPool.QUESTION_MARK) || whereS.contains(str)) {
canRemoveJoin = false;
break;
}
}
if (canRemoveJoin) {
plainSelect.setJoins(null);
}
}
// 优化 SQL
plainSelect.setSelectItems(COUNT_SELECT_ITEM);
return sqlInfo.setSql(selectStatement.toString());
} catch (Throwable e) {
// 无法优化使用原 SQL
return sqlInfo.setSql(SqlParserUtils.getOriginalCountSql(sql));
}
}
(3)sql中加入orderBy
将Page参数中的orderBy合并到sql中的orderBy中
public static String concatOrderBy(String originalSql, IPage<?> page) {
if (CollectionUtils.isNotEmpty(page.orders())) {
try {
//获取page中的orderBy
List<OrderItem> orderList = page.orders();
Select selectStatement = (Select) CCJSqlParserUtil.parse(originalSql);
if (selectStatement.getSelectBody() instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
//获取sql中的orderBy
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
//合并
List<OrderByElement> orderByElementsReturn = addOrderByElements(orderList, orderByElements);
plainSelect.setOrderByElements(orderByElementsReturn);
return plainSelect.toString();
} else if (selectStatement.getSelectBody() instanceof SetOperationList) {
SetOperationList setOperationList = (SetOperationList) selectStatement.getSelectBody();
List<OrderByElement> orderByElements = setOperationList.getOrderByElements();
List<OrderByElement> orderByElementsReturn = addOrderByElements(orderList, orderByElements);
setOperationList.setOrderByElements(orderByElementsReturn);
return setOperationList.toString();
} else if (selectStatement.getSelectBody() instanceof WithItem) {
// todo: don't known how to resole
return originalSql;
} else {
return originalSql;
}
} catch (JSQLParserException e) {
logger.warn("failed to concat orderBy from IPage, exception=" + e.getMessage());
}
}
return originalSql;
}
(4)获取分页sql
DialectModel model = DialectFactory.buildPaginationSql(page, buildSql, dbType, dialectClazz);
public static DialectModel buildPaginationSql(IPage<?> page, String buildSql, DbType dbType, String dialectClazz) {
// fix #196 获取数据库方言,再组装分页sql
return getDialect(dbType, dialectClazz).buildPaginationSql(buildSql, page.offset(), page.getSize());
}
例如MySqlDialect就是通过加上limit参数
public class MySqlDialect implements IDialect {
@Override
public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
String sql = originalSql + " LIMIT " + FIRST_MARK + StringPool.COMMA + SECOND_MARK;
return new DialectModel(sql, offset, limit).setConsumerChain();
}
}
总结
使用Mybatisplus的分页插件后,只需要在mapper方法中传入IPage参数就能自动实现分页。
更多推荐
已为社区贡献5条内容
所有评论(0)