pagehelper 插件
pom.xml
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
PageInterceptor 拦截器 实现Interceptor接口,拦截query方法
@SuppressWarnings({"rawtypes", "unchecked"})
@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}),
}
)
public class PageInterceptor implements Interceptor {
//缓存count查询的ms
protected Cache<String, MappedStatement> msCountMap = null;
private Dialect dialect;
private String default_dialect_class = "com.github.pagehelper.PageHelper";
private Field additionalParametersField;
private String countSuffix = "_COUNT";
在调用查询之前,先调用select count
pagehelper 的使用
1.调用静态方法startPage
2.执行查询方法
3.把查询的结果放在PageInfo 对象中
@Autowired
private DeptMapper deptMapper;
public PageInfo getAllDept(){
PageHelper.startPage(1,10);
List<Dept> deptList = deptMapper.selectAll();
PageInfo pageInfo=new PageInfo(deptList);
List<Dept> list = (List<Dept>)pageInfo.getList();
for (Dept dept : list) {
System.out.println("dept: "+dept);
}
System.out.println(pageInfo.getPageNum());
System.out.println(pageInfo.getPageSize());
System.out.println(pageInfo.getTotal());
return pageInfo;
}