MyBatis 分页插件 PageHelper:是如何拦截SQL进行分页
目录
Springboot项目集成
分页插件参数介绍
如何选择配置这些参数
场景一
场景二
场景三
场景四
场景五
PageHelper的使用
PageHelper实现原理1: interceptor
1. springboot 中接入interceptor
2. interceptor的初始化
3. interceptor的调用过程
4. 是否跳过分页判定
5. PageHelper的 count 操作
6. select list 的改装
思考
ThreadLocal在PageHelper中的应用
PageHelper通过ThreadLocal来共享分页信息,那么它是何时进行清除的呢?
什么时候会导致不安全的分页?
Springboot项目集成
pom.xml中引入依赖
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.3.0</version>
</dependency>
application.yml中引入配置
pagehelper:helperDialect: mysqlreasonable: truesupportMethodsArguments: trueparams: count=countSql
分页插件参数介绍
helperDialect
:分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置helperDialect
属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:
oracle
,mysql
,mariadb
,sqlite
,hsqldb
,postgresql
,db2
,sqlserver
,informix
,h2
,sqlserver2012
,derby
特别注意:使用 SqlServer2012 数据库时,需要手动指定为sqlserver2012
,否则会使用 SqlServer2005 的方式进行分页。
你也可以实现AbstractHelperDialect
,然后配置该属性为实现类的全限定名称即可使用自定义的实现方法。offsetAsPageNum
:默认值为false
,该参数对使用RowBounds
作为分页参数时有效。 当该参数设置为true
时,会将RowBounds
中的offset
参数当成pageNum
使用,可以用页码和页面大小两个参数进行分页。rowBoundsWithCount
:默认值为false
,该参数对使用RowBounds
作为分页参数时有效。 当该参数设置为true
时,使用RowBounds
分页会进行 count 查询。pageSizeZero
:默认值为false
,当该参数设置为true
时,如果pageSize=0
或者RowBounds.limit = 0
就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是Page
类型)。reasonable
:分页合理化参数,默认值为false
。当该参数设置为true
时,pageNum<=0
时会查询第一页,pageNum>pages
(超过总数时),会查询最后一页。默认false
时,直接根据参数进行查询。params
:为了支持startPage(Object params)
方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置pageNum,pageSize,count,pageSizeZero,reasonable
,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
。supportMethodsArguments
:支持通过 Mapper 接口参数来传递分页参数,默认值false
,分页插件会从查询方法的参数值中,自动根据上面params
配置的字段中取值,查找到合适的值时就会自动分页。 使用方法可以参考测试代码中的com.github.pagehelper.test.basic
包下的ArgumentsMapTest
和ArgumentsObjTest
。autoRuntimeDialect
:默认值为false
。设置为true
时,允许在运行时根据多数据源自动识别对应方言的分页 (不支持自动选择sqlserver2012
,只能使用sqlserver
),用法和注意事项参考下面的场景五。closeConn
:默认值为true
。当使用运行时动态数据源或没有设置helperDialect
属性自动获取数据库类型时,会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认true
关闭,设置为false
后,不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。
重要提示:
当 offsetAsPageNum=false
的时候,由于 PageNum
问题,RowBounds
查询的时候 reasonable
会强制为 false
。使用 PageHelper.startPage
方法不受影响。
如何选择配置这些参数
单独看每个参数的说明可能是一件让人不爽的事情,这里列举一些可能会用到某些参数的情况。
场景一
如果你仍然在用类似ibatis式的命名空间调用方式,你也许会用到rowBoundsWithCount
, 分页插件对RowBounds
支持和 MyBatis 默认的方式是一致,默认情况下不会进行 count 查询,如果你想在分页查询时进行 count 查询, 以及使用更强大的 PageInfo
类,你需要设置该参数为 true
。
注: PageRowBounds
想要查询总数也需要配置该属性为 true
。
场景二
如果你仍然在用类似ibatis式的命名空间调用方式,你觉得 RowBounds
中的两个参数 offset,limit
不如 pageNum,pageSize
容易理解, 你可以使用 offsetAsPageNum
参数,将该参数设置为 true
后,offset
会当成 pageNum
使用,limit
和 pageSize
含义相同。
场景三
如果觉得某个地方使用分页后,你仍然想通过控制参数查询全部的结果,你可以配置 pageSizeZero
为 true
, 配置后,当 pageSize=0
或者 RowBounds.limit = 0
就会查询出全部的结果。
场景四
如果你分页插件使用于类似分页查看列表式的数据,如新闻列表,软件列表, 你希望用户输入的页数不在合法范围(第一页到最后一页之外)时能够正确的响应到正确的结果页面, 那么你可以配置 reasonable
为 true
,这时如果 pageNum<=0
会查询第一页,如果 pageNum>总页数
会查询最后一页。
场景五
如果你在 Spring 中配置了动态数据源,并且连接不同类型的数据库,这时你可以配置 autoRuntimeDialect
为 true
,这样在使用不同数据源时,会使用匹配的分页进行查询。 这种情况下,你还需要特别注意 closeConn
参数,由于获取数据源类型会获取一个数据库连接,所以需要通过这个参数来控制获取连接后,是否关闭该连接。 默认为 true
,有些数据库连接关闭后就没法进行后续的数据库操作。而有些数据库连接不关闭就会很快由于连接数用完而导致数据库无响应。所以在使用该功能时,特别需要注意你使用的数据源是否需要关闭数据库连接。
当不使用动态数据源而只是自动获取 helperDialect
时,数据库连接只会获取一次,所以不需要担心占用的这一个连接是否会导致数据库出错,但是最好也根据数据源的特性选择是否关闭连接。
更多参考: https://pagehelper.github.io/docs/howtouse/
PageHelper的使用
使用的时候,只需在查询list前,调用 startPage 设置分页信息,即可使用分页功能。
public Object getUsers(int pageNum, int pageSize) {PageHelper.startPage(pageNum, pageSize);// 不带分页的查询List<UserEntity> list = userMapper.selectAllWithPage(null);// 可以将结果转换为 Page , 然后获取 count 和其他结果值com.github.pagehelper.Page listWithPage = (com.github.pagehelper.Page) list;System.out.println("listCnt:" + listWithPage.getTotal());return list;}
- 即使用时, 只需提前声明要分页的信息, 得到的结果就是有分页信息的了.
- 如果不想进行count, 只要查分页数据, 则调用: PageHelper.startPage(pageNum, pageSize, false); 即可, 避免了不必要的count消耗.
PageHelper实现原理1: interceptor
mybatis 有个插件机制,可以支持外部应用进行任意扩展。它在启动的时候会将 interceptor 添加到mybatis的上下文中。然后在进行查询时再触发实例化动作.
Mybatis拦截器可以对下面4种对象进行拦截:
拦截对象 | 拦截方法 | 方法作用 |
Executor | update | 对应insert,delete,update语句 |
query | 对应select语句 | |
flushStatements | 刷新Statement | |
commit | 提交事务 | |
rollback | 回滚事务 | |
getTransaction | 获取事务 | |
close | 关闭事务 | |
isClosed | 判断事务是否关闭 | |
StatementHandler | prepare |
预编译SQL |
parameterize | 设置参数 | |
batch |
批处理 | |
update | 对应insert,delete,update语句 | |
query | 对应select语句 | |
ParameterHandler | getParameterObject | 获取参数 |
setParameters | 设置参数 | |
ResultSetHandler | handleResultSets | 处理结果集 |
handleOutputParameters | 处理存储过程出参 |
- Executor:mybatis的内部执行器,作为调度核心负责调用StatementHandler操作数据库,并把结果集通过ResultSetHandler进行自动映射
- StatementHandler: 封装了JDBC Statement操作,是sql语法的构建器,负责和数据库进行交互执行sql语句
- ParameterHandler:作为处理sql参数设置的对象,主要实现读取参数和对PreparedStatement的参数进行赋值
- ResultSetHandler:处理Statement执行完成后返回结果集的接口对象,mybatis通过它把ResultSet集合映射成实体对象
估计你也猜到了,PageHelper也是用的mybatis的拦截器进行分页的
1. springboot 中接入interceptor
springboot 中接入pagehelper非常简单, 主要受益于初始化的方式, 它会自动加载配置.
// com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration#addPageInterceptor@PostConstructpublic void addPageInterceptor() {// 初始化 com.github.pagehelper.PageInterceptorPageInterceptor interceptor = new PageInterceptor();Properties properties = new Properties();//先把一般方式配置的属性放进去properties.putAll(pageHelperProperties());//在把特殊配置放进去,由于close-conn 利用上面方式时,属性名就是 close-conn 而不是 closeConn,所以需要额外的一步properties.putAll(this.properties.getProperties());interceptor.setProperties(properties);for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {// 添加inteceptor到 mybatis 中sqlSessionFactory.getConfiguration().addInterceptor(interceptor);}}// org.apache.ibatis.session.Configuration#addInterceptorpublic void addInterceptor(Interceptor interceptor) {interceptorChain.addInterceptor(interceptor);}// org.apache.ibatis.plugin.InterceptorChain#addInterceptorpublic void addInterceptor(Interceptor interceptor) {// 使用 ArrayList 保存intceptorinterceptors.add(interceptor);}
借助springboot的自动配置, 获取mybatis的sqlSessionFactoryList, 依次将 PageHelper接入其中。
2. interceptor的初始化
将 interceptor 添加到mybatis上下文后, 会在每次调用查询时进行拦截请求, 它的初始化也会在这时候触发.
我们知道,Sqlsession对象是通过openSession()方法返回的,
而Executor又是属于SqlSession内部对象,所以让我们跟随openSession方法去看一下Executor对象的初始化过程。
// org.apache.ibatis.session.Configuration#newExecutorpublic Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType = executorType == null ? defaultExecutorType : executorType;executorType = executorType == null ? ExecutorType.SIMPLE : executorType;Executor executor;if (ExecutorType.BATCH == executorType) {executor = new BatchExecutor(this, transaction);} else if (ExecutorType.REUSE == executorType) {executor = new ReuseExecutor(this, transaction);} else {executor = new SimpleExecutor(this, transaction);}if (cacheEnabled) {executor = new CachingExecutor(executor);}// 以interceptorChain包装 executor, 以便inteceptor发挥作用// 可以看到,当初始化完成Executor之后,会调用interceptorChain的pluginAll方法,// pluginAll方法本身非常简单,就是把我们存到list中的插件进行循环,并调用Interceptor对象的plugin方法:executor = (Executor) interceptorChain.pluginAll(executor);return executor;}// org.apache.ibatis.plugin.InterceptorChain#pluginAllpublic Object pluginAll(Object target) {for (Interceptor interceptor : interceptors) {// 使用plugin一层层包装 target, 具体实现为使用代理包装 target// 所以, interceptor 的使用顺序是按照添加的顺序来的, 并不能自行设置target = interceptor.plugin(target);}return target;}// com.github.pagehelper.PageInterceptor#plugin@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}// org.apache.ibatis.plugin.Plugin#wrappublic static Object wrap(Object target, Interceptor interceptor) {// 获取注解中说明的方式列表 @Intercepts -> @Signature, 下面我们看 pageInterceptor的注解Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);Class<?> type = target.getClass();// 过滤需要进行代理的接口, 而非全部代理Class<?>[] interfaces = getAllInterfaces(type, signatureMap);if (interfaces.length > 0) {// 使用jdk方式生成动态代理return Proxy.newProxyInstance(type.getClassLoader(),interfaces,// 使用 Plugin 包装代理实现new Plugin(target, interceptor, signatureMap));}return target;}// pageInterceptor的注解, 即定义要拦截的方法列表
@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),}
)// 过滤代理的接口private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) {Set<Class<?>> interfaces = new HashSet<>();while (type != null) {for (Class<?> c : type.getInterfaces()) {// 只有设置了的接口才会被添加if (signatureMap.containsKey(c)) {interfaces.add(c);}}type = type.getSuperclass();}return interfaces.toArray(new Class<?>[interfaces.size()]);}
这样, interceptor 就和executor绑定了, 后续的查询将会看到interceptor 的作用.
看到InterceptorChain我们是不是可以联想到,MyBatis的插件就是通过责任链模式实现的。
要注意的是MyBatis插件是通过JDK动态代理来实现的,而JDK动态代理的条件就是被代理对象必须要有接口,这一点和Spring中不太一样,Spring中是如果有接口就采用JDK动态代理,没有接口就是用CGLIB动态代理。
3. interceptor的调用过程
在executor被代理后, 会继续执行查询动作, 这时就会被interceptor拦截了.
正因为MyBatis的插件只使用了JDK动态代理,所以我们上面才强调了一定要实现Interceptor接口。
而代理之后会执行Plugin的invoke方法,我们最后再来看看invoke方法:
// org.apache.ibatis.plugin.Plugin#invoke@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {try {Set<Method> methods = signatureMap.get(method.getDeclaringClass());if (methods != null && methods.contains(method)) {// 匹配的方法会被拦截, 即 query 方法return interceptor.intercept(new Invocation(target, method, args));}return method.invoke(target, args);} catch (Exception e) {throw ExceptionUtil.unwrapThrowable(e);}}// PageHelper正式起作用的入口// com.github.pagehelper.PageInterceptor#intercept@Overridepublic Object intercept(Invocation invocation) throws Throwable {try {Object[] args = invocation.getArgs();MappedStatement ms = (MappedStatement) args[0];Object parameter = args[1];RowBounds rowBounds = (RowBounds) args[2];ResultHandler resultHandler = (ResultHandler) args[3];Executor executor = (Executor) invocation.getTarget();CacheKey cacheKey;BoundSql boundSql;//由于逻辑关系,只会进入一次if (args.length == 4) {//4 个参数时boundSql = ms.getBoundSql(parameter);cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);} else {//6 个参数时cacheKey = (CacheKey) args[4];boundSql = (BoundSql) args[5];}checkDialectExists();
List resultList;//调用方法判断是否需要进行分页,如果不需要,直接返回结果if (!dialect.skip(ms, parameter, rowBounds)) {//判断是否需要进行 count 查询if (dialect.beforeCount(ms, parameter, rowBounds)) {//查询总数Long count = count(executor, ms, parameter, rowBounds, resultHandler, boundSql);//处理查询总数,返回 true 时继续分页查询,false 时直接返回if (!dialect.afterCount(count, parameter, rowBounds)) {//当查询总数为 0 时,直接返回空的结果return dialect.afterPage(new ArrayList(), parameter, rowBounds);}}resultList = ExecutorUtil.pageQuery(dialect, executor,ms, parameter, rowBounds, resultHandler, boundSql, cacheKey);} else {//rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);}return dialect.afterPage(resultList, parameter, rowBounds);} finally {if(dialect != null){dialect.afterAll();}}}
以上就是 PageHelper的大体执行框架了:
1. 先解析各位置参数;
2. 初始化 PageHelper实例, 即 dialect;
3. 调用方法判断是否需要进行分页,如果不需要,直接返回结果;
4. 判断是否要进行count, 如果需要则实现一次count, ;
5. 查询分页结果;
6. 封装带分页的结果返回;
下面我们就每个细节依次看看实现吧.
4. 是否跳过分页判定
首先会进行是否需要跳过分页逻辑,如果跳过, 则直接执行mybatis的核心逻辑继续查询.
而是否要跳过分页, 则是通过直接获取page分页参数来决定的,没有分页参数设置,则跳过, 否则执行分页查询. 这算是分页的一个入口判定呢。
/*** 跳过 count 和 分页查询** @param ms MappedStatement* @param parameterObject 方法参数* @param rowBounds 分页参数* @return true 跳过,返回默认查询结果,false 执行分页查询*/// com.github.pagehelper.PageHelper#skip@Overridepublic boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {if (ms.getId().endsWith(MSUtils.COUNT)) {throw new RuntimeException("在系统中发现了多个分页插件,请检查系统配置!");}// 如果 page 返回null, 则不需要进行分页, 即是否调用 PageHelper.start(pageNo, pageSize) 方法Page page = pageParams.getPage(parameterObject, rowBounds);if (page == null) {return true;} else {//设置默认的 count 列if (StringUtil.isEmpty(page.getCountColumn())) {page.setCountColumn(pageParams.getCountColumn());}autoDialect.initDelegateDialect(ms);return false;}}// com.github.pagehelper.page.PageAutoDialect#initDelegateDialect//多数据动态获取时,每次需要初始化public void initDelegateDialect(MappedStatement ms) {if (delegate == null) {if (autoDialect) {// 比如 MySqlDialectthis.delegate = getDialect(ms);} else {dialectThreadLocal.set(getDialect(ms));}}}
/*** 获取分页参数*/// com.github.pagehelper.page.PageParams#getPagepublic Page getPage(Object parameterObject, RowBounds rowBounds) {Page page = PageHelper.getLocalPage();if (page == null) {if (rowBounds != RowBounds.DEFAULT) {if (offsetAsPageNum) {page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);} else {page = new Page(new int[]{rowBounds.getOffset(), rowBounds.getLimit()}, rowBoundsWithCount);//offsetAsPageNum=false的时候,由于PageNum问题,不能使用reasonable,这里会强制为falsepage.setReasonable(false);}if(rowBounds instanceof PageRowBounds){PageRowBounds pageRowBounds = (PageRowBounds)rowBounds;page.setCount(pageRowBounds.getCount() == null || pageRowBounds.getCount());}} else if(parameterObject instanceof IPage || supportMethodsArguments){try {page = PageObjectUtil.getPageFromObject(parameterObject, false);} catch (Exception e) {return null;}}if(page == null){return null;}PageHelper.setLocalPage(page);}//分页合理化if (page.getReasonable() == null) {page.setReasonable(reasonable);}//当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果if (page.getPageSizeZero() == null) {page.setPageSizeZero(pageSizeZero);}return page;}
才上判定决定了后续的分页效果,主要是利用 ThreadLocal 来保存分页信息,从而与用户代码产生关联。
5. PageHelper的 count 操作
判断是否是否需要count, 这些判定都会以 PageHelper作为门面类进行接入, 而特殊地方则由具体方言实现.
// com.github.pagehelper.PageHelper#beforeCount@Overridepublic boolean beforeCount(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {return autoDialect.getDelegate().beforeCount(ms, parameterObject, rowBounds);}
// com.github.pagehelper.dialect.AbstractHelperDialect#beforeCount@Overridepublic boolean beforeCount(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {// 获取page参数信息, 该参数设置在 ThreadLocal 中Page page = getLocalPage();return !page.isOrderByOnly() && page.isCount();}// 如果需要进行count, 则需要自行组装count逻辑进行查询.// com.github.pagehelper.PageInterceptor#countprivate Long count(Executor executor, MappedStatement ms, Object parameter,RowBounds rowBounds, ResultHandler resultHandler,BoundSql boundSql) throws SQLException {// 在原有list 查询后添加 _COUNT 代表count查询idString countMsId = ms.getId() + countSuffix;Long count;//先判断是否存在手写的 count 查询MappedStatement countMs = ExecutorUtil.getExistedMappedStatement(ms.getConfiguration(), countMsId);if (countMs != null) {count = ExecutorUtil.executeManualCount(executor, countMs, parameter, boundSql, resultHandler);} else {countMs = msCountMap.get(countMsId);//自动创建if (countMs == null) {//根据当前的 ms 创建一个返回值为 Long 类型的 mscountMs = MSUtils.newCountMappedStatement(ms, countMsId);msCountMap.put(countMsId, countMs);}count = ExecutorUtil.executeAutoCount(dialect, executor, countMs, parameter, boundSql, rowBounds, resultHandler);}return count;}// 创建count ms// com.github.pagehelper.util.MSUtils#newCountMappedStatement(org.apache.ibatis.mapping.MappedStatement, java.lang.String)public static MappedStatement newCountMappedStatement(MappedStatement ms, String newMsId) {// 直接基于原有 sql 构建新的 MappedStatementMappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), newMsId, ms.getSqlSource(), ms.getSqlCommandType());builder.resource(ms.getResource());// 注意此处并未使用到用户设置的分页参数 builder.fetchSize(ms.getFetchSize());builder.statementType(ms.getStatementType());builder.keyGenerator(ms.getKeyGenerator());if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {StringBuilder keyProperties = new StringBuilder();for (String keyProperty : ms.getKeyProperties()) {keyProperties.append(keyProperty).append(",");}keyProperties.delete(keyProperties.length() - 1, keyProperties.length());builder.keyProperty(keyProperties.toString());}builder.timeout(ms.getTimeout());builder.parameterMap(ms.getParameterMap());//count查询返回值intList<ResultMap> resultMaps = new ArrayList<ResultMap>();ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class, EMPTY_RESULTMAPPING).build();resultMaps.add(resultMap);builder.resultMaps(resultMaps);builder.resultSetType(ms.getResultSetType());builder.cache(ms.getCache());builder.flushCacheRequired(ms.isFlushCacheRequired());builder.useCache(ms.isUseCache());
return builder.build();}
/*** 执行自动生成的 count 查询*/// com.github.pagehelper.util.ExecutorUtil#executeAutoCountpublic static Long executeAutoCount(Dialect dialect, Executor executor, MappedStatement countMs,Object parameter, BoundSql boundSql,RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {Map<String, Object> additionalParameters = getAdditionalParameter(boundSql);//创建 count 查询的缓存 keyCacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);//调用方言获取 count sqlString 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;}// com.github.pagehelper.PageHelper#getCountSql@Overridepublic String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {// 委托给各方言实现 sql 组装return autoDialect.getDelegate().getCountSql(ms, boundSql, parameterObject, rowBounds, countKey);}
// com.github.pagehelper.dialect.AbstractHelperDialect#getCountSql@Overridepublic String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {Page<Object> page = getLocalPage();String countColumn = page.getCountColumn();if (StringUtil.isNotEmpty(countColumn)) {return countSqlParser.getSmartCountSql(boundSql.getSql(), countColumn);}return countSqlParser.getSmartCountSql(boundSql.getSql());}
/*** 获取智能的countSql** @param sql* @param name 列名,默认 0* @return*/// com.github.pagehelper.parser.CountSqlParser#getSmartCountSql(java.lang.String, java.lang.String)public String getSmartCountSql(String sql, String name) {//解析SQLStatement stmt = null;//特殊sql不需要去掉order by时,使用注释前缀if(sql.indexOf(KEEP_ORDERBY) >= 0){return getSimpleCountSql(sql, name);}try {stmt = CCJSqlParserUtil.parse(sql);} catch (Throwable e) {//无法解析的用一般方法返回count语句return getSimpleCountSql(sql, name);}Select select = (Select) stmt;SelectBody selectBody = select.getSelectBody();try {//处理body-去order byprocessSelectBody(selectBody);} catch (Exception e) {//当 sql 包含 group by 时,不去除 order byreturn getSimpleCountSql(sql, name);}//处理with-去order byprocessWithItemsList(select.getWithItemsList());//处理为count查询sqlToCount(select, name);String result = select.toString();return result;}/*** 将sql转换为count查询** @param select*/// com.github.pagehelper.parser.CountSqlParser#sqlToCountpublic void sqlToCount(Select select, String name) {SelectBody selectBody = select.getSelectBody();// 是否能简化count查询List<SelectItem> COUNT_ITEM = new ArrayList<SelectItem>();// 如 select * from user 将会被转化为 select count(0) from userCOUNT_ITEM.add(new SelectExpressionItem(new Column("count(" + name +")")));if (selectBody instanceof PlainSelect && isSimpleCount((PlainSelect) selectBody)) {// 简单sql直接转换select字段为 count(0) 即可, 而这个sql是否支持这种方式则得仔细验证((PlainSelect) selectBody).setSelectItems(COUNT_ITEM);} else {// 如果对于复杂的sql查询, 则只能在现有sql外围加一个 select count(0) from (xxxxx) as table_countPlainSelect plainSelect = new PlainSelect();SubSelect subSelect = new SubSelect();subSelect.setSelectBody(selectBody);subSelect.setAlias(TABLE_ALIAS);// 将原sql作为临时表放入 plainSelect 中plainSelect.setFromItem(subSelect);plainSelect.setSelectItems(COUNT_ITEM);// 替换原有 selectselect.setSelectBody(plainSelect);}}/*** 是否可以用简单的count查询方式*/// net.sf.jsqlparser.statement.select.PlainSelectpublic boolean isSimpleCount(PlainSelect select) {//包含group by的时候不可以if (select.getGroupBy() != null) {return false;}//包含distinct的时候不可以if (select.getDistinct() != null) {return false;}for (SelectItem item : select.getSelectItems()) {//select列中包含参数的时候不可以,否则会引起参数个数错误if (item.toString().contains("?")) {return false;}//如果查询列中包含函数,也不可以,函数可能会聚合列if (item instanceof SelectExpressionItem) {Expression expression = ((SelectExpressionItem) item).getExpression();if (expression instanceof Function) {String name = ((Function) expression).getName();if (name != null) {String NAME = name.toUpperCase();if(skipFunctions.contains(NAME)){//go on} else if(falseFunctions.contains(NAME)){return false;} else {for (String aggregateFunction : AGGREGATE_FUNCTIONS) {if(NAME.startsWith(aggregateFunction)){falseFunctions.add(NAME);return false;}}skipFunctions.add(NAME);}}}}}return true;}
大体上讲就是分析sql, 如果是简单查询, 则直接将字段内容转换为 count(0) 即可, 这和我们普通认为的在select外部简单包一层还不太一样哦.
但是对于复杂查询咱们还是只能使用外包一层的实现方式了. 当然了,以上实现是针对mysql的,其他语言可能会有不一样的实现.
6. select list 的改装
在执行完count后, 分页的功能完成了一半, 我们可以给到用户这个计数值
另外,我们可以根据该值得到后续分页还有多少数据, 如果没有自然不用再查了, 如果有则组装limit语句.
ExecutorUtil.pageQuery()来处理分页,但是怎么处理的呢?
// com.github.pagehelper.dialect.AbstractHelperDialect#afterCount@Overridepublic boolean afterCount(long count, Object parameterObject, RowBounds rowBounds) {Page page = getLocalPage();page.setTotal(count);if (rowBounds instanceof PageRowBounds) {((PageRowBounds) rowBounds).setTotal(count);}//pageSize < 0 的时候,不执行分页查询//pageSize = 0 的时候,还需要执行后续查询,但是不会分页if (page.getPageSize() < 0) {return false;}// 还没到最后一页, 则需要进行分页查询return count > ((page.getPageNum() - 1) * page.getPageSize());}/*** 分页查询*/public static <E> List<E> pageQuery(Dialect dialect, Executor executor, MappedStatement ms, Object parameter,RowBounds rowBounds, ResultHandler resultHandler,BoundSql boundSql, CacheKey cacheKey) throws SQLException {//判断是否需要进行分页查询if (dialect.beforePage(ms, parameter, rowBounds)) {//生成分页的缓存 keyCacheKey pageKey = cacheKey;//处理参数对象, 将会加入 pageStart, pageSize 等参数parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);//调用方言获取分页 sqlString pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter);
Map<String, Object> additionalParameters = getAdditionalParameter(boundSql);//设置动态参数for (String key : additionalParameters.keySet()) {pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));}//执行分页查询return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);} else {//不执行分页的情况下,也不执行内存分页return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);}}// com.github.pagehelper.dialect.AbstractHelperDialect#processParameterObject@Overridepublic Object processParameterObject(MappedStatement ms, Object parameterObject, BoundSql boundSql, CacheKey pageKey) {//处理参数Page page = getLocalPage();//如果只是 order by 就不必处理参数if (page.isOrderByOnly()) {return parameterObject;}Map<String, Object> paramMap = null;if (parameterObject == null) {paramMap = new HashMap<String, Object>();} else if (parameterObject instanceof Map) {//解决不可变Map的情况paramMap = new HashMap<String, Object>();paramMap.putAll((Map) parameterObject);} else {paramMap = new HashMap<String, Object>();//动态sql时的判断条件不会出现在ParameterMapping中,但是必须有,所以这里需要收集所有的getter属性//TypeHandlerRegistry可以直接处理的会作为一个直接使用的对象进行处理boolean hasTypeHandler = ms.getConfiguration().getTypeHandlerRegistry().hasTypeHandler(parameterObject.getClass());MetaObject metaObject = MetaObjectUtil.forObject(parameterObject);//需要针对注解形式的MyProviderSqlSource保存原值if (!hasTypeHandler) {for (String name : metaObject.getGetterNames()) {paramMap.put(name, metaObject.getValue(name));}}//下面这段方法,主要解决一个常见类型的参数时的问题if (boundSql.getParameterMappings() != null && boundSql.getParameterMappings().size() > 0) {for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {String name = parameterMapping.getProperty();if (!name.equals(PAGEPARAMETER_FIRST)&& !name.equals(PAGEPARAMETER_SECOND)&& paramMap.get(name) == null) {if (hasTypeHandler|| parameterMapping.getJavaType().equals(parameterObject.getClass())) {paramMap.put(name, parameterObject);break;}}}}}return processPageParameter(ms, paramMap, page, boundSql, pageKey);}
// 加入 page 参数// com.github.pagehelper.dialect.helper.MySqlDialect#processPageParameter@Overridepublic Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {// First_PageHelper, Second_PageHelperparamMap.put(PAGEPARAMETER_FIRST, page.getStartRow());paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize());//处理pageKeypageKey.update(page.getStartRow());pageKey.update(page.getPageSize());//处理参数配置if (boundSql.getParameterMappings() != null) {List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings());if (page.getStartRow() == 0) {newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build());} else {newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class).build());newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build());}MetaObject metaObject = MetaObjectUtil.forObject(boundSql);metaObject.setValue("parameterMappings", newParameterMappings);}return paramMap;}// 组装分页sql// com.github.pagehelper.dialect.AbstractHelperDialect#getPageSql@Overridepublic String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey) {String sql = boundSql.getSql();Page page = getLocalPage();//支持 order byString orderBy = page.getOrderBy();if (StringUtil.isNotEmpty(orderBy)) {pageKey.update(orderBy);sql = OrderByParser.converToOrderBySql(sql, orderBy);}if (page.isOrderByOnly()) {return sql;}return getPageSql(sql, page, pageKey);}// com.github.pagehelper.dialect.helper.MySqlDialect#getPageSql@Overridepublic String getPageSql(String sql, Page page, CacheKey pageKey) {StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);sqlBuilder.append(sql);// 分页sql拼接, limit xxxif (page.getStartRow() == 0) {sqlBuilder.append(" LIMIT ? ");} else {sqlBuilder.append(" LIMIT ?, ? ");}return sqlBuilder.toString();}
PageHelper首先将前端传递的参数保存到page这个对象中,接着将page的副本存放入ThreadLoacl中,这样可以保证分页的时候,参数互不影响,接着利用了mybatis提供的拦截器,取得ThreadLocal的值,重新拼装分页SQL,完成分页
经过上面的sql重组之后,就可以得到具体分页的list数据了, 返回的也是list数据.
那么, 用户如何获取其他的分页信息呢? 比如count值去了哪里?
实际上, 在list 返回之后, 还有一个 afterPage 的动作要做, 而它的作用就是封装list 为带page信息的list.
// com.github.pagehelper.PageHelper#afterPage@Overridepublic Object afterPage(List pageList, Object parameterObject, RowBounds rowBounds) {//这个方法即使不分页也会被执行,所以要判断 nullAbstractHelperDialect delegate = autoDialect.getDelegate();if (delegate != null) {return delegate.afterPage(pageList, parameterObject, rowBounds);}return pageList;}// com.github.pagehelper.dialect.AbstractHelperDialect#afterPage@Overridepublic Object afterPage(List pageList, Object parameterObject, RowBounds rowBounds) {// 取出本线程的page变量, 放入listPage page = getLocalPage();if (page == null) {return pageList;}page.addAll(pageList);// count 值临时变换, 用于应对没有进行count的场景, 使外部表现一致if (!page.isCount()) {page.setTotal(-1);} else if ((page.getPageSizeZero() != null && page.getPageSizeZero()) && page.getPageSize() == 0) {page.setTotal(pageList.size());} else if(page.isOrderByOnly()){page.setTotal(pageList.size());}return page;}
至此, 一个完整的分页功能就完成了. 核心逻辑最开始也已看到, 就是判断是否需要分页, 是否需要count, 然后添加分页sql取数的这么个过程. 其本身并无太多银弹, 但却是能让我们节省不少时间. 另外就是, 在应对数据库可能发生切换的场景, 我们也可以无需更改此部分代码, 从而减轻了历史负担. 用用又何乐而不为呢?
思考
ThreadLocal
在PageHelper
中的应用
/**
* 分页调用的最终方法
**/
public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count,Boolean reasonable, Boolean pageSizeZero) {Page<E> page = new Page<E>(pageNum, pageSize, count);page.setReasonable(reasonable);page.setPageSizeZero(pageSizeZero);//当已经执行过orderBy的时候Page<E> oldPage = getLocalPage();if (oldPage != null && oldPage.isOrderByOnly()) {page.setOrderBy(oldPage.getOrderBy());}setLocalPage(page);return page;
}//里边最重要的就是Page<E> oldPage = getLocalPage();和setLocalPage(page);方法,他俩是看当前线程中的
//ThreadLocal.ThreadLocalMap中是否存在该page对象,若存在直接取出,若不存在则设置一个,我们以第一个为例继续深入protected static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>();
/*** 获取 Page 参数* @return*/
public static <T> Page<T> getLocalPage() {return LOCAL_PAGE.get();
}public T get() {//获取当前线程Thread t = Thread.currentThread();//获取当前线程中的ThreadLocalMapThreadLocalMap map = getMap(t);//ThreadLocal.ThreadLocalMap threadLocals = null;if (map != null) {//getEntry(ThreadLocal<?> key)源码在下边ThreadLocalMap.Entry e = map.getEntry(this);if (e != null) {@SuppressWarnings("unchecked")T result = (T)e.value;return result;}}return setInitialValue();//=> t.threadLocals = new ThreadLocalMap(this, firstValue);
}private Entry getEntry(ThreadLocal<?> key) {//通过hashCode与length位运算确定出一个索引值i,这个i就是被存储在table数组中的位置int i = key.threadLocalHashCode & (table.length - 1);Entry e = table[i];if (e != null && e.get() == key)return e;elsereturn getEntryAfterMiss(key, i, e);
}static class Entry extends WeakReference<ThreadLocal<?>> {/** The value associated with this ThreadLocal. */Object value;Entry(ThreadLocal<?> k, Object v) {super(k);value = v;}
}
我们发现在Thread中维护着类型为ThreadLocal.ThreadLocalMap的一个参数threadLocals,可以把它看作是一个特殊的map,它的key是threadLocal的threadLocalHashCode,value是我们设置的page信息,
其实它底下维护了一个大小为16的环形的table数组,它的负载因子为2/3,我们的数据就存在这个table中的Entry对象中。
PageHelper通过ThreadLocal来共享分页信息,那么它是何时进行清除的呢?
// 实际上在每次运行完成pageInterceptor之后,都会在finnaly中进行一次清理工作try {// do page things} finally {// afterAll 即为清理任务if(dialect != null){dialect.afterAll();}}// com.github.pagehelper.PageHelper#afterAll@Overridepublic void afterAll() {//这个方法即使不分页也会被执行,所以要判断 nullAbstractHelperDialect delegate = autoDialect.getDelegate();if (delegate != null) {// 默认为空delegate.afterAll();// delegate 移除,这里也是使用 ThreadLocal 实现,直接remove即可autoDialect.clearDelegate();}// 清理 page对象,下次不再有该设置,也就是说 page 设置是一次性的clearPage();}// com.github.pagehelper.page.PageMethod#clearPagepublic static void clearPage() {LOCAL_PAGE.remove();}// 下次再进行分页时,重新调用 PageHelper.startPage(x, x, x); 即可
通过这,也就明白了为什么PageHelper只对startPage后的第一条select语句有效?
因为, 在finally内把ThreadLocal中的分页数据给清除掉了,所以只要执行一次查询语句就会清除分页信息,故而后面的select语句自然就无效了。
什么时候会导致不安全的分页?
PageHelper
方法使用了静态的 ThreadLocal
参数,分页参数和线程是绑定的。
只要你可以保证在 PageHelper
方法调用后紧跟 MyBatis 查询方法,这就是安全的。因为 PageHelper
在 finally
代码段中自动清除了 ThreadLocal
存储的对象。
如果代码在进入 Executor
前发生异常,就会导致线程不可用,这属于人为的 Bug(例如接口方法和 XML 中的不匹配,导致找不到 MappedStatement
时), 这种情况由于线程不可用,也不会导致 ThreadLocal
参数被错误的使用。
但是如果你写出下面这样的代码,就是不安全的用法:
PageHelper.startPage(1, 10);
List<Country> list;
if(param1 != null){list = countryMapper.selectIf(param1);
} else {list = new ArrayList<Country>();
}
这种情况下由于 param1 存在 null 的情况,就会导致 PageHelper 生产了一个分页参数,但是没有被消费,这个参数就会一直保留在这个线程上。当这个线程再次被使用时,就可能导致不该分页的方法去消费这个分页参数,这就产生了莫名其妙的分页。
上面这个代码,应该写成下面这个样子:
List<Country> list;
if(param1 != null){PageHelper.startPage(1, 10);list = countryMapper.selectIf(param1);
} else {list = new ArrayList<Country>();
}
这种写法就能保证安全。
如果你对此不放心,你可以手动清理 ThreadLocal
存储的分页参数,可以像下面这样使用:
List<Country> list;
if(param1 != null){PageHelper.startPage(1, 10);try{list = countryMapper.selectAll();} finally {PageHelper.clearPage();}
} else {list = new ArrayList<Country>();
}
这么写很不好看,而且没有必要。
参考链接:
https://pagehelper.github.io/docs/howtouse/
MyBatis 分页插件 PageHelper:是如何拦截SQL进行分页相关推荐
- MyBatis分页插件PageHelper使用练习
转载自:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/HowToUse.markdown 1.环境准备: 分页插件p ...
- MyBatis学习总结(17)——Mybatis分页插件PageHelper
2019独角兽企业重金招聘Python工程师标准>>> 如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊 ...
- 【MyBatis】MyBatis分页插件PageHelper的使用
转载自 https://www.cnblogs.com/shanheyongmu/p/5864047.html 好多天没写博客了,因为最近在实习,大部分时间在熟悉实习相关的东西,也没有怎么学习新的东西 ...
- Spring Boot系列教程八: Mybatis使用分页插件PageHelper
一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...
- Springboot集成mybatis通用Mapper与分页插件PageHelper
Springboot集成mybatis通用Mapper与分页插件PageHelper 插件介绍 通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 ...
- Java 逻辑分页 和 物理分页(mybatis的分页插件PageHelper)
目录 1 逻辑分页和物理分页的区别 2 项目框架展示 2.1 相关技术 2.2 相关依赖和配置 2.2.1 pom依赖 2.2.2 yml配置 2.3 实体类 3 逻辑分页 3.1 Sevice层 3 ...
- 解决使用mybatis分页插件PageHelper的一个报错问题
解决使用mybatis分页插件PageHelper的一个报错问题 参考文章: (1)解决使用mybatis分页插件PageHelper的一个报错问题 (2)https://www.cnblogs.co ...
- SpringBoot集成MyBatis的分页插件PageHelper
[写在前面] 项目的后台管理系统需要展示所有资源信息,select * 虽然方便但数据量过于庞大会严重降低查找效率,页面加载慢,用户体验差.分页自然是必要选择,但原生的方法过于繁杂.MyBatis的分 ...
- (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示
http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...
最新文章
- 李开复:发明期已过,AI科学家创业需谨慎(附演讲视频)
- .mdb_MDB!= JMS,反之亦然
- 在电脑上安装python的步骤-python中pygame安装过程(超级详细)
- 生成Base58格式的UUID(Hibernate Base64格式的UUID续)
- Boost:bind绑定访客的测试程序
- mpandroidchart y轴从0开始_从零开始学Pytorch(十七)之目标检测基础
- php中echo有哪些,php中echo和print有什么区别
- 数据库减压--php+mysql+memcached模拟nosql
- C#中通过Lambda表达式为委托传入更多的参数
- java field setfont_java高手请进!
- 方法论+本土特色,这个BPM平台不简单
- 如何通过 User-Agent 识别百度蜘蛛
- DiskPart使用方法(ZT)
- GSM和GPRS区别
- 下三角形行列式证明推导
- ref改变样式 vue_vue.js对样式的具体操作详解
- 2023湖南省中职网络安全任务书
- BOM:窗口位置、页面视口大小、window.open
- [WinError 10060]错误
- 《乱音盒子》之《闪耀的星》