转载请注明出处~!!
第一种:
DetachedCriteria Criteria 形式
@SuppressWarnings({ "unchecked", "rawtypes" }) public PageModel getPageModel(final PageModel pageModel, final DetachedCriteria _criteria, Class entityType) throws DataAccessException{ pageModel.initCriteria(_criteria, entityType); HibernateCallback<Integer> callback0 = new HibernateCallback<Integer>() { public Integer doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = _criteria.getExecutableCriteria(session); criteria.setProjection(Projections.rowCount()); criteria.setResultTransformer(CriteriaSpecification.PROJECTION); Number obj = (Number)criteria.uniqueResult(); return obj==null ? 0 : obj.intValue(); } }; final int totalCount = getHibernateTemplate().execute(callback0); pageModel.setTotalCount(totalCount); if(totalCount>0){ HibernateCallback<List<TEntity>> callback1 = new HibernateCallback<List<TEntity>>() { public List<TEntity> doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = _criteria.getExecutableCriteria(session); criteria.setProjection(null); criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); if(totalCount == 0) return ListUtils.EMPTY_LIST; if(pageModel.getPageSize()>0){ int firstResult = (pageModel.getCurrentPage()-1) * pageModel.getPageSize(); int maxResult = pageModel.getPageSize(); if ((firstResult + maxResult) > totalCount) { maxResult = totalCount - firstResult; } criteria.setFirstResult(firstResult); criteria.setMaxResults(maxResult); } List<Order> olist = pageModel.getOrderList(); if (olist!=null) { for (Order o : olist) { criteria.addOrder(o); } } return criteria.list(); } }; pageModel.setResult(getHibernateTemplate().execute(callback1)); }else{ pageModel.setResult(Collections.EMPTY_LIST); } return pageModel; }
第二种:
SQL
Hibernate形式
public PageModel getPagesList(final PageModel pageModel,final DetachedCriteria _criteria, Class entityType,final int totalCount,final KnowledgeType knowledgeType,final String andQuery) { pageModel.initCriteria(_criteria, entityType); pageModel.setTotalCount(totalCount); if(totalCount>0){ List list = getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { int firstResult = (pageModel.getCurrentPage()-1) * pageModel.getPageSize(); int maxResult = pageModel.getPageSize(); if ((firstResult + maxResult) > totalCount) { maxResult = totalCount - firstResult; } String sql = " SELECT * FROM lc_knowledge_manager AS k "+ " WHERE knowledgeType_id "+ " IN "+ " (SELECT id FROM lc_knowledge_type WHERE topParentName="+ " (SELECT topParentName FROM lc_knowledge_type WHERE typeName='" +knowledgeType.getTypeName()+ "' ORDER BY id LIMIT 0,1) AND cateCode LIKE '%" +knowledgeType.getCateCode()+ "%')"+ andQuery + " ORDER BY id DESC"; SQLQuery q = session.createSQLQuery(sql); q.setCacheable(false); return q.addEntity(KnowledgeManager.class) .setFirstResult(firstResult) .setMaxResults(maxResult) .list(); } }); pageModel.setResult(list); }else{ pageModel.setResult(Collections.EMPTY_LIST); } return pageModel; }
第三种:
HQL
Hibernate形式
public PageModel nListArtcs(final int pageNo, final int pageSize, final String obj, final String order) { List list = getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { return session .createQuery("from " + obj + " order by id " + order) .setFirstResult((pageNo - 1) * pageSize) .setMaxResults(pageSize).list(); } }); return new PageModel(list, this.getListAmount(obj), pageSize, pageNo); }