PageHelper是一款开源的Mybatis第三方物理分页插件。支持MySQL,Oracle,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等数据库。
1. 引入依赖
pom.xml
1 | <!--pagehelper插件--> |
2. 配置文件中配置
application.properties
1 | ###############PageHelper ################## |
3. Dao层
1 |
|
4. mapper.xml
1 | <select id="getAllUsers" resultMap="BaseResultMap"> |
5. Service层
在这里查询的时候,设置分页
1 |
|
6. Controller层
1 |
|
7. 数据库
创建了一个名为springboot_pagehelper
的数据库,插入了20条记录
1 | -- 创建数据库 |
7. 启动,请求
在postman地址栏中输入
localhost:8089/all?page=4&size=3
,请求方式为Get。请求第4页的数据,一共三条记录(每页三条记录)。发起请求后,返回
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46{
"total": 20, // 一共有20条记录
"list": [
{
"userId": 11,
"username": "obiwang",
"password": "3434324bnbmn"
},
{
"userId": 10,
"username": "r2d2",
"password": "dhjshs"
},
{
"userId": 9,
"username": "c3po",
"password": "Rds$#"
}
],
"pageNum": 4,
"pageSize": 3,
"size": 3,
"startRow": 10,
"endRow": 12,
"pages": 7, // 一共7页
"prePage": 3,
"nextPage": 5,
"isFirstPage": false,
"isLastPage": false,
"hasPreviousPage": true,
"hasNextPage": true,
"navigatePages": 8,
"navigatepageNums": [
1,
2,
3,
4,
5,
6,
7
],
"navigateFirstPage": 1,
"navigateLastPage": 7,
"firstPage": 1,
"lastPage": 7
}
参考:
- https://blog.csdn.net/qq_37456162/article/details/86775263?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param
- https://juejin.im/post/6844903596996329485
- https://blog.csdn.net/qq_36952874/article/details/80986945?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task