konhon

忘掉過去,展望未來。找回自我,超越自我。
逃避不一定躲的过, 面对不一定最难过, 孤单不一定不快乐, 得到不一定能长久, 失去不一定不再拥有, 可能因为某个理由而伤心难过, 但我却能找个理由让自己快乐.

Google

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

分页支持类:
package com.javaeye.common.util; 

import java.util.List; 

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; 
        } 

}

抽象业务类
java代码: 
/** 
* Created on 2005-7-12 
*/ 
package com.javaeye.common.business; 

import java.io.Serializable; 
import java.util.List; 

import org.hibernate.Criteria; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.criterion.DetachedCriteria; 
import org.hibernate.criterion.Projections; 
import org.springframework.orm.hibernate3.HibernateCallback; 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 

import com.javaeye.common.util.PaginationSupport; 

public abstract class AbstractManager extends HibernateDaoSupport { 

        
private boolean cacheQueries = false

        
private String queryCacheRegion; 

        
public void setCacheQueries(boolean cacheQueries) { 
                
this.cacheQueries = cacheQueries; 
        } 

        
public void setQueryCacheRegion(String queryCacheRegion) { 
                
this.queryCacheRegion = queryCacheRegion; 
        } 

        
public void save(final Object entity) { 
                getHibernateTemplate().save(entity); 
        } 

        
public void persist(final Object entity) { 
                getHibernateTemplate().save(entity); 
        } 

        
public void update(final Object entity) { 
                getHibernateTemplate().update(entity); 
        } 

        
public void delete(final Object entity) { 
                getHibernateTemplate().delete(entity); 
        } 

        
public Object load(final Class entity, final Serializable id) { 
                
return getHibernateTemplate().load(entity, id); 
        } 

        
public Object get(final Class entity, final Serializable id) { 
                
return getHibernateTemplate().get(entity, id); 
        } 

        
public List findAll(final Class entity) { 
                
return getHibernateTemplate().find("from " + entity.getName()); 
        } 

        
public List findByNamedQuery(final String namedQuery) { 
                
return getHibernateTemplate().findByNamedQuery(namedQuery); 
        } 

        
public List findByNamedQuery(final String query, final Object parameter) { 
                
return getHibernateTemplate().findByNamedQuery(query, parameter); 
        } 

        
public List findByNamedQuery(final String query, final Object[] parameters) { 
                
return getHibernateTemplate().findByNamedQuery(query, parameters); 
        } 

        
public List find(final String query) { 
                
return getHibernateTemplate().find(query); 
        } 

        
public List find(final String query, final Object parameter) { 
                
return getHibernateTemplate().find(query, parameter); 
        } 

        
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) { 
                
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0); 
        } 

        
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) { 
                
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex); 
        } 

        
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); 
        } 

        
public List findAllByCriteria(final DetachedCriteria detachedCriteria) { 
                
return (List) getHibernateTemplate().execute(new HibernateCallback() { 
                        
public Object doInHibernate(Session session) throws HibernateException { 
                                Criteria criteria 
= detachedCriteria.getExecutableCriteria(session); 
                                
return criteria.list(); 
                        } 
                }, 
true); 
        } 

        
public int getCountByCriteria(final DetachedCriteria detachedCriteria) { 
                Integer count 
= (Integer) getHibernateTemplate().execute(new HibernateCallback() { 
                        
public Object doInHibernate(Session session) throws HibernateException { 
                                Criteria criteria 
= detachedCriteria.getExecutableCriteria(session); 
                                
return criteria.setProjection(Projections.rowCount()).uniqueResult(); 
                        } 
                }, 
true); 
                
return count.intValue(); 
        } 

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

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

 

 

posted on 2006-04-06 21:37 konhon 优华 阅读(1524) 评论(1)  编辑  收藏 所属分类: Hibernate

Feedback

# re: 应用Hibernate3的DetachedCriteria实现分页查询 2014-02-26 07:09 qinxike
学习了,在findPageByCriteria()中,还以为共享一个Critreria行不通了, criteria.setProjection(null); 这行给我启发。谢谢  回复  更多评论
  


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


网站导航: