日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于hibernate实现的分页技术

發布時間:2025/6/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于hibernate实现的分页技术 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先說明一下基于hibernate實現分頁的原理,假如從數據庫取出100條數據,我們要讓每頁顯示10條,假如從30開始,只需要設置起始位置和最大的返回結果即可

先上代碼:注意傳進來的參數有 Page這類,后面有介紹

[javascript] view plain copy
  • public?List<Article>?queryByPage(final?String?username,?final?Page?page)?{??
  • ????????return?this.getHibernateTemplate().executeFind(new?HibernateCallback()?{??
  • ????????????public?Object?doInHibernate(Session?session)??
  • ????????????????????throws?HibernateException,?SQLException?{??
  • ????????????????Query?query?=?session.createQuery("select?art?from?Article?art?where?art.username?=??");??
  • ????????????????//設置參數??
  • ????????????????query.setParameter(0,?username);??
  • ????????????????//設置每頁顯示多少個,設置多大結果。??
  • ????????????????query.setMaxResults(page.getEveryPage());??
  • ????????????????//設置起點??
  • ????????????????query.setFirstResult(page.getBeginIndex());??
  • ????????????????return?query.list();??
  • ????????????}??
  • ????????});??

  • 上面關鍵代碼是?setMaxResults(),和setFirstResult(),即設置最大顯示值和起點

    這里我們需要一個Page工具類,用來操作分頁。

    Page.java

    [java] view plain copy
  • package?com.fenye;??
  • ??
  • public?class?Page?{??
  • ????//?1.每頁顯示數量(everyPage)??
  • ????private?int?everyPage;??
  • ????//?2.總記錄數(totalCount)??
  • ????private?int?totalCount;??
  • ????//?3.總頁數(totalPage)??
  • ????private?int?totalPage;??
  • ????//?4.當前頁(currentPage)??
  • ????private?int?currentPage;??
  • ????//?5.起始點(beginIndex)??
  • ????private?int?beginIndex;??
  • ????//?6.是否有上一頁(hasPrePage)??
  • ????private?boolean?hasPrePage;??
  • ????//?7.是否有下一頁(hasNextPage)??
  • ????private?boolean?hasNextPage;??
  • ??
  • ????public?Page(int?everyPage,?int?totalCount,?int?totalPage,?int?currentPage,??
  • ????????????int?beginIndex,?boolean?hasPrePage,?boolean?hasNextPage)?{??
  • ????????this.everyPage?=?everyPage;??
  • ????????this.totalCount?=?totalCount;??
  • ????????this.totalPage?=?totalPage;??
  • ????????this.currentPage?=?currentPage;??
  • ????????this.beginIndex?=?beginIndex;??
  • ????????this.hasPrePage?=?hasPrePage;??
  • ????????this.hasNextPage?=?hasNextPage;??
  • ????}??
  • ??
  • ????//構造函數,默認??
  • ????public?Page(){}??
  • ??????
  • ????//構造方法,對所有屬性進行設置??
  • ??????
  • ??????
  • ????public?int?getEveryPage()?{??
  • ????????return?everyPage;??
  • ????}??
  • ??
  • ????public?void?setEveryPage(int?everyPage)?{??
  • ????????this.everyPage?=?everyPage;??
  • ????}??
  • ??
  • ????public?int?getTotalCount()?{??
  • ????????return?totalCount;??
  • ????}??
  • ??
  • ????public?void?setTotalCount(int?totalCount)?{??
  • ????????this.totalCount?=?totalCount;??
  • ????}??
  • ??
  • ????public?int?getTotalPage()?{??
  • ????????return?totalPage;??
  • ????}??
  • ??
  • ????public?void?setTotalPage(int?totalPage)?{??
  • ????????this.totalPage?=?totalPage;??
  • ????}??
  • ??
  • ????public?int?getCurrentPage()?{??
  • ????????return?currentPage;??
  • ????}??
  • ??
  • ????public?void?setCurrentPage(int?currentPage)?{??
  • ????????this.currentPage?=?currentPage;??
  • ????}??
  • ??
  • ????public?int?getBeginIndex()?{??
  • ????????return?beginIndex;??
  • ????}??
  • ??
  • ????public?void?setBeginIndex(int?beginIndex)?{??
  • ????????this.beginIndex?=?beginIndex;??
  • ????}??
  • ??
  • ????public?boolean?isHasPrePage()?{??
  • ????????return?hasPrePage;??
  • ????}??
  • ??
  • ????public?void?setHasPrePage(boolean?hasPrePage)?{??
  • ????????this.hasPrePage?=?hasPrePage;??
  • ????}??
  • ??
  • ????public?boolean?isHasNextPage()?{??
  • ????????return?hasNextPage;??
  • ????}??
  • ??
  • ????public?void?setHasNextPage(boolean?hasNextPage)?{??
  • ????????this.hasNextPage?=?hasNextPage;??
  • ????}??
  • ??
  • }??
  • Page工具類主要是封裝頁面信息,一共多少數據啊,一頁顯示多少啊,起點的序號,總頁數,是否有上一頁下一頁,當前頁。

    還需要一個操作page的工具類,PageUtil.java

    [javascript] view plain copy
  • package?com.sanqing.fenye;??
  • /*?
  • ?*?分頁信息輔助類?
  • ?*/??
  • public?class?PageUtil?{??
  • ??????
  • ????public?static?Page?createPage(int?everyPage,int?totalCount,int?currentPage)?{??
  • ????????everyPage?=?getEveryPage(everyPage);??
  • ????????currentPage?=?getCurrentPage(currentPage);??
  • ????????int?totalPage?=?getTotalPage(everyPage,?totalCount);??
  • ????????int?beginIndex?=?getBeginIndex(everyPage,?currentPage);??
  • ????????boolean?hasPrePage?=?getHasPrePage(currentPage);??
  • ????????boolean?hasNextPage?=?getHasNextPage(totalPage,?currentPage);??
  • ????????return?new?Page(everyPage,?totalCount,?totalPage,?currentPage,??
  • ????????????????beginIndex,?hasPrePage,??hasNextPage);??
  • ????}??
  • ??????
  • ????public?static?Page?createPage(Page?page,int?totalCount)?{??
  • ????????int?everyPage?=?getEveryPage(page.getEveryPage());??
  • ????????int?currentPage?=?getCurrentPage(page.getCurrentPage());??
  • ????????int?totalPage?=?getTotalPage(everyPage,?totalCount);??
  • ????????int?beginIndex?=?getBeginIndex(everyPage,?currentPage);??
  • ????????boolean?hasPrePage?=?getHasPrePage(currentPage);??
  • ????????boolean?hasNextPage?=?getHasNextPage(totalPage,?currentPage);??
  • ????????return?new?Page(everyPage,?totalCount,?totalPage,?currentPage,??
  • ????????????????beginIndex,?hasPrePage,??hasNextPage);??
  • ????}??
  • ??????
  • ????//設置每頁顯示記錄數??
  • ????public?static?int?getEveryPage(int?everyPage)?{??
  • ????????return?everyPage?==?0???10?:?everyPage;??
  • ????}??
  • ??????
  • ????//設置當前頁??
  • ????public?static?int?getCurrentPage(int?currentPage)?{??
  • ????????return?currentPage?==?0???1?:?currentPage;??
  • ????}??
  • ??????
  • ????//設置總頁數,需要總記錄數,每頁顯示多少??
  • ????public?static?int?getTotalPage(int?everyPage,int?totalCount)?{??
  • ????????int?totalPage?=?0;??
  • ????????if(totalCount?%?everyPage?==?0)?{??
  • ????????????totalPage?=?totalCount?/?everyPage;??
  • ????????}?else?{??
  • ????????????totalPage?=?totalCount?/?everyPage?+?1;??
  • ????????}??
  • ????????return?totalPage;??
  • ????}??
  • ??????
  • ????//設置起始點,需要每頁顯示多少,當前頁??
  • ????public?static?int?getBeginIndex(int?everyPage,int?currentPage)?{??
  • ????????return?(currentPage?-?1)?*?everyPage;??
  • ????}??
  • ??????
  • ????//設置是否有上一頁,需要當前頁??
  • ????public?static?boolean?getHasPrePage(int?currentPage)?{??
  • ????????return?currentPage?==?1???false?:?true;??
  • ????}??
  • ??????
  • ????//設置是否有下一個,需要總頁數和當前頁??
  • ????public?static?boolean?getHasNextPage(int?totalPage,?int?currentPage)?{??
  • ????????return?currentPage?==?totalPage?||?totalPage?==?0???false?:?true;??
  • ????}??
  • ??????
  • }??
  • 創建Page只需要3個參數,每頁顯示多少數據,當前頁,總共多少數據,其他的4個參數都可以通過這三個計算出來

    所以后面要創建Page,只需要調用這工具方法PageUtil.createPage(3個參數),就返回一Page.

    返回的Page就是前面參數的Page,即要顯示的分頁

    這樣就算完成了分頁的功能。

    總結

    以上是生活随笔為你收集整理的基于hibernate实现的分页技术的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。