<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    分享一個spring+hibernate的通用分頁類

    Posted on 2007-11-13 10:50 flustar 閱讀(5364) 評論(6)  編輯  收藏 所屬分類: Spring
        時間過得真快,已經半年沒有更新自己的博客了。    好了,言歸正傳。大家都知道網上廣為流傳的一個分頁類是:PaginationSupport.java其源代碼如下:
        

    public class PaginationSupport{

     public final static int PAGESIZE = 30;

     private int pageSize = PAGESIZE;

     private List items;

     private int totalCount;

     private int[] indexes = new int[0];

     private int startIndex = 0;

     public PaginationSupport(List items, int totalCount) {
      setPageSize(PAGESIZE);
      setTotalCount(totalCount);
      setItems(items);
      setStartIndex(0);
     }

     public PaginationSupport(List items, int totalCount, int startIndex) {
      setPageSize(PAGESIZE);
      setTotalCount(totalCount);
      setItems(items);
      setStartIndex(startIndex);
     }

     public PaginationSupport(List items, int totalCount, int pageSize,
       int startIndex) {
      setPageSize(pageSize);
      setTotalCount(totalCount);
      setItems(items);
      setStartIndex(startIndex);
     }

     public List getItems() {
      return items;
     }

     public void setItems(List items) {
      this.items = items;
     }

     public int getPageSize() {
      return pageSize;
     }

     public void setPageSize(int pageSize) {
      this.pageSize = pageSize;
     }

     public int getTotalCount() {
      return totalCount;
     }

     public void setTotalCount(int totalCount) {
      if (totalCount > 0) {
       this.totalCount = totalCount;
       int count = totalCount / pageSize;
       if (totalCount % pageSize > 0)
        count++;
       indexes = new int[count];
       for (int i = 0; i < count; i++) {
        indexes[i] = pageSize * i;
       }
      } else {
       this.totalCount = 0;
      }
     }

     public int[] getIndexes() {
      return indexes;
     }

     public void setIndexes(int[] indexes) {
      this.indexes = indexes;
     }

     public int getStartIndex() {
      return startIndex;
     }

     public void setStartIndex(int startIndex) {
      if (totalCount <= 0)
       this.startIndex = 0;
      else if (startIndex >= totalCount)
       this.startIndex = indexes[indexes.length - 1];
      else if (startIndex < 0)
       this.startIndex = 0;
      else {
       this.startIndex = indexes[startIndex / pageSize];
      }
     }

     public int getNextIndex() {
      int nextIndex = getStartIndex() + pageSize;
      if (nextIndex >= totalCount)
       return getStartIndex();
      else
       return nextIndex;
     }

     public int getPreviousIndex() {
      int previousIndex = getStartIndex() - pageSize;
      if (previousIndex < 0)
       return 0;
      else
       return previousIndex;
     }

     public int getPageCount() {
      int count = totalCount / pageSize;
      if (totalCount % pageSize > 0)
       count++;
      return count;
     }

     public int getCurentPageNum() {
      return getStartIndex() / pageSize + 1;
     }

    }
    在這個分頁類中設定了每頁要顯示的記錄數以及開始索引,如果用普通的jsp來取這個分頁類的數據還可以,但是使用spring+hibernate這種架構就顯得比較麻煩(原因是spring MVC返回的是一個 PaginationSupport的對象,使用jstl作為前端顯示的話,會在jsp頁面中摻雜大量的計算,像下一頁索引,共多少條記錄,當前第幾頁,共多少頁等等會使jsp很難維護)下面是對這個類的改進:

    public class  PaginationSupport {
     public final static int PAGESIZE = 30;

     private int pageSize = PAGESIZE;
     
     private int totalCount;

     private int currentPage;

     private int startIndex;
     
     private int[] indexes = new int[0];
     
     private int nextIndex;

     private int previousIndex;

     private int pageCount;

     private List items;
     
     private int lastIndex;
     
     public  PaginationSupport(int pageSize,
       int startIndex) {
      setPageSize(pageSize);
      setStartIndex(startIndex);
      
     }

     public  PaginationSupport(List items, int totalCount) {
      setPageSize(PAGESIZE);
      setTotalCount(totalCount);
      setItems(items);
      setStartIndex(0);
     
     }

     public  PaginationSupport(List items, int totalCount, int startIndex) {
      setPageSize(PAGESIZE);
      setTotalCount(totalCount);
      setItems(items);
      setStartIndex(startIndex);
      
     }

     public  PaginationSupport(List items, int totalCount, int pageSize,
       int startIndex) {
      setPageSize(pageSize);
      setTotalCount(totalCount);
      setItems(items);
      setStartIndex(startIndex);
      
     }

     
     public void setTotalCount(int totalCount) {
      if (totalCount > 0) {
       this.totalCount = totalCount;
       int count = totalCount / pageSize;
       if (totalCount % pageSize > 0)
        count++;
       indexes = new int[count];
       for (int i = 0; i < count; i++) {
        indexes[i] = pageSize * i;
       }
        } else {
       this.totalCount = 0;
      }
     }
     public int getTotalCount() {
      return totalCount;
     }
     public void setIndexes(int[] indexes) {
      this.indexes = indexes;
     }
     public int[] getIndexes() {
      return indexes;
     }

     
     public void setStartIndex(int startIndex) {
      if (totalCount <= 0)
       this.startIndex = 0;
      else if (startIndex >= totalCount)
       this.startIndex = indexes[indexes.length - 1];
      else if (startIndex < 0)
       this.startIndex = 0;
      else {
       this.startIndex = indexes[startIndex / pageSize];
      }
       }
     public int getStartIndex() {
      return startIndex;
     }

     
     public void setNextIndex(int nextIndex) {
      this.nextIndex = nextIndex;
     }
     public int getNextIndex() {
      int nextIndex = getStartIndex() + pageSize;
      if (nextIndex >= totalCount)
       return getStartIndex();
      else
       return nextIndex;
     }
     public void setPreviousIndex(int previousIndex) {
      this.previousIndex = previousIndex;
     }
     
     public int getPreviousIndex() {
      int previousIndex = getStartIndex() - pageSize;
      if (previousIndex < 0)
       return 0;
      else
       return previousIndex;
     }
     public void setPageCount(int pageCount) {
      this.pageCount = pageCount;
     }
     public int getPageCount() {
      int count = totalCount / pageSize;
      if (totalCount % pageSize > 0)
       count++;
      return count;
     }
     

     public int getCurrentPage() {
      return getStartIndex() / pageSize + 1;
     }

     public void setCurrentPage(int currentPage) {
      this.currentPage = currentPage;
     }

     public void setLastIndex(int lastIndex) {
      this.lastIndex =lastIndex ;
     }
     public int getLastIndex() {
      return indexes[indexes.length-1];
     }

     
     public int getPageSize() {
      return pageSize;
     }

     public void setPageSize(int pageSize) {
      this.pageSize = pageSize;
     }

     

     public List getItems() {
      return items;
     }

     public void setItems(List items) {
      this.items = items;
     }


    }
    以上是分頁的封裝類,下面是支持分頁查詢的方法:
    1)
    public PaginationSupport findPageByCriteria(
       final DetachedCriteria detachedCriteria, final int pageSize,
       final int startIndex) {
      return (PaginationSupport) getHibernateTemplate().execute(
        new HibernateCallback() {
         public Object doInHibernate(Session session)
           throws HibernateException {
          Criteria criteria = detachedCriteria
            .getExecutableCriteria(session);
          int totalCount = ((Integer) criteria.setProjection(
            Projections.rowCount()).uniqueResult())
            .intValue();
          criteria.setProjection(null);
          List items = criteria.setFirstResult(startIndex)
            .setMaxResults(pageSize).list();
          PaginationSupport ps = new PaginationSupport(items,
            totalCount, pageSize, startIndex);
          return ps;
         }
        }, true);
     }
    2)
    public  PaginationSupport findPageByQuery( final  String hsql,  final int pageSize,final int startIndex){
         return (PaginationSupport)getHibernateTemplate().execute(
         new  HibernateCallback() {
           public  Object doInHibernate(Session session)  throws  HibernateException, SQLException {
                 Query query  =  session.createQuery(hsql);
                 int totalCount=query.list().size();
                 query.setFirstResult(startIndex);
                 query.setMaxResults(pageSize);
                 List items  = query.list();
              PaginationSupport ps = new PaginationSupport(items,
           totalCount, pageSize, startIndex);
              return ps;
                
                 }
           },true);
      }
    你也許會問分頁查詢為什么會提供兩個方法,這兩個方法有區別嗎?其實這兩個方法并無本質區別,DetachedCriteria 也是構造查詢語句的與Query功能一致,但是它提供了更加面向對象的方法來寫hsql語句。一般人們都傾向第一種方法,但是這種方法并不通用,它有一種查詢并不支持,那就是當你要查詢的對象并不是一個單一對象的話(例如 你在數據庫中有兩個表,一個是user,另一個是userinfo,這兩個表所對應的對象在hiberante中被指定為共享主鍵的話,在執行查詢的時候就會報類型轉換異常,原因是查詢出來的對象并不是user而是一個包含user 和userinfo的Object,你若強制把它轉換成user類型,肯定會出錯),這時你不得不采用第二個方法。當然這只是我個人見解,也許還有地方說的不是很準確,希望大家多多批評指正。
    最后是這個分頁類的前臺顯示源代碼:
    <%@ page language="java" contentType="text/html; charset=gbk"
        pageEncoding="GBK"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <link type="text/css" rel="stylesheet" href="../css/panel.css">
        <title>顯示所有用戶</title>
      </head>
     
      <body>
        <div style="margin:20px auto 30px; width:70%;"><a href="index.jsp" class="btn2">返回首頁</a></div>
        <div style="margin:10px auto 0; width:70%;">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <caption>
          顯示所有用戶
        </caption>
        <tr>
          <td>用戶ID</td>
          <td>用戶名</td>
       <td>用戶昵稱</td>
          <td>電子郵件</td>
          <td>注冊時間</td>
          <td>詳細信息</td>
          <td>用戶充值記錄</td>
          <td>用戶定制服務信息</td>
        </tr>
    <c:forEach var="user" items="${userPage.items}">
     <tr>
       <td>${user.intId}</td>
          <td>${user.username}</td>
          <td>${user.name}</td>
          <td>${user.email}</td>
          <td><fmt:formatDate value='${user.creationTime}' pattern='yyyy-MM-dd HH:mm' /></td>
       <td><a href="user_getdetailUser.ado?userId=${user.intId}" class="btn">詳細信息</a></td>
       <td><a href="orderService_getUserAccountAdds.ado?userId=${user.intId}" class="btn">用戶充值記錄</a></td>
       <td><a href="orderService_getUserChargeItems.ado?userId=${user.intId}" class="btn">用戶定制服務信息</a></td>
     </tr>
    </c:forEach>
      </table>
       <c:if test="${!empty userPage}">
         共${userPage.totalCount}記錄
         <c:choose>
          <c:when test="${userPage.startIndex ne '0'}">
           <a href="user_getPage.ado?startIndex=0">首頁</a>
          </c:when>
          <c:otherwise>
           首頁
          </c:otherwise>
         </c:choose>
         <c:choose>
          <c:when test="${userPage.previousIndex lt userPage.startIndex}">
           <a href="user_getPage.ado?startIndex=${userPage.previousIndex }">上一頁</a>
          </c:when>
          <c:otherwise>
           上一頁
          </c:otherwise>
         </c:choose>
         <c:choose>
          <c:when test="${userPage.nextIndex>userPage.startIndex}">
           <a href="user_getPage.ado?startIndex=${userPage.nextIndex}">下一頁</a>
          </c:when>
          <c:otherwise>
           下一頁
          </c:otherwise>
         </c:choose>
         <c:choose>
          <c:when test="${userPage.lastIndex eq userPage.startIndex}">
           最后一頁
          </c:when>
          <c:otherwise>
           <a href="user_getPage.ado?startIndex=${userPage.lastIndex}">最后一頁</a>
          </c:otherwise>
         </c:choose>
         每頁顯示${userPage.pageSize}條記錄
         當前第${userPage.currentPage }/${userPage.pageCount}頁
      </c:if>
        </div>
      </body>
    </html>


    Feedback

    # re: 分享一個spring+hibernate的通用分頁類  回復  更多評論   

    2007-11-13 19:01 by 千里冰封
    呵呵,有點意思

    # re: 分享一個spring+hibernate的通用分頁類  回復  更多評論   

    2007-11-13 19:51 by addday
    springside 里,早就有了

    # re: 分享一個spring+hibernate的通用分頁類  回復  更多評論   

    2008-03-18 11:26 by huangcq
    煩請樓主發一個包括所有源碼的壓縮包過來

    # re: 分享一個spring+hibernate的通用分頁類  回復  更多評論   

    2008-03-18 16:42 by flustar
    @huangcq
    不好意思 源碼都沒了 電腦重裝 不小心給抹去了

    # re: 分享一個spring+hibernate的通用分頁類  回復  更多評論   

    2008-04-12 08:44 by 人面桃花
    呵呵 很好啊

    # re: 分享一個spring+hibernate的通用分頁類  回復  更多評論   

    2015-10-06 23:34 by 暗夜百家
    里面userPage是什么

    posts - 146, comments - 143, trackbacks - 0, articles - 0

    Copyright © flustar

    主站蜘蛛池模板: 日本亚洲免费无线码| 99re免费99re在线视频手机版| 在人线av无码免费高潮喷水| 亚洲欧洲日产国产最新| 在线永久看片免费的视频| 亚洲免费在线观看视频| 在线天堂免费观看.WWW| 亚洲heyzo专区无码综合| 国产午夜鲁丝片AV无码免费| 无套内谢孕妇毛片免费看看| 国产亚洲自拍一区| 免费无码成人AV在线播放不卡| 亚洲成av人片不卡无码| 毛片免费视频在线观看| 国产精品亚洲а∨无码播放不卡 | 国产AV无码专区亚洲AV麻豆丫| 在线观看91精品国产不卡免费| 国产精品亚洲一区二区三区久久| 亚洲性日韩精品国产一区二区| a毛片在线免费观看| 亚洲欧洲日本国产| 手机看片久久国产免费| h视频免费高清在线观看| 亚洲人成网站影音先锋播放| 99久久精品日本一区二区免费| 亚洲国产欧美一区二区三区| 精品亚洲视频在线观看 | 日韩大片免费观看视频播放| 亚洲乱码中文字幕久久孕妇黑人| **毛片免费观看久久精品| 亚洲AV无码成人网站在线观看| 亚洲欧洲国产精品香蕉网| 久久笫一福利免费导航| 免费的黄网站男人的天堂| 亚洲av成人无码久久精品 | 91精品国产免费入口| 亚洲中文字幕乱码AV波多JI| 中文字幕亚洲无线码| 欧亚精品一区三区免费| 老司机精品免费视频| 久久亚洲国产最新网站|