小弟在此帖出自己平时用的hibernate分页类,仅供参考,欢迎提出宝贵意见,谢谢!
package itms.util;
import java.util.List;
import java.util.Iterator;
public class pageBean {
int currentPage = 1; // 当前页
public int totalPages = 0; // 总页数
int pageSize = 15;// 每页15条数据
int totalRows = 0; // 总数据数
int pageStartRow = 0;// 每页的起始数
int pageEndRow = 0; // 每页显示数据的终止数
boolean hasNextPage = false; // 是否有下一页
boolean hasPrevPage = false; // 是否有前一页
List list;
Iterator it;
public pageBean() {
}
public pageBean(List list) {
this.list = list;
totalRows = list.size();
it = list.iterator();
hasPrevPage = false;
currentPage = 1;
if ((totalRows % pageSize) == 0) {
totalPages = totalRows / pageSize;
} else {
totalPages = totalRows / pageSize + 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if (totalRows < pageSize) {
this.pageStartRow = 0;
this.pageEndRow = totalRows;
} else {
this.pageStartRow = 0;
this.pageEndRow = pageSize;
}
}
/**
* @return Returns the currentPage.
*/
public String getCurrentPage() {
return this.toString(currentPage);
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageRecorders.
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageRecorders
* The pageRecorders to set.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return Returns the pageEndRow.
*/
public int getPageEndRow() {
return pageEndRow;
}
/**
* @return Returns the pageStartRow.
*/
public int getPageStartRow() {
return pageStartRow;
}
/**
* @return Returns the totalPages.
*/
public String getTotalPages() {
return this.toString(totalPages);
}
/**
* @return Returns the totalRows.
*/
public String getTotalRows() {
return this.toString(totalRows);
}
/**
* @return Returns the hasNextPage.
*/
public boolean isHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return Returns the hasPreviousPage.
*/
public boolean isHasPrevPage() {
return hasPrevPage;
}
/**
* @param hasPreviousPage
* The hasPreviousPage to set.
*/
public void setHasPrevPage(boolean hasPrevPage) {
this.hasPrevPage = hasPrevPage;
}
public Object[] getNextPage() {
currentPage = currentPage + 1;
if ((currentPage - 1) > 0) {
hasPrevPage = true;
} else {
hasPrevPage = false;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
Object[] objs = getObjs();
return objs;
}
public Object[] getPrevPage() {
currentPage = currentPage - 1;
if (currentPage == 0) {
currentPage = 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if ((currentPage - 1) > 0) {
hasPrevPage = true;
} else {
hasPrevPage = false;
}
Object[] objs = getObjs();
return objs;
}
public Object[] getAppointPage(int appointPage) {
currentPage = appointPage;
if (currentPage <= 0) {
currentPage = 1;
}
if (currentPage >= totalPages) {
currentPage = totalPages;
hasNextPage = false;
} else {
hasNextPage = true;
}
if ((currentPage - 1) > 0) {
hasPrevPage = true;
} else {
hasPrevPage = false;
}
Object[] objs = getObjs();
return objs;
}
public Object[] getObjs() {
if (currentPage * pageSize < totalRows) {// 判断是否为最后一页
pageEndRow = currentPage * pageSize;
pageStartRow = pageEndRow - pageSize;
} else {
pageEndRow = totalRows;
pageStartRow = pageSize * (totalPages - 1);
}
Object[] objs = new Object[pageEndRow - pageStartRow + 1];
int j = 0;
for (int i = pageStartRow; i < pageEndRow; i++) {
Object obj = list.get(i);
objs[j++] = obj;
}
return objs;
}
public Object[] getAllObjs() {
Object[] objs = new Object[totalRows];
int j = 0;
for (int i = 0; i < totalRows; i++) {
Object obj = (Object) list.get(i);
objs[j++] = obj;
}
return objs;
}
}