hibernate通用分页类

小弟在此帖出自己平时用的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;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sunfount

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

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

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

打赏作者

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

抵扣说明:

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

余额充值