共计 2626 个字符,预计需要花费 7 分钟才能阅读完成。
IPage 外部原理也是基于拦截器,然而这个拦挡的是办法以及办法中的参数,这个也会判断是否是查问操作。如果是查问操作,才会进入分页的解决逻辑。进入分页逻辑解决后,拦截器会通过反射获取该办法的参数 进行判断是否存在 IPage 对象的实现类。如果不存在则不进行分页,存在则将该参数赋值给 IPage 对象。而后进行拼接 sql 的解决实现分页操作。然而应用 IPage 须要注入一个 bean 拦截器交给 spring 进行治理。如下。否则不会进行拦挡
- 注入 paginationInterceptor 分页 bean
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
- 拦挡源码
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);
}
String originalSql = boundSql.getSql();
Connection connection = (Connection) invocation.getArgs()[0];
if (page.isSearchCount() && !page.isHitCount()) {SqlInfo sqlInfo = SqlParserUtils.getOptimizeCountSql(page.optimizeCountSql(), countSqlParser, originalSql);
this.queryTotal(sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
if (page.getTotal() <= 0) {return null;}
}
DbType dbType = Optional.ofNullable(this.dbType).orElse(JdbcUtils.getDbType(connection.getMetaData().getURL()));
IDialect dialect = Optional.ofNullable(this.dialect).orElse(DialectFactory.getDialect(dbType));
String buildSql = concatOrderBy(originalSql, page);
// 执行物理分页
DialectModel model = dialect.buildPaginationSql(buildSql, page.offset(), page.getSize());
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();}
参考博客
`https://blog.csdn.net/xiaolegeaizy/article/details/108461284
正文完
发表至: mybatis-plus
2020-11-05