利用hibernate的DetachedCriteria进行分页

来源:互联网 发布:日本进口美工刀片 编辑:程序博客网 时间:2024/05/11 11:34

Hibernate3提供了DetachedCriteria,使得我们可以在Web层构造detachedCriteria,然后调用业务层Bean,进行动态条件查询,根据这一功能,我设计了通用的抽象Bean基类和分页类支持,代码来自于Quake Wang的javaeye-core包的相应类,然后又做了很多修改。
  分页支持类:
   

  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.   //设置总的记录数,总页数(count)  ,每一页的开始记录号
  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.   


抽象业务类(我们的类要继承这个抽象类, 以便使用里面的类进行分类)

  1. package com.javaeye.common.business;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. import org.hibernate.Criteria;  
  7. import org.hibernate.HibernateException;  
  8. import org.hibernate.Session;  
  9. import org.hibernate.criterion.DetachedCriteria;  
  10. import org.hibernate.criterion.Projections;  
  11. import org.springframework.orm.hibernate3.HibernateCallback;  
  12. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  13.   
  14. import com.javaeye.common.util.PaginationSupport;  
  15.   
  16. public abstract class AbstractManager extends HibernateDaoSupport {  
  17.   
  18.     private boolean cacheQueries = false;  
  19.   
  20.     private String queryCacheRegion;  
  21.   
  22.     public void setCacheQueries(boolean cacheQueries) {  
  23.         this.cacheQueries = cacheQueries;  
  24.     }  
  25.   
  26.     public void setQueryCacheRegion(String queryCacheRegion) {  
  27.         this.queryCacheRegion = queryCacheRegion;  
  28.     }  
  29.    //保存
  30.     public void save(final Object entity) {  
  31.         getHibernateTemplate().save(entity);  
  32.     }  
  33.   
  34.     public void persist(final Object entity) {  
  35.         getHibernateTemplate().save(entity);  
  36.     }  
  37.   //更新
  38.     public void update(final Object entity) {  
  39.         getHibernateTemplate().update(entity);  
  40.     }  
  41.   //删除
  42.     public void delete(final Object entity) {  
  43.         getHibernateTemplate().delete(entity);  
  44.     }  
  45.    //加载
  46.     public Object load(final Class entity, final Serializable id) {  
  47.         return getHibernateTemplate().load(entity, id);  
  48.     }  
  49.   
  50.     public Object get(final Class entity, final Serializable id) {  
  51.         return getHibernateTemplate().get(entity, id);  
  52.     }  
  53.    //查询全部
  54.     public List findAll(final Class entity) {  
  55.         return getHibernateTemplate().find("from " + entity.getName());  
  56.     }  
  57.    //通过名字查询
  58.     public List findByNamedQuery(final String namedQuery) {  
  59.         return getHibernateTemplate().findByNamedQuery(namedQuery);  
  60.     }  
  61.   //通过参数动态查询
  62.     public List findByNamedQuery(final String query, final Object parameter) {  
  63.         return getHibernateTemplate().findByNamedQuery(query, parameter);  
  64.     }  
  65.   
  66.     public List findByNamedQuery(final String query, final Object[] parameters) {  
  67.         return getHibernateTemplate().findByNamedQuery(query, parameters);  
  68.     }  
  69.    
  70.     public List find(final String query) {  
  71.         return getHibernateTemplate().find(query);  
  72.     }  
  73.   
  74.     public List find(final String query, final Object parameter) {  
  75.         return getHibernateTemplate().find(query, parameter);  
  76.     }  
  77.   //以下是分页查询
  78.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) { 
  79.              //通过detachedCriteri,一页显示记录数,   当前是第几页查询(0)
  80.         return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);  
  81.     }  
  82.   
  83.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteriafinal int startIndex) {  
  84.            //用户指定当前为第几页
  85.            return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);  
  86.     }  
  87.   
  88.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteriafinal int pageSize,  
  89.             final int startIndex) { 
  90.            //用户同时指定  每页显示的记录数,和  当前页为第几页
  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.     }  

 用户在web层构造查询条件detachedCriteria,和可选的startIndex,调用业务bean的相应findByCriteria方法,返回一个PaginationSupport的实例ps。

ps.getItems()得到已分页好的结果集
ps.getIndexes()得到分页索引的数组
ps.getTotalCount()得到总结果数
ps.getStartIndex()当前分页索引
ps.getNextIndex()下一页索引
ps.getPreviousIndex()上一页索引

原创粉丝点击