SSH hibernate 分页的几种实现方式

Posted 当富网络

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
	



以上是关于SSH hibernate 分页的几种实现方式的主要内容,如果未能解决你的问题,请参考以下文章

SQL分页的几种方法

ElasticSearch第5天 es实现分页查询的几种方式

MyBatis-Plus 实现分页的几种写法

查询分页的几种Sql写法

ssh框架开发crm(客户关系系统总结)

java中实现分页的常见几种方式