PageHelper 分页和排序之间的关系
(1)PageHelper是根据pageSize或者orderByOnly来判断是否要分页的,pageSize大于0 或者orderByOnly 为true才进行分页操作
@Override public boolean beforePage(MappedStatement ms, Object parameterObject, RowBounds rowBounds) { Page page = getLocalPage(); if (page.isOrderByOnly() || page.getPageSize() > 0) { return true; } return false; }
(2)如果不进行分页则PageHelper的order by操作也是不进行的;如下是PageInterceptor类的部分代码:
if (dialect.beforePage(ms, parameter, rowBounds)) { //生成分页的缓存 key CacheKey pageKey = cacheKey; //处理参数对象 parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey); //调用方言获取分页 sql String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey); BoundSql pageBoundSql = new BoundSql(configuration, pageSql, boundSql.getParameterMappings(), parameter); //设置动态参数 for (String key : additionalParameters.keySet()) { pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } //执行分页查询 resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql); } else { //不执行分页的情况下,也不执行内存分页 resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql); }
当不分页时,是直接拿拦截获得的SQL进行查询的,所以没有组装 order by 语句