pagehelper不用说,只要是用过的就会爱上他!
Maven依赖:
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
controller调用,我这里用的thymeleaf模板
@GetMapping(value = "")
public String index(@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "limit", defaultValue = "8") int limit, HttpServletRequest request) {
PageHelper.startPage(page, limit);
List<Article> list = modifyService.getArticleList();
PageInfo<Article> pageInfo = new PageInfo<>(list);
request.setAttribute("pageInfo", pageInfo);
return "admin/article_list";
}
那么来到了pagehelper源码:
根据源码,这里就分析一下:
//跳过 count 和 分页查询,当项目存在多个分页插件、设置默认的 count 列为空时
boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds);
//执行分页前,返回 true 会进行 count 查询,false 会继续下面的 beforePage 判断
boolean beforeCount(MappedStatement ms, Object parameterObject, RowBounds rowBounds);
//生成 count 查询 sql
String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey);
// 执行完 count 查询后
// count:查询结果总数
// parameterObject:接口参数
// rowBounds:分页参数
// 返回值为true时继续分页查询,false 直接返回
boolean afterCount(long count, Object parameterObject, RowBounds rowBounds);
//处理查询参数对象
ct processParameterObject(MappedStatement ms, Object parameterObject, BoundSql boundSql, CacheKey pageKey);
// 执行分页前,返回 true 会进行分页查询,false 会返回默认查询结果
boolean beforePage(MappedStatement ms, Object parameterObject, RowBounds rowBounds);
// 生成分页查询 sql,这里就不乖=怪为啥我们总是能看见一系列的SQL语句输出:limit ?,?,但是我们并没有写这样的SQL语句吧
String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey);
//分页查询后,处理分页结果,拦截器中直接 return 该方法的返回值
Object afterPage(List pageList, Object parameterObject, RowBounds rowBounds);
//完成所有任务后
void afterAll();
//给pagehelper设置参数
//拿到properties里面的插件属性
void setProperties(Properties properties);