概述
Mybatis-Plus 提供了分页的功能。
PageHelper内部原理是将传入的页码和条数赋值给了Page对象,保存到了一个本地线程ThreadLoacl中,然后会进入Mybatis的拦截器中。
在拦截器中获取本地线程中保存的分页的参数。最后再将这写分页参数和原本的sql以及内部定义好的sql进行拼接完成sql的分页处理。
中间会进行判断该sql 的类型是查询还是修改操作。如果是查询才会进入分页的逻辑并判断封装好的Page对象是否为null,为null则不分页,否则分页。
使用分页
Maven 依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
配置文件
# 分页配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
服务类
@Service
public class DepartmentsServiceImpl extends ServiceImpl<DepartmentsMapper, Departments> implements DepartmentsService {
@Autowired
private DepartmentsMapper departmentsMapper;
@Override
public List<Departments> getList(int pageNum, int pageSize) throws Exception {
//使用分页插件
PageHelper.startPage(pageNum, pageSize);
// 获取List
List<Departments> departmentlist = departmentsMapper.selectList(new QueryWrapper());
return departmentlist;
}
}
测试
http://localhost:8082/getDeptEmpList?pageNum=1&pageSize=3
参考
https://mp.baomidou.com/guide/
https://pagehelper.github.io/docs/howtouse/