通用分页

        private int total = 0 ;  //总记录数
private static int page = 1; //当前页
private int rows = 10; //每页大小
private int firstPage = 1; //首页
private int lastPage; //尾页(总页数)
private List<T> content = new ArrayList<>();//存放分页数据的集合

private boolean b = false; //是否初始化参数


        /**

* @Title: init 
* @Description: TODO(初始化参数) 
* @param total 总记录数
* @param rows 每页大小
* @param page 当前页
*/
public void init(int total,int rows,int page) {
this.total = total;//总记录数
this.rows = rows;//每页大小
//尾页(总页数)
this.lastPage=this.total % this.rows > 0 ? this.total / this.rows + 1 : this.total / this.rows;
//当前页
if(page>this.lastPage) {
this.page = this.lastPage;
}else if(page<1) {
this.page = 1;
}else {
this.page = page;
}
b=true;

}



      /**
* 注意:调用此方法前请先调用init()
* @Title: paging 
* @Description: TODO(获得分页) 
* @param list 分页的集合
* @return List<T>
*/
public List<T> paging(List<T> list){
if(b && list!=null && list.size() !=0) {
//总记录数
this.total = list.size();
//获得数据显示的区间
int s = page*rows<total?page*rows:total;
for (int i = (page-1)*rows; i < s; i++) {
content.add(list.get(i));
}
return  this.content;
}else {
return  null;
}
}

猜你喜欢

转载自blog.csdn.net/qq_40369944/article/details/80457358