日期:2014-05-20 浏览次数:20810 次
public abstract class AbstBaseQuerySpec implements HibernateCallback, Serializable { /** 当前页码数 */ protected Paginate paginate = new Paginate(); /** 是否需要分页支持 */ protected boolean paginary = true; /** * 重置查询条件 */ public void resetSpec() { paginate.reset(); reset(); } public Object doInHibernate(Session session) throws HibernateException, SQLException { return null; } protected void applyNamedParameterToQuery(Query queryObject, String paramName, Object value) throws HibernateException { if (value instanceof Collection) { queryObject.setParameterList(paramName, (Collection) value); } else if (value instanceof Object[]) { queryObject.setParameterList(paramName, (Object[]) value); } else { queryObject.setParameter(paramName, value); } } /** * reset spec */ protected abstract void reset(); /** * 执行查询后,通过此方法取得分页信息 * * @return */ public Paginate getPaginate() { return paginate; } /** * @param paginate * The paginate to set. */ public void setPaginate(Paginate paginate) { this.paginate = paginate; } /** * @param paginary * The paginary to set. */ public void setPaginary(boolean paginary) { this.paginary = paginary; } } // 基于Hibernate查询规则限定条件 public abstract class AbstQueryStringSpec extends AbstBaseQuerySpec { /** */ private Map<String, Object> params = new HashMap<String, Object>(); /** * Named hql queryName * * @return */ public abstract String queryString(); /** * * @param session * @return * @throws HibernateException * @throws SQLException * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session) */ public Object doInHibernate(Session session) throws HibernateException, SQLException { params.clear(); Query query = session.createQuery(queryString()); Iterator<Map.Entry<String, Object>> pIt = params.entrySet().iterator(); while (pIt.hasNext()) { Map.Entry<String, Object> entry = pIt.next(); applyNamedParameterToQuery(query, entry.getKey(), entry.getValue()); } if (paginary) { List total = query.list(); int totalRows = total.size(); paginate.setTotalRow(totalRows); query.setFirstResult(paginate.getStartIndex()); query.setMaxResults(paginate.getPageLine()); } List result = query.list(); return result; } /** * 设置Query接口命名参数 * * @param name * @param value */ protected void setParam(String name, Object value) { this.params.put(name, value); } } /** * 功能概述:<br> * 机柜统计查询类 * * 以下是查询类,具体查询数据时 * */ //public class CabinetStatisticSpec extends AbstQueryStringSpec { /** 机房ID */ private Integer compLocaId; @Override public String queryString() { // hql 查询 StringBuffer hql = new StringBuffer( "select cl.name, " + "sum(case when nc.useHigh=null or nc.useHigh=0 then 1 else 0 end) as unusedCabinet, " + "sum(case when nc.useHigh>0 and nc.useHigh<nc.height then 1 else 0 end) as nofullCabinet, " + "sum(case when nc.useHigh>=nc.height then 1 else 0 end) as fullCabinet, " + "count(*) as totalCabinet " + "FROM NetworkCabinets nc,ComputerLocation cl " + "WHERE nc.compLocaId=cl.id "); if (QuerySpecUtil.valid(compLocaId)) { hql.append(" AND nc.compLocaId=:compLocaId "); this.setParam("compLocaId", compLocaId); } hql.append(" group by cl.name "); return hql.toString(); @Override protected void reset() { compLocaId = null; } public Integer getCompLocaId() { return compLocaId; } public void setCompLocaId(Integer compLocaId) { this.compLocaId = compLocaId; } } spec在struts2 action中作为属性,调用service时作为参数,service中直接用DAO查询,参数就是从action中传过来的spec 我感觉这个设计还是很好的,遂给出部分代码。希望对大家有一点参考作用