通用的hibernate分页的实例

 因为现在框架很多都用hibernate,所以写了一个比较通用的hibernate分页的实例

1。首先是 Page 类
  1. public class Page {
  2. private int pageSize = 10;
  3. private int totalCount;
  4. private int currentPage;
  5. private int startIndex = 0;
  6. private int[] indexes = new int[0];
  7. private int nextIndex;
  8. private int previousIndex;
  9. private int pageCount;
  10. private int lastIndex = 0;
  11. public Page(int pageSize,
  12.     int startIndex) {
  13.    this.pageSize=pageSize;
  14.    this.startIndex=startIndex;
  15.    
  16. }
  17. public Page(List list, int totalCount) {
  18.    setPageSize(pageSize);
  19.    setTotalCount(totalCount);
  20.    setStartIndex(0);
  21. }
  22. public Page(List list, int totalCount, int startIndex) {
  23.    setPageSize(pageSize);
  24.    setTotalCount(totalCount);
  25.    
  26.    setStartIndex(startIndex);
  27.    
  28. }
  29. public Page(List list, int totalCount, int pageSize,
  30.     int startIndex) {
  31.    setPageSize(pageSize);
  32.    if(totalCount==0)
  33.    {
  34.     setTotalCount(1);
  35.    }else{
  36.    setTotalCount(totalCount);
  37.    }
  38.    setStartIndex(startIndex);
  39.    
  40. }
  41. public void setTotalCount(int totalCount) {
  42.    if (totalCount > 0) {
  43.     this.totalCount = totalCount;
  44.     int count = totalCount / pageSize;
  45.     if (totalCount % pageSize > 0)
  46.      count++;
  47.     indexes = new int[count];
  48.     for (int i = 0; i < count; i++) {
  49.      indexes[i] = pageSize * i;
  50.     }
  51.      } else {
  52.     this.totalCount = 0;
  53.    }
  54. }
  55. public int getTotalCount() {
  56.    return totalCount;
  57. }
  58. public void setIndexes(int[] indexes) {
  59.    this.indexes = indexes;
  60. }
  61. public int[] getIndexes() {
  62.    return indexes;
  63. }
  64. public void setStartIndex(int startIndex) {
  65.    if (totalCount <= 0)
  66.     this.startIndex = 0;
  67.    else if (startIndex >= totalCount)
  68.     this.startIndex = indexes[indexes.length - 1];
  69.    else if (startIndex < 0)
  70.     this.startIndex = 0;
  71.    else {
  72.     this.startIndex = indexes[startIndex / pageSize];
  73.     
  74.    }
  75.     }
  76. public int getStartIndex() {
  77.   
  78.    return startIndex;
  79. }
  80. public void setNextIndex(int nextIndex) {
  81.    this.nextIndex = nextIndex;
  82. }
  83. public int getNextIndex() {
  84.    int nextIndex = getStartIndex() + pageSize;
  85.    if (nextIndex >= totalCount)
  86.     return getStartIndex();
  87.    else
  88.     return nextIndex;
  89. }
  90. public void setPreviousIndex(int previousIndex) {
  91.    this.previousIndex = previousIndex;
  92. }
  93. public int getPreviousIndex() {
  94.    int previousIndex = this.getStartIndex()-this.pageSize;
  95.   
  96.    if (previousIndex <=0)
  97.     return 0;
  98.    else
  99.     return previousIndex;
  100. }
  101. public void setPageCount(int pageCount) {
  102.    this.pageCount = pageCount;
  103. }
  104. public int getPageCount() {
  105.    int count = totalCount / pageSize;
  106.    if (totalCount % pageSize > 0)
  107.     count++;
  108.    return count;
  109. }
  110. public int getCurrentPage() {
  111.    if(0== this.getPageCount())
  112.    {
  113.     return 0;
  114.    }else{
  115.    return getStartIndex() / pageSize + 1;
  116.    }
  117. }
  118. public void setCurrentPage(int currentPage) {
  119.    this.currentPage = currentPage;
  120. }
  121. public void setLastIndex(int lastIndex) {
  122.    this.lastIndex =lastIndex ;
  123. }
  124. public int getLastIndex() {
  125.   
  126.    if(indexes.length<=0)
  127.    {
  128.     return 0;
  129.    }else{
  130.     return indexes[indexes.length-1];
  131.    }
  132. }
  133. public int getPageSize() {
  134.    return pageSize;
  135. }
  136. public void setPageSize(int pageSize) {
  137.    this.pageSize = pageSize;
  138. }
  139. }
2. SearchResult 类
  1. public class SearchResult {
  2. private List list = new ArrayList();
  3. private Page page;
  4. public List getList() {
  5.    return list;
  6. }
  7. public void setList(List list) {
  8.    this.list = list;
  9. }
  10. public Page getPage() {
  11.    return page;
  12. }
  13. public void setPage(Page page) {
  14.    this.page = page;
  15. }
  16. }
3 page.jsp 需要用到jstl的c标签
  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <script type="text/javascript">
  4. function changeRecord(num){
  5. document.getElementById('startIndex').value=num
  6.    document.forms[0].submit();
  7. }
  8. function gopage(lastnum)
  9. {
  10. var page=document.getElementById('page').value;
  11. var num=parseInt(page*10)-parseInt(10);
  12. //if(num>lastnum)num=lastnum;
  13. if(num>lastnum)
  14. {
  15.    alert("错误页数");
  16.    return false;
  17. }
  18. if(num<0)num=0;
  19.    
  20. document.getElementById('startIndex').value=num
  21.    document.forms[0].submit();
  22. }
  23. </script>
  24. <c:if test="${!empty Page}">
  25. <table align="right">
  26. <tr>
  27.    <td>
  28.       共${Page.totalCount}记录
  29.       <input type="hidden" name="startIndex" id="startIndex" value="0">
  30.       </td>
  31.       <td>
  32.      <c:choose>
  33.       <c:when test="${Page.startIndex ne '0'}">
  34.         <a href="#" onclick="changeRecord('0')">首页</a>
  35.            </c:when>
  36.       <c:otherwise>
  37.       首页          
  38.      </c:otherwise>
  39.      </c:choose>
  40.       </td>
  41.       <td>
  42.      <c:choose>
  43.       <c:when test="${Page.startIndex ne '0'}">
  44.       <a href="#" onclick="changeRecord('${Page.previousIndex}')">上一页</a> 
  45.       </c:when>
  46.       <c:otherwise>
  47.       上一页 
  48.       </c:otherwise>
  49.      </c:choose>
  50.      </td>
  51.      <td>
  52.      <c:choose>
  53.       <c:when test="${Page.nextIndex>Page.startIndex}">
  54.     
  55.        <a href="#" onclick="changeRecord('${Page.nextIndex}')">下一页</a> 
  56.       </c:when>
  57.       <c:otherwise>
  58.       下一页
  59.     
  60.       </c:otherwise>
  61.      </c:choose>
  62.      </td>
  63.      <td>
  64.      <c:choose>
  65.       <c:when test="${Page.lastIndex eq Page.startIndex}">
  66.      
  67.       末页
  68.       </c:when>
  69.       <c:otherwise>
  70.       <a href="#" onclick="changeRecord('${Page.lastIndex}')"> 末页</a>   
  71.       </c:otherwise>
  72.      </c:choose>
  73.      </td>
  74.      <td>
  75.      <input type="text" name="page" id="page" size="3" onkeyup="this.value=this.value.replace(/[^/d]/g,'')" >
  76.      <input type="button" value="GO" onclick="gopage('${Page.lastIndex}')">
  77.      当前第${Page.currentPage}/${Page.pageCount}页
  78.      </td>
  79. </tr>
  80. </table>
  81.    </c:if>

用法:

在hibetnate DAO

  1. public   SearchResult   findPageByQuery(String hql ,Page page,int totalCount)   {   
  2.    
  3.     Query   query   =   getSession().createQuery(hql);
  4.     query.setFirstResult(page.getStartIndex());   
  5.     query.setMaxResults(page.getPageSize());   
  6.     List list =   query.list();    
  7.    
  8.     page.setTotalCount(totalCount); 
  9.     SearchResult sr= new SearchResult();
  10.     sr.setList(list);
  11.     sr.setPage(page);
  12.     return sr;   
  13. }

ex:

在action里面

  1. Page page=null;
  2.     String startIndex = request.getParameter("startIndex");
  3.     if(StringUtils.isNotBlank(startIndex)){
  4.      page=new Page(10,Integer.valueOf(startIndex));
  5.     }  
  6.     else{
  7.      page= new Page(100);
  8.     } 
  9.     SearchResult sr = null;
  10.     try{
  11.      sr = sysLogService.getAllSysLogs(page);
  12.     }catch(Exception e){
  13.      e.printStackTrace();
  14.     }
  15.    
  16.     request.setAttribute("searchSysLog", sr.getList());
  17.     request.setAttribute("Page", sr.getPage());

在dao里面

  1. public SearchResult getAllSysLogs(Page page) {
  2.    String hql = "From Lovesyslog ";
  3.    String numHql = "select count(*) From Lovesyslog";
  4.    SearchResult sr =null;
  5.    try{   
  6.     List list = this.getHibernateTemplate().find(numHql);
  7.     Long number = (Long)list.get(0); 
  8.     sr = this.findPageByQuery(hql, page, number.intValue());
  9.    }catch (Exception e) {
  10.     e.printStackTrace(); 
  11.    }
  12.    return sr;    
  13. }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值