最近对自己的一个springCloud项目重构过程中,重构前一直运作正常的分页突然不生效了
分页配置mybatis.xml
"http://mybatis.org/dtd/mybatis-3-config.dtd">
分页插件代码
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
ParameterHandler parameterHandler = statementHandler.getParameterHandler();
BoundSql boundSql = statementHandler.getBoundSql();
MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY,
DEFAULT_OBJECT_WRAPPER_FACTORY,
DEFAULT_REFLECTOR_FACTORY);
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
// 没有分页参数
if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
return invocation.proceed();
}
Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration");
Dialect dialect = DialectFactory.buildDialect(configuration);
String originalSql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");
// 获取总记录数
Page> page = (Page>) rowBounds;
String countSql = dialect.getCountString(originalSql);
Connection connection = (Connection) invocation.getArgs()[0];
int total = getTotal(parameterHandler, connection, countSql);
page.setTotal(total);
// 设置物理分页语句
metaStatementHandler.setValue("delegate.boundSql.sql",
dialect.getLimitString(originalSql, page.getOffset(), page.getLimit()));
// 屏蔽mybatis原有分页
metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
logger.error("分页SQL : " + boundSql.getSql());
if (logger.isDebugEnabled()) {
logger.debug("分页SQL : " + boundSql.getSql());
}
return invocation.proceed();
}
重构前后都没有修改这部分代码,但重构后的
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
获取到的却是默认org.apache.ibatis.session.RowBounds
通过debug发现boundSql的parameterMappings多了一些参数,是我没有传的,一个是Second_pageHelper
这时候就怀疑应该是有什么插件帮我传参导致我自定义的RowBounds没有传进来。
查看POM文件发现原来升级Springboot的时候由于拷贝了别的项目的pom文件,多拷贝了
屏蔽后一切正常了。问题解决