在项目中使用的实用hibernate 分页

来源:互联网 发布:做淘宝优惠券赚钱吗 编辑:程序博客网 时间:2024/05/19 01:11

将项目中使用的分页方法记录如下

PageIndex.java

package com.archermind.util;

/**
 * 页索引,主要是起始页和终止页
 */
public class PageIndex
{
 /** 开始索引 **/
 private int startIndex;
 /** 结束索引 **/
 private int endIndex;

 public PageIndex(int startIndex, int endIndex)
 {
  this.startIndex = startIndex;
  this.endIndex = endIndex;
 }

 public int getStartIndex()
 {
  return startIndex;
 }

 public void setStartIndex(int startIndex)
 {
  this.startIndex = startIndex;
 }

 public int getEndIndex()
 {
  return endIndex;
 }

 public void setEndIndex(int endIndex)
 {
  this.endIndex = endIndex;
 }

 /**
  * 起始页和终止页
  *
  * @param viewPageCount
  *            显示多少页
  * @param currentPage
  *            当前页
  * @param totalpage
  *            总页数
  * @return PageIndex 起始页和终止页
  */
 public static PageIndex getPageIndex(int viewPageCount, int currentPage, int totalpage)
 {
  int startpage = currentPage - (viewPageCount % 2 == 0 ? viewPageCount / 2 - 1 : viewPageCount / 2);
  int endpage = currentPage + viewPageCount / 2;
  if (startpage < 1)
  {
   startpage = 1;
   if (totalpage >= viewPageCount)
    endpage = viewPageCount;
   else
    endpage = totalpage;
  }
  if (endpage > totalpage)
  {
   endpage = totalpage;
   if ((endpage - viewPageCount) > 0)
    startpage = endpage - viewPageCount + 1;
   else
    startpage = 1;
  }
  return new PageIndex(startpage, endpage);
 }
}

 

QueryResult.java

package com.archermind.util;

import java.util.List;

/**
 * 查询结果集,包括数据和总数
 *
 */
public class QueryResult<T>
{
 /** 查询得出的数据List **/
 private List<T> resultList;
 /** 查询得出的总数 **/
 private int totalRecord;

 public List<T> getResultList()
 {
  return resultList;
 }

 public void setResultList(List<T> resultList)
 {
  this.resultList = resultList;
 }

 public int getTotalRecord()
 {
  return totalRecord;
 }

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

PageView.java

 

 

package com.archermind.util;

import java.util.List;
 
   public class PageView<T> {
  /** 分页数据 **/
  private List<T> records;
  /** 页码开始索引和结束索引 **/
  private PageIndex pageindex;
  /** 总页数 **/
  private long totalpage =1;
  /** 每页显示记录数 **/
  private int maxresult =10;
  /** 当前页 **/
  private int currentpage =1;
  /** 总记录数 **/
  private int totalrecord;
  /** 页码数量 **/
  private int pagecode =3;
  /** 要获取记录的开始索引 **/
  public int getFirstResult() {
  return (this.currentpage-1)*this.maxresult;
  }
  public int getPagecode() {
  return pagecode;
  }
 
  public void setPagecode(int pagecode) {
  this.pagecode = pagecode;
  }
  public PageView(){}
 
  public PageView(int maxresult, int currentpage) {
  this.maxresult = maxresult;
  this.currentpage = currentpage;
  }
 
  public void setQueryResult(QueryResult<T> qr){
  setTotalrecord(qr.getTotalRecord());
  setRecords(qr.getResultList());
  }
 
  public long getTotalrecord() {
  return totalrecord;
  }
  public void setTotalrecord(int totalrecord) {
  this.totalrecord = totalrecord;
  setTotalpage(this.totalrecord%this.maxresult==0?this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
  }
  public List<T> getRecords() {
  return records;
  }
  public void setRecords(List<T> records) {
  this.records = records;
  }
  public PageIndex getPageindex() {
  return pageindex;
  }
  public long getTotalpage() {
  return totalpage;
  }
  public void setTotalpage(int totalpage) {
  this.totalpage = totalpage;
  this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
  }
  public int getMaxresult() {
  return maxresult;
  }
  public int getCurrentpage() {
  return currentpage;
  }
  public void setCurrentpage(int currentpage){
  this.currentpage=currentpage;
  }
  }

 

 

原创粉丝点击