1.導入jar包
<!-- 分頁插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
<exclusions>
<exclusion>
<artifactId>pagehelper</artifactId>
<groupId>com.github.pagehelper</groupId>
</exclusion>
</exclusions>
</dependency>
2.yml中的配置

pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
3.封裝PageRequest
package com.ccc.demoboot.commons;
/**
* 分頁請求
*/
public class PageRequest {
/**
* 當前頁碼
*/
private int pageNum;
/**
* 每頁數量
*/
private int pageSize;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
4.封裝PageUtils
package com.ccc.demoboot.commons;
import com.github.pagehelper.PageInfo;
public class PageUtils {
/**
* 將分頁信息封裝到統一的接口
* @return
*/
public static PageResult getPageResult(PageInfo<?> pageInfo) {
PageResult pageResult = new PageResult();
pageResult.setPageNum(pageInfo.getPageNum());
pageResult.setPageSize(pageInfo.getPageSize());
pageResult.setTotalSize(pageInfo.getTotal());
pageResult.setTotalPages(pageInfo.getPages());
pageResult.setContent(pageInfo.getList());
return pageResult;
}
}
5.封裝分頁返回的結果PageResult
package com.ccc.demoboot.commons;
import java.util.List;
/**
* 分頁結果類
*/
public class PageResult {
/**
* 當前頁碼
*/
private int pageNum;
/**
* 每頁數量
*/
private int pageSize;
/**
* 記錄總數
*/
private long totalSize;
/**
* 頁碼總數
*/
private int totalPages;
/**
* 數據模型
*/
private List<?> content;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public List<?> getContent() {
return content;
}
public void setContent(List<?> content) {
this.content = content;
}
}
6.controller層的實現
注意:以下四句話必須放在查詢總數之前,放在邏輯處理之前,不然分頁將不起作用

package com.ccc.demoboot.controller;
import com.ccc.demoboot.commons.*;
import com.ccc.demoboot.domain.*;
import com.ccc.demoboot.response.ResultModel;
import com.ccc.demoboot.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
public class pagecs {
@Autowired
private RiskUserService riskUserService;
@RequestMapping(value = "getuser11")
public ResultModel addProject(@RequestBody Map<String, Object> map) {
PageRequest pageRequest=new PageRequest();
pageRequest.setPageSize(Integer.parseInt(map.get("pageSize").toString()));
pageRequest.setPageNum(Integer.parseInt(map.get("pageNum").toString()));
PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
List<RiskUser> riskUserList=riskUserService.getUserByCompanyId(17);
PageResult pageResult= PageUtils.getPageResult(new PageInfo<>(riskUserList));
return ResultModel.successModel("查詢成功",pageResult);
}
}
附加內容:PageInfo屬性表
當前頁 private int pageNum; 每頁的數量 private int pageSize; 當前頁的數量 private int size; //由於startRow和endRow不常用,這里說個具體的用法 //可以在頁面中"顯示startRow到endRow 共size條數據" 當前頁面第一個元素在數據庫中的行號 private int startRow; 當前頁面最后一個元素在數據庫中的行號 private int endRow; 總記錄數 private long total; 總頁數 private int pages; 結果集 private List<T> list; 第一頁 private int firstPage; 前一頁 private int prePage; 是否為第一頁 private boolean isFirstPage = false; 是否為最后一頁 private boolean isLastPage = false; 是否有前一頁 private boolean hasPreviousPage = false; 是否有下一頁 private boolean hasNextPage = false; 導航頁碼數 private int navigatePages; 所有導航頁號 private int[] navigatepageNums; 后台分頁
