用Java編寫的page分頁工具類,其代碼如下:
package entity;
import java.io.Serializable;
import java.util.List;
/***
* @author Aaron
* @param <T>
*/
public class Page <T> implements Serializable{
// 頁數(第幾頁)
private long currentpage;
// 查詢資料庫裡面對應的資料有多少條
private long total;// 從資料庫查處的總記錄數
// 每頁查5條
private int size;
// 下頁
private int next;
private List<T> list;
// 最後一頁
private int last;
private int lpage;//左邊的開始的頁碼
private int rpage;//右邊的開始的頁碼
//從哪條開始查
private long start;
//全局偏移量
public int offsize = 2;
public Page() {
super();
}
/****
*
* @param currentpage
* @param total
* @param pagesize
*/
public void setCurrentpage(long currentpage,long total,long pagesize) {
//可以整除的情況下
long pagecount = total/pagesize;
//如果整除表示正好分N頁,如果不能整除在N頁的基礎上+1頁
int totalPages = (int) (total%pagesize==0? total/pagesize : (total/pagesize)+1);
//總頁數
this.last = totalPages;
//判斷目前頁是否越界,如果越界,我們就查最後一頁
if(currentpage>totalPages){
this.currentpage = totalPages;
}else{
this.currentpage=currentpage;
}
//計算start
this.start = (this.currentpage-1)*pagesize;
}
//上一頁
public long getUpper() {
return currentpage>1? currentpage-1: currentpage;
}
//總共有多少頁,即末頁
public void setLast(int last) {
this.last = (int) (total%size==0? total/size : (total/size)+1);
}
/****
* 帶有偏移量設定的分頁
* @param total
* @param currentpage
* @param pagesize
* @param offsize
*/
public Page(long total,int currentpage,int pagesize,int offsize) {
this.offsize = offsize;
initPage(total, currentpage, pagesize);
}
/****
*
* @param total 總記錄數
* @param currentpage 目前頁
* @param pagesize 每頁顯示多少條
*/
public Page(long total,int currentpage,int pagesize) {
initPage(total,currentpage,pagesize);
}
/****
* 初始化分頁
* @param total
* @param currentpage
* @param pagesize
*/
public void initPage(long total,int currentpage,int pagesize){
//總記錄數
this.total = total;
//每頁顯示多少條
this.size=pagesize;
//計算目前頁和資料庫查詢起始值以及總頁數
setCurrentpage(currentpage, total, pagesize);
//分頁計算
int leftcount =this.offsize, //需要向上一頁執行多少次
rightcount =this.offsize;
//起點頁
this.lpage =currentpage;
//結束頁
this.rpage =currentpage;
//2點判斷
this.lpage = currentpage-leftcount; //正常情況下的起點
this.rpage = currentpage+rightcount; //正常情況下的終點
//頁差=總頁數和結束頁的差
int topdiv = this.last-rpage; //判斷是否大于最大頁數
/***
* 起點頁
* 1、頁差<0 起點頁=起點頁+頁內插補點
* 2、頁差>=0 起點和終點判斷
*/
this.lpage=topdiv<0? this.lpage+topdiv:this.lpage;
/***
* 結束頁
* 1、起點頁<=0 結束頁=|起點頁|+1
* 2、起點頁>0 結束頁
*/
this.rpage=this.lpage<=0? this.rpage+(this.lpage*-1)+1: this.rpage;
/***
* 當起點頁<=0 讓起點頁為第一頁
* 否則不管
*/
this.lpage=this.lpage<=0? 1:this.lpage;
/***
* 如果結束頁>總頁數 結束頁=總頁數
* 否則不管
*/
this.rpage=this.rpage>last? this.last:this.rpage;
}
public long getNext() {
return currentpage<last? currentpage+1: last;
}
public void setNext(int next) {
this.next = next;
}
public long getCurrentpage() {
return currentpage;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public long getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public long getLast() {
return last;
}
public long getLpage() {
return lpage;
}
public void setLpage(int lpage) {
this.lpage = lpage;
}
public long getRpage() {
return rpage;
}
public void setRpage(int rpage) {
this.rpage = rpage;
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
public void setCurrentpage(long currentpage) {
this.currentpage = currentpage;
}
/**
* @return the list
*/
public List<T> getList() {
return list;
}
/**
* @param list the list to set
*/
public void setList(List<T> list) {
this.list = list;
}
public static void main(String[] args) {
//總記錄數
//目前頁
//每頁顯示多少條
int cpage =17;
Page page = new Page(1001,cpage,50,7);
System.out.println("開始頁:"+page.getLpage()+"__目前頁:"+page.getCurrentpage()+"__結束頁"+page.getRpage()+"____總頁數:"+page.getLast());
}
}
僅供個人學習記錄,如有侵權可聯系删除。