hibernate中利用criteria分页的实现

花了一些时间查资料,做出的东西不能满意,先贴出来,希望有人指正。

首先查询具体记录内容并放入page对象:

public Page findAll(Page page) {
        System.out.println("finding Apply instance by some condition");
        try {
        	final DetachedCriteria query = DetachedCriteria
            .forClass(Apply.class);
        	Criteria criteria = query.getExecutableCriteria(getSession());
        	//select condition
        	criteria.add(Restrictions.eq("approvalUserId", 2));
        	//...
        	
        	criteria.setFirstResult(page.getFirstResult());
        	criteria.setMaxResults(page.getMaxResult());
        	List<Apply> results=criteria.list();
        	page.setList( results);
        	//the total
        	int total=findAllCount(page);
            page.setTotal(total);
            
	        System.out.println("result:"+results);
    		return page;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }
其中的 findAllCount(page)函数为获得查询记录的总数,代码如下:

public int findAllCount(Page page) {
    	System.out.println("finding count Apply instance by some condition");
    	try {
    		final DetachedCriteria query = DetachedCriteria
    		.forClass(Apply.class);
    		Criteria criteria = query.getExecutableCriteria(getSession());
    		criteria.add(Restrictions.eq("approvalUserId", 2));
    		int total=((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue(); 
    		System.out.println("find by example successful, result size: " + total);

    		return total;
    	} catch (RuntimeException re) {
    		log.error("find by example failed", re);
    		throw re;
    	}
    }
控制台输出信息:

finding Apply instance by some condition
Hibernate: select this_.apply_id as apply1_2_0_, this_.approval_user_id as approval2_2_0_, this_.organizer as organizer2_0_, this_.organizer_nature as organizer4_2_0_, this_.organizer_address as organizer5_2_0_, this_.apply_user_id as apply6_2_0_, this_.apply_user_position as apply7_2_0_, this_.layout as layout2_0_, this_.begin_time as begin9_2_0_, this_.end_time as end10_2_0_, this_.people_amount as people11_2_0_, this_.is_lodging as is12_2_0_, this_.lodging_amount as lodging13_2_0_, this_.hotel_level as hotel14_2_0_, this_.lodging_days as lodging15_2_0_, this_.is_tour as is16_2_0_, this_.tour_amount as tour17_2_0_, this_.tour_date as tour18_2_0_, this_.tour_place as tour19_2_0_, this_.note as note2_0_ from meeting.apply this_ where this_.approval_user_id=? limit ?, ?
finding count Apply instance by some condition
Hibernate: select count(*) as y0_ from meeting.apply this_ where this_.approval_user_id=?
find by example successful, result size: 13
result:[Apply [applyId=2, applyUserId=34, applyUserPosition=position1, approvalUserId=2, beginTime=234, endTime=234, hotelLevel=1, isLodging=0, isTour=1, layout=layout1, lodgingAmount=1, lodgingDays=12, note=2, organizer=324, organizerAddress=address1, organizerNature=1, peopleAmount=234, tourAmount=1, tourDate=1, tourPlace=1], Apply [applyId=3, applyUserId=34, applyUserPosition=position1, approvalUserId=2, beginTime=234, endTime=234, hotelLevel=1, isLodging=0, isTour=1, layout=layout1, lodgingAmount=1, lodgingDays=12, note=3, organizer=324, organizerAddress=address1, organizerNature=1, peopleAmount=234, tourAmount=1, tourDate=1, tourPlace=1], Apply [applyId=4, applyUserId=34, applyUserPosition=position1, approvalUserId=2, beginTime=234, endTime=234, hotelLevel=1, isLodging=0, isTour=1, layout=layout1, lodgingAmount=1, lodgingDays=12, note=4, organizer=324, organizerAddress=address1, organizerNature=1, peopleAmount=234, tourAmount=1, tourDate=1, tourPlace=1], Apply [applyId=5, applyUserId=34, applyUserPosition=position1, approvalUserId=2, beginTime=234, endTime=234, hotelLevel=1, isLodging=0, isTour=1, layout=layout1, lodgingAmount=1, lodgingDays=12, note=5, organizer=324, organizerAddress=address1, organizerNature=1, peopleAmount=234, tourAmount=1, tourDate=1, tourPlace=1], Apply [applyId=6, applyUserId=34, applyUserPosition=position1, approvalUserId=2, beginTime=234, endTime=234, hotelLevel=1, isLodging=0, isTour=1, layout=layout1, lodgingAmount=1, lodgingDays=12, note=6, organizer=324, organizerAddress=address1, organizerNature=1, peopleAmount=234, tourAmount=1, tourDate=1, tourPlace=1]]
{"total":13,"page":1,"rows":[{"id":2,"cell":{"id":2,"applyUserId":34,"applyUserPosition":"position1","approvalUserId":2,"beginTime":"234","endTime":"234","isLodging":"0","hotelLevel":"1","isTour":"1","layout":"layout1","lodgingAmount":"1","lodgingDays":"12","note":"2","organizer":"324","organizerAddress":"address1","organizerNature":"1","peopleAmount":"234","tourAmount":"1","tourDate":"1","tourPlace":"1"}},{"id":3,"cell":{"id":3,"applyUserId":34,"applyUserPosition":"position1","approvalUserId":2,"beginTime":"234","endTime":"234","isLodging":"0","hotelLevel":"1","isTour":"1","layout":"layout1","lodgingAmount":"1","lodgingDays":"12","note":"3","organizer":"324","organizerAddress":"address1","organizerNature":"1","peopleAmount":"234","tourAmount":"1","tourDate":"1","tourPlace":"1"}},{"id":4,"cell":{"id":4,"applyUserId":34,"applyUserPosition":"position1","approvalUserId":2,"beginTime":"234","endTime":"234","isLodging":"0","hotelLevel":"1","isTour":"1","layout":"layout1","lodgingAmount":"1","lodgingDays":"12","note":"4","organizer":"324","organizerAddress":"address1","organizerNature":"1","peopleAmount":"234","tourAmount":"1","tourDate":"1","tourPlace":"1"}},{"id":5,"cell":{"id":5,"applyUserId":34,"applyUserPosition":"position1","approvalUserId":2,"beginTime":"234","endTime":"234","isLodging":"0","hotelLevel":"1","isTour":"1","layout":"layout1","lodgingAmount":"1","lodgingDays":"12","note":"5","organizer":"324","organizerAddress":"address1","organizerNature":"1","peopleAmount":"234","tourAmount":"1","tourDate":"1","tourPlace":"1"}},{"id":6,"cell":{"id":6,"applyUserId":34,"applyUserPosition":"position1","approvalUserId":2,"beginTime":"234","endTime":"234","isLodging":"0","hotelLevel":"1","isTour":"1","layout":"layout1","lodgingAmount":"1","lodgingDays":"12","note":"6","organizer":"324","organizerAddress":"address1","organizerNature":"1","peopleAmount":"234","tourAmount":"1","tourDate":"1","tourPlace":"1"}}]}
结果虽然是正确的,但是,在两个函数里面,重复代码:

final DetachedCriteria query = DetachedCriteria
    	.forClass(Apply.class);
Criteria criteria = query.getExecutableCriteria(getSession());
criteria.add(Restrictions.eq("approvalUserId", 2));
也试图在一个函数中实现,没能完成。

也在疑惑,难道获取记录和获取总数必须要分开来做吗?希望高手能够解惑。谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值