随笔 - 6  文章 - 129  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 815440
  • 排名 - 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 阅读(747) 评论(0)  编辑  收藏 所属分类: hibernatepagination

只有注册用户登录后才能发表评论。


网站导航: