分页工具类 pageInfoUtil

import java.io.Serializable;


/**
 * 分页的工具类
 * @author 
 */
public class PageInfoUtil implements Serializable
{
	/* 总条数 */
	private int totalRecord;
	/* 每页多少条 */
	private int pageSize = 20;

	/* 总页数 */
	private int totalPage;
	/* 当前页 */
	private int currentPage;
	/* 上一页 */
	private int prePage;
	/* 下一页 */
	private int nextPage;

	/*
	 * 当前页对应的条数 如果每页10条, 1 1 2 11 3 21
	 * 
	 * limit ?(currRecord),?(pageSize)
	 */
	private int currRecord;

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getCurrentPage()
	{
		/*
		 * -1,
		 * 100000>总页数的时候
		 * 获取当前页的时候要做一个判断
		 * */
		if(this.currentPage < 1)
		{
			this.currentPage = 1 ; 
		}
		if(this.getTotalPage() > 0 && this.currentPage > this.getTotalPage())
		{
			this.currentPage = this.getTotalPage() ; 
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}

	public int getTotalPage()
	{
		/*
		 * 总页数
		 * 总记录数:每页多少条,
		 * 总页数????咋算?
		 * 
		 * 总记录数,每页多少条
		 * 21		10		3
		 * 20		10 		2
		 * */
		if(this.totalRecord % this.pageSize == 0)
		{
			this.totalPage = this.totalRecord / this.pageSize; 
		}else
		{
			this.totalPage = this.totalRecord / this.pageSize + 1;
		}
		return totalPage;
	}

	public int getPrePage()
	{
		this.prePage = this.getCurrentPage() - 1 ;
		/*
		 * 总页数为:5
		 * this.getCurrentPage():的值:1 --- > 5
		 * 上页一页的值:0-->4
		 * 所以上一页的取值范围木有必须和总页数相比,因为上一页始终小于总页数
		 * */
		if(this.prePage < 1)
		{
			this.prePage = 1 ; 
		}
		return prePage;
	}

	public int getNextPage()
	{
		this.nextPage = this.getCurrentPage() + 1 ;
		/*
		 * 总页数为:5
		 * this.getCurrentPage():的值:1 --- > 5
		 * 下一页的值:2-->6
		 * 所以上一页的取值范围木有必须和总页数相比,因为上一页始终小于总页数
		 * */
		if(this.getTotalPage() > 0 && this.nextPage > this.getTotalPage())
		{
			this.nextPage = this.getTotalPage() ; 
		}
		return nextPage;
	}

	public int getCurrRecord()
	{
		/*
		 *当前页的条数:
		 * 每页10条
		 * 当前页	当前页的条数
		 * 1		1
		 * 2		11
		 * 3		21
		 * 4		31
		 * 5		51
		 * 数据库的索引值是从0开始的
		 * SELECT * FROM `a_admins` LIMIT 0, 1000
		 * 因为数据库的原因,不应该+1
		 */
		this.currRecord = (this.getCurrentPage() - 1 ) * this.pageSize; 
		return currRecord;
	}
	
	public static void main(String[] args)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		pageInfoUtil.setTotalRecord(50);
		pageInfoUtil.setPageSize(10);
		pageInfoUtil.setCurrentPage(1000);
		System.out.println(pageInfoUtil);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

散装程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值