1. 程式人生 > 其它 >簡單的Java分頁工具類

簡單的Java分頁工具類

Java開發工具有很多,Java分頁工具類只是其中之一。

import java.util.List;
/**
* Paging tool
* @author Administrator
*
*/
public class PageBean<T> {
private int pageNo = 1; //Current page
private int pageSize = 4; //Number of pages per page
private int totalCount; //Total number of records
private int totalPages; //Total pages--read only
private List<T> pageList; //The collection generic type corresponding to each page
public int getPageNo() {
return pageNo;
}
//The current page number cannot be less than 1 and cannot be greater than the total number of pages
public void setPageNo(int pageNo) {
if(pageNo<1)
this.pageNo = 1;
else if(pageNo > totalPages)
this.pageNo = totalPages;
else
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
//The total number of records determines the total number of pages
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
this.totalPages = (this.totalCount%this.pageSize==0)?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1;
}
public int getTotalCount() {
return totalCount;
}
//Read only
public int getTotalPages() {
return totalPages;
}
public List<T> getPageList() {
return pageList;
}
public void setPageList(List<T> pageList) {
this.pageList = pageList;
}
public PageBean(int pageNo, int pageSize, int totalCount, int totalPages,
List<T> pageList) {
super();
this.pageNo = pageNo;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPages = totalPages;
this.pageList = pageList;
}
public PageBean() {
super();
// TODO Auto-generated constructor stub
}
}

mysql 分頁:select * from table limit (pageNo-1)*pageSize,pageSize;

oracle分頁:select a.* (select table.*,rowum rn from table) a where rn>(pageNo-1)*pageSize and rn <=pageNo*pageSize;

這是最簡單的分頁工具之一。