SpringBoot2.0集成分页插件pagehelper-spring-boot-starter
参考SpringBoot2.0集成分页插件pagehelper-spring-boot-starter
1. 添加pom
依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
2. 添加配置
application.yml
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
application.prperties
# 添加分页配置信息
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true
3. 在service中添加如下代码
// 开启分页插件,放在查询语句上面 帮助生成分页语句
PageHelper.startPage(page, size);
List<Attachment> attachments = attachmentMapper.getAllAttachments();
// 封装分页之后的数据 返回给客户端展示 PageInfo做了一些封装 作为一个类
PageInfo<Attachment> pageAttachments = new PageInfo<>(attachments);
return pageAttachments;
4. 控制器代码
@GetMapping("/attachment")
public ApiResponse getAllAttachmentsGroup(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
return attachmentService.getAllAttachments(page, size);
}
5. 返回数据
{
"code": 200,
"message": "success",
"timestamp": "2020-05-27T09:54:54.784+08:00",
"data": {
"total": 6,
"list": [
{
"id": 1,
"title": "来源组3",
"format": "doc",
"url": "/opt/data/aa4.doc",
"articleId": 1,
"size": 200,
"filename": "aa4.doc",
"filenameOrigin": "abc4.doc"
},
{
"id": 2,
"title": "来源组3",
"format": "doc",
"url": "/opt/data/aa5.doc",
"articleId": 1,
"size": 200,
"filename": "aa4.doc",
"filenameOrigin": "abc4.doc"
}
],
"pageNum": 1,
"pageSize": 2,
"size": 2,
"startRow": 1,
"endRow": 2,
"pages": 3,
"prePage": 0,
"nextPage": 2,
"isFirstPage": true,
"isLastPage": false,
"hasPreviousPage": false,
"hasNextPage": true,
"navigatePages": 8,
"navigatepageNums": [
1,
2,
3
],
"navigateFirstPage": 1,
"navigateLastPage": 3
}
}