分页PageInterceptor
依赖引入
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>xx.xx.xx</version>
</dependency>
编码
PageHelper.startPage()在查询前执行,设置Page放入ThreadLocal中
/com/github/pagehelper/page/PageMethod.java:147
PageInterceptor拦截请求,自动添加limit & offset
/com/github/pagehelper/util/ExecutorUtil.java:166
/com/github/pagehelper/PageInterceptor.java:140,count计数
private Long count(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { String countMsId = countMsIdGen.genCountMsId(ms, parameter, boundSql, countSuffix); Long count; //先判断是否存在手写的 count 查询 MappedStatement countMs = ExecutorUtil.getExistedMappedStatement(ms.getConfiguration(), countMsId); if (countMs != null) { count = ExecutorUtil.executeManualCount(executor, countMs, parameter, boundSql, resultHandler); } else { if (msCountMap != null) { countMs = msCountMap.get(countMsId); } //自动创建 if (countMs == null) { //根据当前的 ms 创建一个返回值为 Long 类型的 ms countMs = MSUtils.newCountMappedStatement(ms, countMsId); if (msCountMap != null) { msCountMap.put(countMsId, countMs); } } count = ExecutorUtil.executeAutoCount(this.dialect, executor, countMs, parameter, boundSql, rowBounds, resultHandler); } return count; }
@Bean
public PageInterceptor pageInterceptor(){
return new PageInterceptor();
}
@GetMapping("/geta")
private PageInfo<Demo> getA(@RequestParam String str) {
PageHelper.startPage(1, 3);
List<Demo> querydemo = demoBaseMapper.querydemo(str);
PageInfo<Demo> pageInfo = new PageInfo<Demo>(querydemo);
return pageInfo;
}
{
"total": 2,
"list": [
{
"id": 1,
"name": "tom",
"date": "2023-08-28T16:00:00.000+00:00"
}
],
"pageNum": 1,
"pageSize": 3,
"size": 2,
"startRow": 1,
"endRow": 2,
"pages": 1,
"prePage": 0,
"nextPage": 0,
"isFirstPage": true,
"isLastPage": true,
"hasPreviousPage": false,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1
],
"navigateFirstPage": 1,
"navigateLastPage": 1
}