基於hibernate完成的分頁技巧實例剖析。本站提示廣大學習愛好者:(基於hibernate完成的分頁技巧實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是基於hibernate完成的分頁技巧實例剖析正文
本文實例講述了基於hibernate完成的分頁技巧。分享給年夜家供年夜家參考,詳細以下:
先解釋一下基於hibernate完成分頁的道理,假設從數據庫掏出100條數據,我們要讓每頁顯示10條,假設從30開端,只須要設置肇端地位和最年夜的前往成果便可
先上代碼:留意傳出去的參數有 Page這類,前面有引見
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:
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
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框架的Java法式設計有所贊助。