逻辑分页之PageBean

PageBean:

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

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

public class PageBean<T> implements Serializable{
	
	private static final long serialVersionUID = 787170437337348516L;
	
	//每页显记录的条数
	private Integer pageSize;
	//当前页
	private Integer currentPage;
	//总页数
	private Integer totalPage;
	//总记录数
	private Integer totalRecord;
	//要显示的数据
	private List<T> dataList;
	
	public PageBean() {
	}
	/**
	 * 逻辑分页
	 * @param pageNum	当前页
	 * @param pageSize	当前页显示的记录条数数
	 * @param sourceList	所有数据集合
	 */
	public PageBean(Integer pageNum, Integer pageSize, List<T> 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<T> 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<T> getDataList() {
		return dataList;
	}
	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
	}
	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值