この記事では、Hibernateの実装に基づいたページネーション技術について説明します。次のように、参照のために共有してください。
最初に、冬眠に基づいてページングを実装するという原則を説明させてください。データベースから100個のデータを取得する場合は、ページごとに10個を表示する必要があります。 30から始まる場合、最初にコードを入力するには、開始位置と最大リターン結果を設定する必要があります。最初にコードを入力します。
パブリックリスト<portion> querybypage(最終文字列ユーザー名、最終ページページ){this.gethibernateTemplate()。executefind(new hibernatecallback(){public object doinhibernate(session session)throws hibernateexception、sqlexception {query query = session.creater( "sect aart" " Query.SetParameter(ユーザー名)。上記のキーコードは、setMaxResults()、およびsetFirstreSult()で、最大表示値と開始点を設定します。
ここでは、ページネーションを操作するにはページツールクラスが必要です。
page.java:
パッケージcom.fenye; public classページ{//1。ページごとのディスプレイ数(すべてのページ)プライベートintすべてのページ。 // 2。レコードの総数(TotalCount)Private Int TotalCount。 // 3。ページの総数(TotalPage)Private Int TotalPage; //4。現在のページ(currentPage)private int currentPage; //5。StartPoint(beginIndex)Private int beginIndex; // 6。前のページ(hasprepage)private boolean hasprepageはありますか。 // 7。次のページ(HasNextPage)Private Boolean HasNextPageがありますか。パブリックページ(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; } // constructor、default public page(){} //メソッドを作成する、すべての属性を設定しますpublic int geteverypage(){return EveryPage; } public void seteverypage(int EveryPage){this.EveryPage = EveryPage; } public int gettotalcount(){return totalcount; } public void settoTotalcount(int totalcount){this.totalcount = totalcount; } public int getTotalPage(){return totalpage; } public void settotalpage(int totalpage){this.totalpage = totalpage; } 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; }}ページツールのクラスは、主にページ情報、合計のデータの量、ページに表示される金額、出発点の数、ページの総数、前のページ、次のページ、および現在のページをカプセル化します。
また、操作ページのツールクラスが必要です、pageutil.java
パッケージ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);新しいページを返します(すべてのページ、TotalCount、TotalPage、currentPage、beginIndex、hasNextPage); } public staticページCreatePage(ページ、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);新しいページを返します(すべてのページ、TotalCount、TotalPage、currentPage、beginIndex、hasNextPage); } //ページごとに表示されるレコードの数を設定しますpublic static int geteverypage(int Everypage){return Everypage == 0? 10:すべてのページ。 } //現在のページを設定しますpublic static int getCurrentPage(int currentPage){return currentPage == 0? 1:CurrentPage; } //ページの総数を設定し、レコードの総数が必要です。パブリックstatic int getTotalPage(int EveryPage、int totalcount){int totalpage = 0; if(totalcount%EveryPage == 0){TotalPage = TotalCount / EveryPage; } else {totalpage = totalcount / everypage + 1; } totalpageを返します。 } //開始ポイントを設定します。どのパブリック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; }}ページを作成するには、3つのパラメーターのみが必要です。各ページに表示されるデータの量、現在のページに表示されるデータの量、合計データの量、およびこれら3つを通じて他の4つのパラメーターを計算できます。
したがって、後でページを作成するには、このツールメソッドPageutil.createpage(3パラメーター)を呼び出し、ページを返すだけです。
返されたページは、前のパラメーターのページ、つまり表示されるページです
これは、ページネーション関数を完了すると見なされます。
この記事の説明が、Hibernate Frameworkに基づいた全員のJavaプログラミングに役立つことを願っています。