分頁的工具類

package cn.util;


/**
 * 分頁的工具類
 * 
 * 總共36條記錄,每頁10條,問:共幾頁?4頁 當前頁 起始條數 1 1 2 11 3 21 4 31 起始條數如何算出來的? (頁數 - 1)*10 +
 * 1
 * 
 * ~生成讀寫器,做容錯處理
 * @author sun
 *
 */
public class PageInfoUtil
{
/* 每頁多少條 */
private int pageSize = 10;
/* 當前頁 */
private int currentPage;
/* 總條數 */
private int totalRecord;


/* 上一頁 */
private int prePage;
/* 下一頁 */
private int nextPage;
/* 總頁數 */
private int totalPage;
/* 當前記錄數(每頁的起始條數) */
private int currRecord;


public int getPageSize()
{
return pageSize;
}


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


public int getCurrentPage()
{
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 getTotalRecord()
{
return totalRecord;
}


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


public int getPrePage()
{
this.prePage = this.currentPage - 1 ; 
if(this.prePage < 1 )
{
this.prePage = 1 ; 
}
return prePage;
}


public int getNextPage()
{
this.nextPage = this.currentPage + 1 ;
if(this.nextPage > this.getTotalPage())
{
this.nextPage = this.getTotalPage() ; 
}
return nextPage;
}


public int getTotalPage()
{
/* 
* 總頁數

* 總條數 / 10 + 1 

* 總條數如果是30條,每頁10條,問多少頁?
* */
this.totalPage = this.totalRecord / this.pageSize ; 
if(this.totalRecord % this.pageSize != 0 )
{
this.totalPage = this.totalRecord / this.pageSize + 1;
}
return totalPage;
}


public int getCurrRecord()
{
/* 當前頁的起始條數如何算呢? 
* 不能加+1,
* Mysql的limit?,?是從0開始的.
* */
this.currRecord = (this.getCurrentPage() - 1 ) * this.getPageSize() ; 
return currRecord;
}


}
發佈了31 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章