基于hibernate的灵活分页查询

说明:Pagenation是一个分页工具类,主要运算一些分页内容
分页实现:
/**
	 * 根据商家ID,获取记录总数
	 * @return
	 */
	public Integer findTotalA(String mId,String orId){
		String hql = "select count(*) from OrderformTransinfor o where o.prodCommonProp.manufacturer.id=? and o.orderinfo.id=?";
		List<Number>  list=getHibernateTemplate().find(hql,mId,orId);
		Number  count = list.get(0);
		return count.intValue();
	}



/**
	 * 根据自由条件分页查询,灵活性增强
	 * @return
	 */
	//@Override
	public Pagenation find4PageFree(Integer pageNum,Integer pageSize,String mId,String orId){
		String hql = "from OrderformTransinfor o where o.prodCommonProp.manufacturer.id=? and o.orderinfo.id=?";
		SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql);
		Pagenation page = new Pagenation(pageNum,findTotalA(mId,orId),pageSize);
		query.setFirstResult(page.getStartNav());
		query.setMaxResults(pageSize);
		query.setString(0, mId);
		query.setString(1, orId);
		List<OrderformTransinfor> orderformTransinfors = query.list();
		page.setData(orderformTransinfors);
		return page;
	}




你可能感兴趣的:(基于hibernate的灵活分页查询)