人助者天助,天不助者人自助
做Javaweb开发一定离不开分页查询,服务端要实现分页一般可分为以下部分:1,需要一个pagebean。2,在每个controller中需要分页查询的接口方法的入参中加入分页对象(pagebean)。3,调用service方法查询数据后,按照pagebean中用户是否传入正确的pagenumber,size来判断是否进行总条数查询,如果判断成功那么调用service中查询总条数的方法,然后还需要给pagebean中总条数赋值。如下:
1.pagebean
package com.china.shanxi.songbi.common.bean;import java.util.List;
//分页对象类
public class PageBean {
/* int total;总记录数,这个需要从数据库中查询得到 int pageSize;每页显示的记录数,一般是设定好的具体数值 int pageNumber;当前页码,从前端获取 int totalPage;总页数,计算得到;有许多方法,介绍其中一种:totalPage = total % pageSize == 0 ? total / pageSize:total / pageSize + 1
int startIndex;起始索引,计算得到:(pageNumber-1)*pageSize;
*/
//总条数
private int total;
//总页数
private int totalPage;
//第几页
private int pageNumber;
//每页条数
private int pageSize;
//起始索引
private int startIndex;
private Listproducts;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getTotalPage() {
if (pageSize<=0){
return 0;
}
return totalPage =this.total %this.pageSize ==0 ?
(this.total /this.pageSize) : (this.total/this.pageSize+1);
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStartIndex() {
return startIndex =this.pageSize * (this.pageNumber -1);
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public List getProducts() {
return products;
}
public void setProducts(List products) {
this.products = products;
}
public PageBean() {
}
}