前言
今天使用了分页插件,并将其整合到SpringBoot中。各种遇到了个别问题,现在记录下。吃一垫长一智。
整合
与SpringBoot整合
<!--pagehelper 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
接着在application.yml中配置相关参数
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
参数说明
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
#方式1
PageHelper.startPage(1, 10);
List<ScoreGoodsCategory> goodsCategoryList = mapper.selectByPage();
int totalCount=(int) ((Page)goodsCategoryList).getTotal();
# 方式二
PageHelper.offsetPage(1, 10);
List<ScoreGoodsCategory> goodsCategoryList = mapper.selectByPage();
PageInfo<ScoreGoodsCategory> pageInfo = new PageInfo<>(goodsCategoryList);
int totalCount=(int) pageInfo.getTotal();
与Spring MVC 整合
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>最新版本</version>
</dependency>
在Spring的配置文件中配置拦截器插件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注意其他配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
params=count=countSql
</value>
<value>
helperDialect=mysql
</value>
<value>
reasonable=true
</value>
<value>
supportMethodsArguments=true
</value>
<value>
returnPageInfo=check
</value>
</property>
</bean>
</array>
</property>
</bean>
配置好之后,使用同上。
原理
其最核心的方法就在拦截器中,那我们首先看看拦截器中的拦截方法。该方法主要做了两件事,1. 统计总条数,2.对原始的SQL进行改写使其可以分页。PageInterceptor
类的intercept
方法是拦截器的总入口方法。
1.统计总条数
首先,我们来看看统计总条数的相关代码。
//PageInterceptor 类
//设置count的sql的id,在原始的msId后面加上_COUNT后缀。
String countMsId = msId + countSuffix;
// 生成统计sql的入口方法
count = executeAutoCount(executor, countMs, parameter, boundSql, rowBounds, resultHandler);
接下来我们就来看看executeAutoCount 方法
/**
* 执行自动生成的 count 查询
*
* @param executor sql执行器
* @param countMs
* @param parameter
* @param boundSql
* @param rowBounds
* @param resultHandler 结果处理器
* @return
* @throws IllegalAccessException
* @throws SQLException
*/
private Long executeAutoCount(Executor executor, MappedStatement countMs,
Object parameter, BoundSql boundSql,
RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException, SQLException {
Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql);
//创建 count 查询的缓存 key
CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
//调用方言获取 count sql
String countSql = dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey);
//countKey.update(countSql);
BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
//当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中
for (String key : additionalParameters.keySet()) {
countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
}
//执行 count 查询
Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
Long count = (Long) ((List) countResultList).get(0);
return count;
}
如上,方法的注释比较详实,此处我们主要介绍下第二步调用方言获取 count sql。dialect 是一个接口类,PageHelper
是其的一个实现类。接着我们来看看PageHelper中的getCountSql方法。
。。。。。。。。。。。。。。。。。
版权原因,完整文章,请参考如下: