pagerHelper与mybatisPlus分页冲突问题分析
问题现象
在开发owner服务的时候发现,mybatisPlus分页不起作用,返回总数永远是0.
//mybatisPlus分页接口
public interface IService<T> {
//返回的page信息不包含total等信息,只显示一页内容
Page<T> selectPage(Page<T> page);
}
- jar包版本
- mybatis-plus 2.0.5
- pagehelper-spring-boot-starter 1.2.5
- pagehelper 5.1.4
问题分析
由于不管是pageHelper还是mybatis-plus分页的实现逻辑都是基于mybatis的拦截器来实现的,即Interceptor。因此初步推断是这里产生了冲突。
mybatis-plus实现逻辑
通过查看源码可以发现实现分页逻辑的拦截器是PaginationInterceptor
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
public class PaginationInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
if (target instanceof StatementHandler) {
StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
//这里通过rowBounds的类型来判断是否分页
if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
return invocation.proceed();
}
BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
String originalSql = (String) boundSql.getSql();
if (rowBounds instanceof Pagination) {
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
Pagination page = (Pagination) rowBounds;
boolean orderBy = true;
if (page.isSearchCount()) {
CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
page.isOptimizeCount());
orderBy = countOptimize.isOrderBy();
this.count(countOptimize.getCountSQL(), mappedStatement, boundSql, page);
if (page.getTotal() <= 0) {
return invocation.proceed();
}
}
String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
} else {
originalSql =