隨筆 - 6  文章 - 129  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827218
          • 排名 - 49

          最新評論

          閱讀排行榜

          評論排行榜

          Hibernate3提供了DetachedCriteria,使得我們可以在Web層構造detachedCriteria,然后調用業務層Bean,進行動態條件查詢,根據這一功能,我設計了通用的抽象Bean基類和分頁類支持,代碼來自于Quake Wang的javaeye-core包的相應類,然后又做了很多修改。

          分頁支持類:

          java 代碼
          1. package com.javaeye.common.util;   
          2.   
          3. import java.util.List;   
          4.   
          5. public class PaginationSupport {   
          6.   
          7.     public final static int PAGESIZE = 30;   
          8.   
          9.     private int pageSize = PAGESIZE;   
          10.   
          11.     private List items;   
          12.   
          13.     private int totalCount;   
          14.   
          15.     private int[] indexes = new int[0];   
          16.   
          17.     private int startIndex = 0;   
          18.   
          19.     public PaginationSupport(List items, int totalCount) {   
          20.         setPageSize(PAGESIZE);   
          21.                 setTotalCount(totalCount);   
          22.         setItems(items);           
          23.         setStartIndex(0);   
          24.     }   
          25.   
          26.     public PaginationSupport(List items, int totalCount, int startIndex) {   
          27.                 setPageSize(PAGESIZE);   
          28.         setTotalCount(totalCount);   
          29.         setItems(items);           
          30.         setStartIndex(startIndex);   
          31.     }   
          32.   
          33.     public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {   
          34.                 setPageSize(pageSize);   
          35.         setTotalCount(totalCount);   
          36.         setItems(items);   
          37.         setStartIndex(startIndex);   
          38.     }   
          39.   
          40.     public List getItems() {   
          41.         return items;   
          42.     }   
          43.   
          44.     public void setItems(List items) {   
          45.         this.items = items;   
          46.     }   
          47.   
          48.     public int getPageSize() {   
          49.         return pageSize;   
          50.     }   
          51.   
          52.     public void setPageSize(int pageSize) {   
          53.         this.pageSize = pageSize;   
          54.     }   
          55.   
          56.     public int getTotalCount() {   
          57.         return totalCount;   
          58.     }   
          59.   
          60.     public void setTotalCount(int totalCount) {   
          61.         if (totalCount > 0) {   
          62.             this.totalCount = totalCount;   
          63.             int count = totalCount / pageSize;   
          64.             if (totalCount % pageSize > 0)   
          65.                 count++;   
          66.             indexes = new int[count];   
          67.             for (int i = 0; i < count; i++) {   
          68.                 indexes[i] = pageSize * i;   
          69.             }   
          70.         } else {   
          71.             this.totalCount = 0;   
          72.         }   
          73.     }   
          74.   
          75.     public int[] getIndexes() {   
          76.         return indexes;   
          77.     }   
          78.   
          79.     public void setIndexes(int[] indexes) {   
          80.         this.indexes = indexes;   
          81.     }   
          82.   
          83.     public int getStartIndex() {   
          84.         return startIndex;   
          85.     }   
          86.   
          87.     public void setStartIndex(int startIndex) {   
          88.         if (totalCount <= 0)   
          89.             this.startIndex = 0;   
          90.         else if (startIndex >= totalCount)   
          91.             this.startIndex = indexes[indexes.length - 1];   
          92.         else if (startIndex < 0)   
          93.             this.startIndex = 0;   
          94.         else {   
          95.             this.startIndex = indexes[startIndex / pageSize];   
          96.         }   
          97.     }   
          98.   
          99.     public int getNextIndex() {   
          100.         int nextIndex = getStartIndex() + pageSize;   
          101.         if (nextIndex >= totalCount)   
          102.             return getStartIndex();   
          103.         else  
          104.             return nextIndex;   
          105.     }   
          106.   
          107.     public int getPreviousIndex() {   
          108.         int previousIndex = getStartIndex() - pageSize;   
          109.         if (previousIndex < 0)   
          110.             return 0;   
          111.         else  
          112.             return previousIndex;   
          113.     }   
          114.   
          115. }  

           

          抽象業務類

          java 代碼
          1. /**  
          2.  * Created on 2005-7-12  
          3.  */  
          4. package com.javaeye.common.business;   
          5.   
          6. import java.io.Serializable;   
          7. import java.util.List;   
          8.   
          9. import org.hibernate.Criteria;   
          10. import org.hibernate.HibernateException;   
          11. import org.hibernate.Session;   
          12. import org.hibernate.criterion.DetachedCriteria;   
          13. import org.hibernate.criterion.Projections;   
          14. import org.springframework.orm.hibernate3.HibernateCallback;   
          15. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
          16.   
          17. import com.javaeye.common.util.PaginationSupport;   
          18.   
          19. public abstract class AbstractManager extends HibernateDaoSupport {   
          20.   
          21.     private boolean cacheQueries = false;   
          22.   
          23.     private String queryCacheRegion;   
          24.   
          25.     public void setCacheQueries(boolean cacheQueries) {   
          26.         this.cacheQueries = cacheQueries;   
          27.     }   
          28.   
          29.     public void setQueryCacheRegion(String queryCacheRegion) {   
          30.         this.queryCacheRegion = queryCacheRegion;   
          31.     }   
          32.   
          33.     public void save(final Object entity) {   
          34.         getHibernateTemplate().save(entity);   
          35.     }   
          36.   
          37.     public void persist(final Object entity) {   
          38.         getHibernateTemplate().save(entity);   
          39.     }   
          40.   
          41.     public void update(final Object entity) {   
          42.         getHibernateTemplate().update(entity);   
          43.     }   
          44.   
          45.     public void delete(final Object entity) {   
          46.         getHibernateTemplate().delete(entity);   
          47.     }   
          48.   
          49.     public Object load(final Class entity, final Serializable id) {   
          50.         return getHibernateTemplate().load(entity, id);   
          51.     }   
          52.   
          53.     public Object get(final Class entity, final Serializable id) {   
          54.         return getHibernateTemplate().get(entity, id);   
          55.     }   
          56.   
          57.     public List findAll(final Class entity) {   
          58.         return getHibernateTemplate().find("from " + entity.getName());   
          59.     }   
          60.   
          61.     public List findByNamedQuery(final String namedQuery) {   
          62.         return getHibernateTemplate().findByNamedQuery(namedQuery);   
          63.     }   
          64.   
          65.     public List findByNamedQuery(final String query, final Object parameter) {   
          66.         return getHibernateTemplate().findByNamedQuery(query, parameter);   
          67.     }   
          68.   
          69.     public List findByNamedQuery(final String query, final Object[] parameters) {   
          70.         return getHibernateTemplate().findByNamedQuery(query, parameters);   
          71.     }   
          72.   
          73.     public List find(final String query) {   
          74.         return getHibernateTemplate().find(query);   
          75.     }   
          76.   
          77.     public List find(final String query, final Object parameter) {   
          78.         return getHibernateTemplate().find(query, parameter);   
          79.     }   
          80.   
          81.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {   
          82.         return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);   
          83.     }   
          84.   
          85.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {   
          86.         return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);   
          87.     }   
          88.   
          89.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,   
          90.             final int startIndex) {   
          91.         return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {   
          92.             public Object doInHibernate(Session session) throws HibernateException {   
          93.                 Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
          94.                 int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();   
          95.                 criteria.setProjection(null);   
          96.                 List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();   
          97.                 PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);   
          98.                 return ps;   
          99.             }   
          100.         }, true);   
          101.     }   
          102.   
          103.     public List findAllByCriteria(final DetachedCriteria detachedCriteria) {   
          104.         return (List) getHibernateTemplate().execute(new HibernateCallback() {   
          105.             public Object doInHibernate(Session session) throws HibernateException {   
          106.                 Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
          107.                 return criteria.list();   
          108.             }   
          109.         }, true);   
          110.     }   
          111.   
          112.     public int getCountByCriteria(final DetachedCriteria detachedCriteria) {   
          113.         Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {   
          114.             public Object doInHibernate(Session session) throws HibernateException {   
          115.                 Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
          116.                 return criteria.setProjection(Projections.rowCount()).uniqueResult();   
          117.             }   
          118.         }, true);   
          119.         return count.intValue();   
          120.     }   
          121. }   

           

          用戶在web層構造查詢條件detachedCriteria,和可選的startIndex,調用業務bean的相應findByCriteria方法,返回一個PaginationSupport的實例ps。

          ps.getItems()得到已分頁好的結果集
          ps.getIndexes()得到分頁索引的數組
          ps.getTotalCount()得到總結果數
          ps.getStartIndex()當前分頁索引
          ps.getNextIndex()下一頁索引
          ps.getPreviousIndex()上一頁索引

          文章出處:http://www.javaeye.com/topic/14657



          posted on 2007-10-21 21:51 Ke 閱讀(769) 評論(0)  編輯  收藏 所屬分類: hibernatepagination
          主站蜘蛛池模板: 佛学| 云霄县| 古浪县| 朝阳区| 周至县| 洛阳市| 天镇县| 青神县| 元阳县| 清原| 库伦旗| 中方县| 玛多县| 东山县| 西丰县| 巴林右旗| 岳西县| 阳高县| 昌平区| 锦屏县| 犍为县| 江达县| 信阳市| 富裕县| 昌江| 武城县| 土默特右旗| 辽宁省| 绿春县| 巴林左旗| 芮城县| 广饶县| 内丘县| 民勤县| 阿拉善右旗| 红桥区| 徐州市| 连江县| 新乡市| 进贤县| 新建县|