SSH hibernate 分页的几种实现方式

转载请注明出处~!!

第一种:

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值