SpringBoot实现列表数据分页
系统环境:
编辑器:Idea2018.1
操作系统:win7
maven:1.8.0_171
StudentService中添加分页方法
/**
* 普通的列表分页
* @param page
* @param pageSize
* @return
*/
public Page<StudentEntity> pageRequest(int page, int pageSize){
//findAll中的page是从0开始计数的
return studentRepository.findAll(PageRequest.of(page -1 , pageSize));
}
StudentController中加上测试的调用方法
@RequestMapping(value = "/student/response", method = RequestMethod.GET)
public Page<StudentEntity> page(@RequestParam int page, @RequestParam int pageSize){
return studentService.pageRequest(page, pageSize);
}
此时若测试接口,可以看到SpringBoot默认的分页数据方式:
{
"content": [
{
"id": 27,
"name": "demo"
},
{
"id": 1,
"name": "demo11"
},
{
"id": 2,
"name": "lisi"
},
{
"id": 29,
"name": "lisi"
},
{
"id": 32,
"name": "lisi"
},
{
"id": 35,
"name": "lisi"
},
{
"id": 38,
"name": "lisi"
},
{
"id": 30,
"name": "wangwu"
},
{
"id": 33,
"name": "wangwu"
},
{
"id": 36,
"name": "wangwu"
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 0,
"pageSize": 10,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"totalPages": 2,
"totalElements": 15,
"last": false,
"number": 0,
"size": 10,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"numberOfElements": 10,
"first": true,
"empty": false
}
默认的列表展示样式不一定和项目中需要的分页样式相同,因此需要在此基础上做展示的优化,这里我分了两步,第一步,将列表包装在code、message中;第二步、将列表的字段名称做修改和精简
entity中添加CommonResponseEntity类
package com.example.demo.entity;
public class CommonResponseEntity<T> {
private final int status;
private final String message;
private T data;
private static final int SUCCESS_STATUS = 200;
private static final int DEFAULT_STATUS = SUCCESS_STATUS;
private static final String DEFAULT_MESSAGE = "ok";
public CommonResponseEntity(int status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
public CommonResponseEntity(int status, String message){
this.status = status;
this.message = message;
}
public static <T> CommonResponseEntity<T> success(T data){
return new CommonResponseEntity<>(DEFAULT_STATUS, DEFAULT_MESSAGE, data);
}
public static <T> CommonResponseEntity<T> error(int status, String message){
return new CommonResponseEntity<>(status, message);
}
public int getStatus() {
return status;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
在StudentController中添加路由测试:
@RequestMapping(value = "/student/responsePage", method = RequestMethod.GET)
public CommonResponseEntity<Page<StudentEntity>> responsePage(@RequestParam int page, @RequestParam int pageSize){
return CommonResponseEntity.success(studentService.pageRequest(page, pageSize));
}
若此时测试返回数据样式可以看到统一成了code、message包装后的样式:
{
"status": 200,
"message": "ok",
"data": {
"content": [
{
"id": 27,
"name": "demo"
},
{
"id": 1,
"name": "demo11"
},
{
"id": 2,
"name": "lisi"
},
{
"id": 29,
"name": "lisi"
},
{
"id": 32,
"name": "lisi"
},
{
"id": 35,
"name": "lisi"
},
{
"id": 38,
"name": "lisi"
},
{
"id": 30,
"name": "wangwu"
},
{
"id": 33,
"name": "wangwu"
},
{
"id": 36,
"name": "wangwu"
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 0,
"pageSize": 10,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"totalPages": 2,
"totalElements": 15,
"last": false,
"number": 0,
"size": 10,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"numberOfElements": 10,
"first": true,
"empty": false
}
}
以返回列表格式为page、pageSize、totalPage为例,需要在entity中添加PageResponse
package com.example.demo.entity;
import org.springframework.data.domain.Page;
import java.math.BigDecimal;
import java.util.List;
public class PageResponse<T> {
private final List<T> list;
private final int page;
private final int pageSize;
private final int totalPage;
public PageResponse(List<T> data, int pageSize, int page){
this.page = page;
this.pageSize = pageSize;
this.totalPage = (int)Math.ceil(BigDecimal.valueOf(data.size()).divide(BigDecimal.valueOf(pageSize)).doubleValue());
this.list = data.subList((page -1) * pageSize, Math.min(page * pageSize, data.size()));
}
public PageResponse(Page<T> page){
this.list = page.getContent();
//page类的number是从0开始计数的
this.page = page.getNumber() + 1;
this.pageSize = page.getSize();
this.totalPage = page.getTotalPages();
}
public static <T> PageResponse<T> of(List<T> data, int pageSize, int page){
return new PageResponse<>(data, pageSize, page);
}
public List<T> getList() {
return list;
}
public int getPage() {
return page;
}
public int getPageSize() {
return pageSize;
}
public int getTotalPage() {
return totalPage;
}
}
然后在StudentController中添加测试方法:
@RequestMapping(value = "/student/responsePageResponse", method = RequestMethod.GET)
public CommonResponseEntity<PageResponse<StudentEntity>> responsePageResponse(@RequestParam int page, @RequestParam int pageSize){
return CommonResponseEntity.success(PageResponse.of(studentService.findAll(), pageSize, page));
}
最后,为了验证,可以测试下该路由,可以看到数据已经成为我们需要的样子:
{
"status": 200,
"message": "ok",
"data": {
"list": [
{
"id": 27,
"name": "demo"
},
{
"id": 1,
"name": "demo11"
},
{
"id": 2,
"name": "lisi"
},
{
"id": 29,
"name": "lisi"
},
{
"id": 32,
"name": "lisi"
},
{
"id": 35,
"name": "lisi"
},
{
"id": 38,
"name": "lisi"
},
{
"id": 30,
"name": "wangwu"
},
{
"id": 33,
"name": "wangwu"
},
{
"id": 36,
"name": "wangwu"
}
],
"page": 1,
"pageSize": 10,
"totalPage": 2
}
}