一:什么是PageHelper
PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件
物理分页
支持常见的 12 种数据库。Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等
支持多种分页方式
支持常见的RowBounds(PageRowBounds),PageHelper.startPage 方法调用,Mapper 接口参数调用
二:添加PageHelper依赖
1
2
3
4
5
6 1<dependency>
2 <groupId>com.github.pagehelper</groupId>
3 <artifactId>pagehelper-spring-boot-starter</artifactId>
4 <version>1.2.5</version>
5</dependency>
6
然后鼠标右键选择Maven→Reimport进行依赖下载
三:添加PageHelper配置
在application.properties中添加
1
2
3
4
5
6
7 1logging.level.com.example.demo.dao=DEBUG
2pagehelper.helperDialect=mysql
3pagehelper.reasonable=true
4pagehelper.supportMethodsArguments=true
5pagehelper.params=count=countSql
6pagehelper.page-size-zero=true
7
四:使用方法
UserInfoMapper.xml
1
2
3
4
5
6 1<select id="selectAll" resultMap="BaseResultMap">
2 select
3 <include refid="Base_Column_List"/>
4 from user_info
5</select>
6
UserInfoMapper
1
2 1List<UserInfo> selectAll();
2
UserInfoService
1
2 1PageInfo<UserInfo> selectAll(Integer page,Integer size);复制代码
2
UserInfoServiceImpl
1
2
3
4
5
6
7
8
9
10 1@Override
2public PageInfo<UserInfo> selectAll(Integer page, Integer size) {
3 //开启分页查询,写在查询语句上方
4 //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select)方法会被分页。
5 PageHelper.startPage(page, size);
6 List<UserInfo> userInfoList = userInfoMapper.selectAll();
7 PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList);
8 return pageInfo;
9}
10
UserInfoController
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1@ApiOperation(value = "查询用户", notes = "分页查询用户所有")
2@ApiImplicitParams({
3 @ApiImplicitParam(name = "page", value = "当前页码",
4 dataType = "Integer", paramType = "query"),
5 @ApiImplicitParam(name = "size", value = "每页显示条数",
6 dataType = "Integer", paramType = "query")
7})
8@PostMapping("/selectAll")
9public RetResult<PageInfo<UserInfo>> selectAll(@RequestParam(defaultValue = "0") Integer page,
10 @RequestParam(defaultValue = "0") Integer size) {
11 PageInfo<UserInfo> pageInfo = userInfoService.selectAll(page, size);
12 return RetResponse.makeOKRsp(pageInfo);
13}
14
五:PageHelper中默认PageInfo的成员变量介绍
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 1//当前页
2private int pageNum;
3//每页的数量
4private int pageSize;
5//当前页的数量
6private int size;
7//当前页面第一个元素在数据库中的行号
8private int startRow;
9//当前页面最后一个元素在数据库中的行号
10private int endRow;
11//总记录数
12private long total;
13//总页数
14private int pages;
15//结果集
16private List<T> list;
17//第一页
18private int firstPage;
19//前一页
20private int prePage;
21//是否为第一页
22private boolean isFirstPage;
23//是否为最后一页
24private boolean isLastPage;
25//是否有前一页
26private boolean hasPreviousPage;
27//是否有下一页
28private boolean hasNextPage;
29//导航页码数
30private int navigatePages;
31//所有导航页号
32private int[] navigatepageNums;
33
六:功能测试
地址:http://192.168.1.104:8080/userInfo/selectAll
情况一:不传参数,默认为全查询
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 1{
2 "code": 200,
3 "data": {
4 "endRow": 2,
5 "firstPage": 0,
6 "hasNextPage": false,
7 "hasPreviousPage": false,
8 "isFirstPage": false,
9 "isLastPage": true,
10 "lastPage": 0,
11 "list": [
12 {
13 "id": 1,
14 "userName": "Mr_初晨"
15 },
16 {
17 "id": 2,
18 "userName": "Mr_初晨"
19 }
20 ],
21 "navigateFirstPage": 0,
22 "navigateLastPage": 0,
23 "navigatePages": 8,
24 "navigatepageNums": [],
25 "nextPage": 0,
26 "orderBy": "",
27 "pageNum": 0,
28 "pageSize": 0,
29 "pages": 0,
30 "prePage": 0,
31 "size": 2,
32 "startRow": 1,
33 "total": 2
34 },
35 "msg": "success"
36}
37
*情况二:参数 *page=2&size=1
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 1{
2 "code": 200,
3 "data": {
4 "endRow": 2,
5 "firstPage": 1,
6 "hasNextPage": false,
7 "hasPreviousPage": true,
8 "isFirstPage": false,
9 "isLastPage": true,
10 "lastPage": 2,
11 "list": [
12 {
13 "id": 2,
14 "userName": "Mr_初晨"
15 }
16 ],
17 "navigateFirstPage": 1,
18 "navigateLastPage": 2,
19 "navigatePages": 8,
20 "navigatepageNums": [
21 1,
22 2
23 ],
24 "nextPage": 0,
25 "orderBy": "",
26 "pageNum": 2,
27 "pageSize": 1,
28 "pages": 2,
29 "prePage": 1,
30 "size": 1,
31 "startRow": 2,
32 "total": 2
33 },
34 "msg": "success"
35}
36