---Dao方法: public List query(String hql, String firstResult, String maxResults) { Query query = this.getHibernateTemplate().getSessionFactory() .getCurrentSession().createQuery(hql); query.setFirstResult(new Integer(firstResult).intValue()); query.setMaxResults(new Integer(maxResults).intValue()); return query.list(); } //对query中的参数赋值 protected void setQueryParams(Query query, Object[] queryParams){ if(queryParams!=null && queryParams.length>0){ for(int i=0; i<queryParams.length; i++){ query.setParameter(i, queryParams[i]); } } } //创建排序hql语句 protected String createOrderBy(Map<String, String> orderby){ StringBuffer sb = new StringBuffer(""); if(orderby != null && orderby.size() > 0){ sb.append(" order by "); for(String key : orderby.keySet()){ sb.append(key).append(" ").append(orderby.get(key)).append(","); } sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } List list = query.setFirstResult(getFirstResult(pageNo, maxResult))//设置分页起始位置 .setMaxResults(maxResult)//设置每页显示的记录数 .list();//将查询结果转化为List对象 //可变长度的参数 public List<Object[]> findBySql(final String sql, final Object... values) { Query query = this.getSession().createSQLQuery(sql); if (values != null) { for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } } return query.list(); } //Page: Session session = this.getSession(); Criteria criteria = session.createCriteria(this.getModelClass()); criteria.setFirstResult((pageIndex - 1) * pageSize); criteria.setMaxResults(pageSize); List result = criteria.list(); //TT: public long countExact(String[] propertyNames, Object[] propertyValues) { List valueList = new ArrayList(); String hql = "select count(*) from " + getModelClass().getName() + " model where 1=1"; for (int i = 0; i < propertyNames.length; i++) { String propName = propertyNames[i]; if (null != propertyValues[i] && !"".equals(propertyValues[i])) { hql += " and model." + propName + "=?"; valueList.add(propertyValues[i]); } else { hql += " and model." + propName + " is null"; } } Integer result; if (valueList.size() > 0) { result = (Integer) this.getHibernateTemplate().iterate(hql, valueList.toArray()).next(); return result.longValue(); } else { result = (Integer) this.getHibernateTemplate().iterate(hql).next(); return result.longValue(); } } public long countLike(String[] propertyNames, Object[] propertyValues) { String hql = "select count(*) from " + getModelClass().getName() + " model where 1=1"; //for (String propName : propertyNames) { for (int i = 0; i < propertyNames.length; i++) { hql += " and model." + propertyNames[i] + "like :" + propertyNames[i]; } Long result = (Long) this.getHibernateTemplate().findByNamedParam(hql, propertyNames, propertyValues).get(0); return result.longValue(); } public List findExact(String propertyName, Object propertyValue) { String hql = "from " + getModelClass().getName() + " model where model." + propertyName; if (null != propertyValue) { hql += "= ?"; return this.getHibernateTemplate().find(hql, propertyValue); } else { hql += " is null"; //null return this.getHibernateTemplate().find(hql); } } public List findExact(String[] propertyNames, Object[] propertyValues) { List valueList = new ArrayList(); String hql = "from " + getModelClass().getName() + " model where 1=1"; for (int i = 0; i < propertyNames.length; i++) { String propName = propertyNames[i]; if (null != propertyValues[i]) { hql += " and model." + propName + "=?"; valueList.add(propertyValues[i]); } else { hql += " and model." + propName + " is null"; } } if (valueList.size() > 0) { return this.getHibernateTemplate().find(hql, valueList.toArray()); } else { return this.getHibernateTemplate().find(hql); } } public List findLike(String propertyName, Object propertyValue) { String hql = "from " + getModelClass().getName() + " model where model." + propertyName + " like ?"; return this.getHibernateTemplate().find(hql, propertyValue); } public List findLike(String[] propertyNames, Object[] propertyValues) { String hql = "from " + getModelClass().getName() + " model where 1=1"; //for (String propName : propertyNames) { for (int i = 0; i < propertyNames.length; i++) { hql += " and model." + propertyNames[i] + " like :" + propertyNames[i]; } return this.getHibernateTemplate().findByNamedParam(hql, propertyNames, propertyValues); } public List getQueryResult(String queryStr, String[] paramNames, Object[] paramValues, Type[] paramTypes, int page, int pageSize) { List result; // if(returnAlias!=null && returnClass!=null){ // query = getSession().createSQLQuery(queryStr).addEntity(returnAlias, returnClass); // }else{ // query = getSession().createSQLQuery(queryStr); // } Query query = getSession().createQuery(queryStr); if (pageSize > 0) { query.setFirstResult((page - 1) * pageSize); //使当前记录滚动到指定记录 query.setMaxResults(pageSize); } for (int i = 0; i < paramNames.length; i++) { query.setParameter(paramNames[i], paramValues[i], paramTypes[i]); } result = query.list(); return result; }