java后端实现分页逻辑
创建一个工具类
用来计算和存储页码、页数等参数
public class PageSupport {
//当前页码-来自于用户输入
private int currentPageNo = 1;
//总数量(表)
private int totalCount = 0;
//页面容量
private int pageSize = 0;
//总页数-totalCount/pageSize(+1)
private int totalPageCount = 1;
public int getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(int currentPageNo) {
if(currentPageNo > 0){
this.currentPageNo = currentPageNo;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if(totalCount > 0){
this.totalCount = totalCount;
//设置总页数
this.setTotalPageCountByRs();
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize > 0){
this.pageSize = pageSize;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public void setTotalPageCountByRs(){
if(this.totalCount % this.pageSize == 0){
this.totalPageCount = this.totalCount / this.pageSize;
}else if(this.totalCount % this.pageSize > 0){
this.totalPageCount = this.totalCount / this.pageSize + 1;
}else{
this.totalPageCount = 0;
}
}
}
controller 实现方法
其中limit 、page 是前端传入的参数。 limit 表示每页记录数,
page 表示当前页码
if (params.get("limit") == null) {
params.put("limit", 10);
}
String pageIndex="";
if (params.get("page") != null) {
pageIndex= (String) params.get("page");
}
String num= (String) params.get("limit");
int pageSize=Integer.parseInt(num);//每页显示的条数
Query query = new Query(params);
int total = wksbService.count(query);//数据库中总的记录数
Map pageParams=CommonUtil.getPageParams(total,pageIndex,pageSize);
int offset = Integer.parseInt(String.valueOf(pageParams.get("offset")));//数据库开始的下标
query.put("offset",offset);
int limit=offset+pageSize;
query.put("limit",limit);
List list= wksbService.list(query);
此时返回的list就是分页的数据
编写一个方法 getPageParams() 用来 计算出sql语句开始查询的记录数 。其中需要传入总的记录数,以及页码、每页显示的条数
public static Map getPageParams(int totalCount,String pageIndex,int pageSize){
Map resultMap=new HashMap();
//当前页码
int currentPageNo = 1;
if (pageIndex != null) {
try {
currentPageNo = Integer.valueOf(pageIndex);
} catch (NumberFormatException e) {
System.out.println("传入的页码 不规范!");
}
}
//总页数
PageSupport pages = new PageSupport();
pages.setCurrentPageNo(currentPageNo);
pages.setPageSize(pageSize);
pages.setTotalCount(totalCount);
int totalPageCount = pages.getTotalPageCount();
//控制首页和尾页
if (currentPageNo < 1) {
currentPageNo = 1;
} else if (currentPageNo > totalPageCount) {
currentPageNo = totalPageCount;
}
int offset = (currentPageNo - 1) * pageSize;
resultMap.put("offset",offset);
return resultMap;
}
工具类 Query
就是自己封装的一个Map
public class Query extends LinkedHashMap<String, Object> {
private static final long serialVersionUID = 1L;
// offset开始索引,limit步长
private int offset;
// 每页条数
private int limit;
public Query(Map<String, Object> params) {
this.putAll(params);
// 分页参数
this.offset = Integer.parseInt(params.get("offset").toString());
this.limit = Integer.parseInt(params.get("limit").toString());
this.put("offset", offset);
this.put("page", offset / limit + 1);
this.put("limit", limit);
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.put("offset", offset);
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
}