参考
- PageHelper官网 https://pagehelper.github.io/
1. PageHelper使用流程
1.1. 依赖
1 | <dependencies> |
1.2. 配置拦截器插件
在MyBatis配置文件中配置拦截器插件1
2
3
4
5<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
1.3. Mapper和映射
1 | public interface AuthorMapper { |
1 | <select id="selectAll" resultMap="demo.mybatis.entity.Author"> |
1.4. 测试分页查询
1 |
|
2. PageHelper 一次性原则
注意,分页查询只对紧跟的首次查询有效,之后自动失效
1 |
|
之后的查询如果还想分页,则必须再调用PageHelper.startPage()1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void test() {
int page = 2;
int pageSize = 3;
PageHelper.startPage(page, pageSize);
AuthorMapper mapper = sqlSession.getMapper(AuthorMapper.class);
List<Author> list = mapper.selectAll();
list.forEach(System.out::println);
PageHelper.startPage(page, pageSize); // 再启用分页
List<Author> list2 = mapper.selectAll();
list2.forEach(System.out::println);
}
3. PageInfo的使用
PageInfo是用来获取分页结果的额外信息的
1 |
|