逻辑分页之PageBean

PageBean:

  • 基本需求:具有前台页面分页所需的所有参数内容
  • 特点:具有逻辑分页功能
  • 原理:利用List的subList实现对当前页数据的截取
  • 具体代码如下:
package com.java.utils;

import java.io.Serializable;
import java.util.List;

public class PageBean implements Serializable{
	
	private static final long serialVersionUID = 787170437337348516L;
	
	//每页显记录的条数
	private Integer pageSize;
	//当前页
	private Integer currentPage;
	//总页数
	private Integer totalPage;
	//总记录数
	private Integer totalRecord;
	//要显示的数据
	private List dataList;
	
	public PageBean() {
	}
	/**
	 * 逻辑分页
	 * @param pageNum	当前页
	 * @param pageSize	当前页显示的记录条数数
	 * @param sourceList	所有数据集合
	 */
	public PageBean(Integer pageNum, Integer pageSize, List sourceList){
		//总记录数
		this.totalRecord = sourceList.size();
		//每页显示的记录条数
		this.pageSize = pageSize;
		//总页数
		this.totalPage = (this.totalRecord+this.pageSize-1)/this.pageSize;
		//当前页小于1则当前页等于1
		pageNum = pageNum >= 1 ? pageNum : 1;
		//当前页
		this.currentPage = pageNum >= totalPage ? totalPage : pageNum;
		
		//起始索引
		Integer fromIndex = (this.currentPage-1) * this.pageSize;
		//结束索引
		Integer toIndex = this.pageSize*this.currentPage >= this.totalRecord ? this.totalRecord : this.pageSize*this.currentPage;
		//当前页显示的记录
		this.dataList = sourceList.subList(fromIndex, toIndex);
	}
	/**
	 * 通过构造函数生成一个Pager对象
	 * @param currentPage	当前页
	 * @param pageSize	当前页显示的记录条数
	 * @param totalRecord	总记录数
	 * @param dataList	当前页要显示的数据
	 */
	public PageBean(Integer currentPage, Integer pageSize,Integer totalRecord, List dataList) {
		this.pageSize = pageSize;
		this.currentPage = currentPage;
		this.totalPage = (totalRecord+pageSize-1)/pageSize;
		this.totalRecord = totalRecord;
		this.dataList = dataList;
	}

	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public Integer getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	public Integer getTotalRecord() {
		return totalRecord;
	}
	public void setTotalRecord(Integer totalRecord) {
		this.totalRecord = totalRecord;
	}
	public List getDataList() {
		return dataList;
	}
	public void setDataList(List dataList) {
		this.dataList = dataList;
	}
	
}

 

你可能感兴趣的:(常用工具类整理)