最近对自己的一个springCloud项目重构过程中,重构前一直运作正常的分页突然不生效了
分页配置mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties>
<property name="dialectClass" value="com.iori.common.support.orm.dialect.MySql5Dialect"/>
</properties>
<!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 允许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true"/>
<!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
<setting name="useGeneratedKeys" value="false"/>
<!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->
<!-- <setting name="defaultExecutorType" value="BATCH" /> -->
<!-- 数据库超过25000秒仍未响应则超时 -->
<!-- <setting name="defaultStatementTimeout" value="25000" /> -->
<!-- Allows using RowBounds on nested statements -->
<setting name="safeRowBoundsEnabled" value="false"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT
local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
<setting name="localCacheScope" value="SESSION"/>
<!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values
like NULL, VARCHAR or OTHER. -->
<setting name="jdbcTypeForNull" value="OTHER"/>
<!-- Specifies which Object's methods trigger a lazy load -->
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<plugins>
<plugin interceptor="com.iori.common.support.orm.plugins.PaginationResultSetHandlerInterceptor"/>
<plugin interceptor="com.iori.common.support.orm.plugins.PaginationStatementHandlerInterceptor"/>
</plugins>
<mappers>
<package name="com.iori.**.dao"/>
</mappers>
</configuration>
分页插件代码
@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文件,多拷贝了
<!-- mybatis的分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
屏蔽后一切正常了。问题解决