上篇我们了解了Mybatis的plugin,最后顺便提到了分页插件,现在我们来看看分页插件PageHelper怎么实现分页的。
1.PageHelper的插件配置
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="dialect" value="mysql" />
<property name="offsetAsPageNum" value="false" />
<property name="rowBoundsWithCount" value="true" />
<property name="reasonable" value="true" />
</plugin>
</plugins>
offsetAsPageNum:该参数默认为false,设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用,和startPage中的pageNum效果一样。
rowBoundsWithCount:该参数默认为false,设置为true时,使用RowBounds分页会进行count查询。
reasonable:设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 。
2. PageHelper源码分析
我们一般是通过PageHelper.startPage()静态方法实现分页的, 要想知道PageHelper怎么实现分页的,看看PageHelper的源码就差不读了,代码如下:
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
public class PageHelper implements Interceptor {
//sql工具类
private SqlUtil sqlUtil;
//属性参数信息
private Properties properties;
//配置对象方式
private SqlUtilConfig sqlUtilConfig;
//自动获取dialect,如果没有setProperties或setSqlUtilConfig,也可以正常进行
private boolean autoDialect = true;
//运行时自动获取dialect
private boolean autoRuntimeDialect;
//多数据源时,获取jdbcurl后是否关闭数据源
private boolean closeConn = true;
//缓存
private Map<String, SqlUtil> urlSqlUtilMap = new ConcurrentHashMap<String, SqlUtil>();
private ReentrantLock lock = new ReentrantLock();
// ...
}
SqlUtil:数据库类型专用sql工具类,一个数据库url对应一个SqlUtil实例,SqlUtil内有一个Parser对象,如果是mysql,它是MysqlParser,如果是oracle,它是OracleParser。
Map<String, SqlUtil> urlSqlUtilMap:它就用来缓存autoRuntimeDialect自动检测结果的,key是数据库的url,value是SqlUtil。
/**
* Mybatis拦截器方法
*
* @param invocation 拦截器入参
* @return 返回执行结果
* @throws Throwable 抛出异常
*/
public Object intercept(Invocation invocation) throws Throwable {
if (autoRuntimeDialect) {
//多数据源
SqlUtil sqlUtil = getSqlUtil(invocation);
return sqlUtil.processPage(invocation);
} else {
//单数据源
if (autoDialect) {
initSqlUtil(invocation);
}
//指定dialect
return sqlUtil.processPage(invocation);
}
}
/**
* 根据datasource创建对应的sqlUtil
*
* @param invocation
*/
public SqlUtil getSqlUtil(Invocation invocation) {
MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
//改为对dataSource做缓存
DataSource dataSource = ms.getConfiguration().getEnvironment().getDataSource();
String url = getUrl(dataSource);
if (urlSqlUtilMap.containsKey(url)) {
return urlSqlUtilMap.get(url);
}
try {
lock.lock();
if (urlSqlUtilMap.containsKey(url)) {
return urlSqlUtilMap.get(url);
}
if (StringUtil.isEmpty(url)) {
throw new RuntimeException("无法自动获取jdbcUrl,请在分页插件中配置dialect参数!");
}
String dialect = Dialect.fromJdbcUrl(url);
if (dialect == null) {
throw new RuntimeException("无法自动获取数据库类型,请通过dialect参数指定!");
}
SqlUtil sqlUtil = new SqlUtil(dialect);
if (this.properties != null) {
sqlUtil.setProperties(properties);
} else if (this.sqlUtilConfig != null) {
sqlUtil.setSqlUtilConfig(this.sqlUtilConfig);
}
urlSqlUtilMap.put(url, sqlUtil);
return sqlUtil;
} finally {
lock.unlock();
}
}
autoRuntimeDialect:多数据源,会创建多个SqlUtil。
autoDialect:单数据源,只会创建1个SqlUtil。
指定dialect:只会创建1个SqlUtil。
3. PageSqlSource
public abstract class PageSqlSource implements SqlSource {
/**
* 获取正常的BoundSql
*
* @param parameterObject
* @return
*/
protected abstract BoundSql getDefaultBoundSql(Object parameterObject);
/**
* 获取Count查询的BoundSql
*
* @param parameterObject
* @return
*/
protected abstract BoundSql getCountBoundSql(Object parameterObject);
/**
* 获取分页查询的BoundSql
*
* @param parameterObject
* @return
*/
protected abstract BoundSql getPageBoundSql(Object parameterObject);
/**
* 获取BoundSql
*
* @param parameterObject
* @return
*/
@Override
public BoundSql getBoundSql(Object parameterObject) {
Boolean count = getCount();
if (count == null) {
return getDefaultBoundSql(parameterObject);
} else if (count) {
return getCountBoundSql(parameterObject);
} else {
return getPageBoundSql(parameterObject);
}
}
}
public class PageStaticSqlSource extends PageSqlSource {
private String sql;
private List<ParameterMapping> parameterMappings;
private Configuration configuration;
private SqlSource original;
@Override
protected BoundSql getDefaultBoundSql(Object parameterObject) {
String tempSql = sql;
String orderBy = PageHelper.getOrderBy();
if (orderBy != null) {
tempSql = OrderByParser.converToOrderBySql(sql, orderBy);
}
return new BoundSql(configuration, tempSql, parameterMappings, parameterObject);
}
@Override
protected BoundSql getCountBoundSql(Object parameterObject) {
// localParser指的就是MysqlParser或者OracleParser
// localParser.get().getCountSql(sql),可以根据原始的sql,生成一个count查询的sql
return new BoundSql(configuration, localParser.get().getCountSql(sql), parameterMappings, parameterObject);
}
@Override
protected BoundSql getPageBoundSql(Object parameterObject) {
String tempSql = sql;
String orderBy = PageHelper.getOrderBy();
if (orderBy != null) {
tempSql = OrderByParser.converToOrderBySql(sql, orderBy);
}
// getPageSql可以根据原始的sql,生成一个带有分页参数信息的sql,比如 limit ?, ?
tempSql = localParser.get().getPageSql(tempSql);
// 由于sql增加了分页参数的?号占位符,getPageParameterMapping()就是在原有List<ParameterMapping>基础上,增加两个分页参数对应的ParameterMapping对象,为分页参数赋值使用
return new BoundSql(configuration, tempSql, localParser.get().getPageParameterMapping(configuration, original.getBoundSql(parameterObject)), parameterObject);
}
}
4. 分页查询
其实分页查询是执行PageHelper的intercept()方法,通过一系列的追踪,最后会执行的是SqlUtil的doProcessPage()方法,代码如下:
/**
* Mybatis拦截器方法
*
* @param invocation 拦截器入参
* @return 返回执行结果
* @throws Throwable 抛出异常
*/
private Page doProcessPage(Invocation invocation, Page page, Object[] args) throws Throwable {
//保存RowBounds状态
RowBounds rowBounds = (RowBounds) args[2];
//获取原始的ms
MappedStatement ms = (MappedStatement) args[0];
//判断并处理为PageSqlSource
if (!isPageSqlSource(ms)) {
processMappedStatement(ms);
}
//设置当前的parser,后面每次使用前都会set,ThreadLocal的值不会产生不良影响
((PageSqlSource)ms.getSqlSource()).setParser(parser);
try {
//忽略RowBounds-否则会进行Mybatis自带的内存分页
args[2] = RowBounds.DEFAULT;
//如果只进行排序 或 pageSizeZero的判断
if (isQueryOnly(page)) {
return doQueryOnly(page, invocation);
}
//简单的通过total的值来判断是否进行count查询
if (page.isCount()) {
page.setCountSignal(Boolean.TRUE);
//替换MS
args[0] = msCountMap.get(ms.getId());
//查询总数
Object result = invocation.proceed();
//还原ms
args[0] = ms;
//设置总数
page.setTotal((Integer) ((List) result).get(0));
if (page.getTotal() == 0) {
return page;
}
} else {
page.setTotal(-1l);
}
//pageSize>0的时候执行分页查询,pageSize<=0的时候不执行相当于可能只返回了一个count
if (page.getPageSize() > 0 &&
((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0)
|| rowBounds != RowBounds.DEFAULT)) {
//将参数中的MappedStatement替换为新的qs
page.setCountSignal(null);
BoundSql boundSql = ms.getBoundSql(args[1]);
args[1] = parser.setPageParameter(ms, args[1], boundSql, page);
page.setCountSignal(Boolean.FALSE);
//执行分页查询
Object result = invocation.proceed();
//得到处理结果
page.addAll((List) result);
}
} finally {
((PageSqlSource)ms.getSqlSource()).removeParser();
}
//返回结果
return page;
}
最后通过Invocatuon的proceed方法查到结果,而这个方法其实是在执行Executor的query()方法,上面的getBoundSql、setPageParameter方法可以通过第三点知道,只是把分页参数设置到sql语句里面去。