基于hibernate实现的分页技术

先说明一下基于hibernate实现分页的原理,假如从数据库取出100条数据,我们要让每页显示10条,假如从30开始,只需要设置起始位置和最大的返回结果即可

先上代码:注意传进来的参数有 Page这类,后面有介绍

[javascript] view plain copy
  1. publicList<Article>queryByPage(finalStringusername,finalPagepage){
  2. returnthis.getHibernateTemplate().executeFind(newHibernateCallback(){
  3. publicObjectdoInHibernate(Sessionsession)
  4. throwsHibernateException,SQLException{
  5. Queryquery=session.createQuery("selectartfromArticleartwhereart.username=?");
  6. //设置参数
  7. query.setParameter(0,username);
  8. //设置每页显示多少个,设置多大结果。
  9. query.setMaxResults(page.getEveryPage());
  10. //设置起点
  11. query.setFirstResult(page.getBeginIndex());
  12. returnquery.list();
  13. }
  14. });

上面关键代码是setMaxResults(),和setFirstResult(),即设置最大显示值和起点

这里我们需要一个Page工具类,用来操作分页。

Page.java

[java] view plain copy
  1. packagecom.fenye;
  2. publicclassPage{
  3. //1.每页显示数量(everyPage)
  4. privateinteveryPage;
  5. //2.总记录数(totalCount)
  6. privateinttotalCount;
  7. //3.总页数(totalPage)
  8. privateinttotalPage;
  9. //4.当前页(currentPage)
  10. privateintcurrentPage;
  11. //5.起始点(beginIndex)
  12. privateintbeginIndex;
  13. //6.是否有上一页(hasPrePage)
  14. privatebooleanhasPrePage;
  15. //7.是否有下一页(hasNextPage)
  16. privatebooleanhasNextPage;
  17. publicPage(inteveryPage,inttotalCount,inttotalPage,intcurrentPage,
  18. intbeginIndex,booleanhasPrePage,booleanhasNextPage){
  19. this.everyPage=everyPage;
  20. this.totalCount=totalCount;
  21. this.totalPage=totalPage;
  22. this.currentPage=currentPage;
  23. this.beginIndex=beginIndex;
  24. this.hasPrePage=hasPrePage;
  25. this.hasNextPage=hasNextPage;
  26. }
  27. //构造函数,默认
  28. publicPage(){}
  29. //构造方法,对所有属性进行设置
  30. publicintgetEveryPage(){
  31. returneveryPage;
  32. }
  33. publicvoidsetEveryPage(inteveryPage){
  34. this.everyPage=everyPage;
  35. }
  36. publicintgetTotalCount(){
  37. returntotalCount;
  38. }
  39. publicvoidsetTotalCount(inttotalCount){
  40. this.totalCount=totalCount;
  41. }
  42. publicintgetTotalPage(){
  43. returntotalPage;
  44. }
  45. publicvoidsetTotalPage(inttotalPage){
  46. this.totalPage=totalPage;
  47. }
  48. publicintgetCurrentPage(){
  49. returncurrentPage;
  50. }
  51. publicvoidsetCurrentPage(intcurrentPage){
  52. this.currentPage=currentPage;
  53. }
  54. publicintgetBeginIndex(){
  55. returnbeginIndex;
  56. }
  57. publicvoidsetBeginIndex(intbeginIndex){
  58. this.beginIndex=beginIndex;
  59. }
  60. publicbooleanisHasPrePage(){
  61. returnhasPrePage;
  62. }
  63. publicvoidsetHasPrePage(booleanhasPrePage){
  64. this.hasPrePage=hasPrePage;
  65. }
  66. publicbooleanisHasNextPage(){
  67. returnhasNextPage;
  68. }
  69. publicvoidsetHasNextPage(booleanhasNextPage){
  70. this.hasNextPage=hasNextPage;
  71. }
  72. }
Page工具类主要是封装页面信息,一共多少数据啊,一页显示多少啊,起点的序号,总页数,是否有上一页下一页,当前页。

还需要一个操作page的工具类,PageUtil.java

[javascript] view plain copy
  1. packagecom.sanqing.fenye;
  2. /*
  3. *分页信息辅助类
  4. */
  5. publicclassPageUtil{
  6. publicstaticPagecreatePage(inteveryPage,inttotalCount,intcurrentPage){
  7. everyPage=getEveryPage(everyPage);
  8. currentPage=getCurrentPage(currentPage);
  9. inttotalPage=getTotalPage(everyPage,totalCount);
  10. intbeginIndex=getBeginIndex(everyPage,currentPage);
  11. booleanhasPrePage=getHasPrePage(currentPage);
  12. booleanhasNextPage=getHasNextPage(totalPage,currentPage);
  13. returnnewPage(everyPage,totalCount,totalPage,currentPage,
  14. beginIndex,hasPrePage,hasNextPage);
  15. }
  16. publicstaticPagecreatePage(Pagepage,inttotalCount){
  17. inteveryPage=getEveryPage(page.getEveryPage());
  18. intcurrentPage=getCurrentPage(page.getCurrentPage());
  19. inttotalPage=getTotalPage(everyPage,totalCount);
  20. intbeginIndex=getBeginIndex(everyPage,currentPage);
  21. booleanhasPrePage=getHasPrePage(currentPage);
  22. booleanhasNextPage=getHasNextPage(totalPage,currentPage);
  23. returnnewPage(everyPage,totalCount,totalPage,currentPage,
  24. beginIndex,hasPrePage,hasNextPage);
  25. }
  26. //设置每页显示记录数
  27. publicstaticintgetEveryPage(inteveryPage){
  28. returneveryPage==0?10:everyPage;
  29. }
  30. //设置当前页
  31. publicstaticintgetCurrentPage(intcurrentPage){
  32. returncurrentPage==0?1:currentPage;
  33. }
  34. //设置总页数,需要总记录数,每页显示多少
  35. publicstaticintgetTotalPage(inteveryPage,inttotalCount){
  36. inttotalPage=0;
  37. if(totalCount%everyPage==0){
  38. totalPage=totalCount/everyPage;
  39. }else{
  40. totalPage=totalCount/everyPage+1;
  41. }
  42. returntotalPage;
  43. }
  44. //设置起始点,需要每页显示多少,当前页
  45. publicstaticintgetBeginIndex(inteveryPage,intcurrentPage){
  46. return(currentPage-1)*everyPage;
  47. }
  48. //设置是否有上一页,需要当前页
  49. publicstaticbooleangetHasPrePage(intcurrentPage){
  50. returncurrentPage==1?false:true;
  51. }
  52. //设置是否有下一个,需要总页数和当前页
  53. publicstaticbooleangetHasNextPage(inttotalPage,intcurrentPage){
  54. returncurrentPage==totalPage||totalPage==0?false:true;
  55. }
  56. }
创建Page只需要3个参数,每页显示多少数据,当前页,总共多少数据,其他的4个参数都可以通过这三个计算出来

所以后面要创建Page,只需要调用这工具方法PageUtil.createPage(3个参数),就返回一Page.

返回的Page就是前面参数的Page,即要显示的分页

这样就算完成了分页的功能。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值