第08篇:分页插件pagehelper
流程
- 依赖
- 配置
- 使用
1. 依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2. 配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
3. 使用
3.1 返回分页数据
说明:只查询分页数据,而分页相关信息没有
public List<Count> getAllCount(int pageNo, int pageSize) {
PageHelper.startPage(pageNo, pageSize);
return countMapper.getAllCount();
}
返回值:
[{
"id": 1,
"cnt": 11
},
{
"id": 2,
"cnt": 12
},
{
"id": 3,
"cnt": 13
},
{
"id": 4,
"cnt": 14
},
{
"id": 5,
"cnt": 15
},
{
"id": 6,
"cnt": 16
},
{
"id": 7,
"cnt": 17
},
{
"id": 8,
"cnt": 18
},
{
"id": 9,
"cnt": 19
},
{
"id": 10,
"cnt": 10
}]
3.2 返回分页数据和分页信息
// mapper接口
Page<Count> getAllCount();
// service接口
public PageInfo<Count> getAllCount(int pageNo, int pageSize) {
PageHelper.startPage(pageNo, pageSize);
PageInfo<Count> pageInfo = new PageInfo<>(countMapper.getAllCount());
return pageInfo;
}
返回值:
{
"total": 15,
"list": [{
"id": 1,
"cnt": 11
},
{
"id": 2,
"cnt": 12
},
{
"id": 3,
"cnt": 13
},
{
"id": 4,
"cnt": 14
},
{
"id": 5,
"cnt": 15
},
{
"id": 6,
"cnt": 16
},
{
"id": 7,
"cnt": 17
},
{
"id": 8,
"cnt": 18
},
{
"id": 9,
"cnt": 19
},
{
"id": 10,
"cnt": 10
}],
"pageNum": 1,
"pageSize": 10,
"size": 10,
"startRow": 1,
"endRow": 10,
"pages": 2,
"prePage": 0,
"nextPage": 2,
"isFirstPage": true,
"isLastPage": false,
"hasPreviousPage": false,
"hasNextPage": true,
"navigatePages": 8,
"navigatepageNums": [1,2],
"navigateFirstPage": 1,
"navigateLastPage": 2
}