Hibernate分页的两种实现方法
程序员文章站
2024-03-09 11:00:41
本文实例讲述了hibernate分页的两种实现方法。分享给大家供大家参考,具体如下:
1. criteria分页
public page getpage(int...
本文实例讲述了hibernate分页的两种实现方法。分享给大家供大家参考,具体如下:
1. criteria分页
public page getpage(int currentpage,int pagesize,criterion...crts){ criteria c=session.createcriteria(house.class); list list=null; for (int i = 0; i < crts.length; i++) { c.add(crts[i]); } c.setprojection(projections.rowcount()); int totalrecord=integer.valueof(c.uniqueresult().tostring()); c.setprojection(null); c.setfirstresult((pagesize)*(currentpage-1)); c.setmaxresults(pagesize); list=c.list(); page page=new page(); page.setcurrentpage(currentpage); page.setpagesize(pagesize); page.settotalrecord(totalrecord); page.setlist(list); return page; }
2. hql分页
public page getpage(int currentpage,int pagesize,string hql,object...args){ string counthql="select count(*) "+hql.substring(hql.indexof("from")); session session=hibernateutil.getinstance().getsession(); query query=session.createquery(counthql); for (int i = 0; i < args.length; i++) { query.setparameter(i, args[i]); } int totalrecord=integer.valueof(query.uniqueresult()+""); query=session.createquery(hql); for (int i = 0; i < args.length; i++) { query.setparameter(i, args[i]); } query.setfirstresult(pagesize*(currentpage-1)); query.setmaxresults(pagesize); list<house> list=(list<house>)query.list(); page page=new page(); page.setcurrentpage(currentpage); page.setpagesize(pagesize); page.settotalrecord(totalrecord); page.setlist(list); return page; }
希望本文所述对大家基于hibernate框架的java程序设计有所帮助。