基于hibernate實(shí)現(xiàn)的分頁技術(shù)實(shí)例分析
本文實(shí)例講述了基于hibernate實(shí)現(xiàn)的分頁技術(shù)。分享給大家供大家參考,具體如下:
先說明一下基于hibernate實(shí)現(xiàn)分頁的原理,假如從數(shù)據(jù)庫取出100條數(shù)據(jù),我們要讓每頁顯示10條,假如從30開始,只需要設(shè)置起始位置和最大的返回結(jié)果即可
先上代碼:注意傳進(jìn)來的參數(shù)有 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 = ?");
//設(shè)置參數(shù)
query.setParameter(0, username);
//設(shè)置每頁顯示多少個(gè),設(shè)置多大結(jié)果。
query.setMaxResults(page.getEveryPage());
//設(shè)置起點(diǎn)
query.setFirstResult(page.getBeginIndex());
return query.list();
}
});
上面關(guān)鍵代碼是 setMaxResults(),和setFirstResult(),即設(shè)置最大顯示值和起點(diǎn)
這里我們需要一個(gè)Page工具類,用來操作分頁。
Page.java:
package com.fenye;
public class Page {
// 1.每頁顯示數(shù)量(everyPage)
private int everyPage;
// 2.總記錄數(shù)(totalCount)
private int totalCount;
// 3.總頁數(shù)(totalPage)
private int totalPage;
// 4.當(dāng)前頁(currentPage)
private int currentPage;
// 5.起始點(diǎn)(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;
}
//構(gòu)造函數(shù),默認(rèn)
public Page(){}
//構(gòu)造方法,對(duì)所有屬性進(jìn)行設(shè)置
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工具類主要是封裝頁面信息,一共多少數(shù)據(jù)啊,一頁顯示多少啊,起點(diǎn)的序號(hào),總頁數(shù),是否有上一頁下一頁,當(dāng)前頁。
還需要一個(gè)操作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);
}
//設(shè)置每頁顯示記錄數(shù)
public static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
//設(shè)置當(dāng)前頁
public static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
//設(shè)置總頁數(shù),需要總記錄數(shù),每頁顯示多少
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;
}
//設(shè)置起始點(diǎn),需要每頁顯示多少,當(dāng)前頁
public static int getBeginIndex(int everyPage,int currentPage) {
return (currentPage - 1) * everyPage;
}
//設(shè)置是否有上一頁,需要當(dāng)前頁
public static boolean getHasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
//設(shè)置是否有下一個(gè),需要總頁數(shù)和當(dāng)前頁
public static boolean getHasNextPage(int totalPage, int currentPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
創(chuàng)建Page只需要3個(gè)參數(shù),每頁顯示多少數(shù)據(jù),當(dāng)前頁,總共多少數(shù)據(jù),其他的4個(gè)參數(shù)都可以通過這三個(gè)計(jì)算出來
所以后面要?jiǎng)?chuàng)建Page,只需要調(diào)用這工具方法PageUtil.createPage(3個(gè)參數(shù)),就返回一Page.
返回的Page就是前面參數(shù)的Page,即要顯示的分頁
這樣就算完成了分頁的功能。
希望本文所述對(duì)大家基于Hibernate框架的Java程序設(shè)計(jì)有所幫助。
- 深入解析Java的Hibernate框架中的持久對(duì)象
- Java的Hibernate框架中的基本映射用法講解
- Java Hibernate中使用HQL語句進(jìn)行數(shù)據(jù)庫查詢的要點(diǎn)解析
- Java的Hibernate框架中一對(duì)多的單向和雙向關(guān)聯(lián)映射
- Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
- 全面解析Hibernate關(guān)聯(lián)操作、查詢操作、高級(jí)特性、并發(fā)處理機(jī)制
- 解決Hibernate4執(zhí)行save()或update()無效問題的方法
- SSH框架網(wǎng)上商城項(xiàng)目第16戰(zhàn)之Hibernate二級(jí)緩存處理首頁熱門顯示
- Spring,hibernate,struts經(jīng)典面試筆試題(含答案)
- 擴(kuò)展Hibernate使用自定義數(shù)據(jù)庫連接池的方法
- Hibernate延遲加載原理與實(shí)現(xiàn)方法
- Hibernate延遲加載技術(shù)詳解
- hibernate批量操作實(shí)例詳解
- Hibernate的Annotation版Hello world實(shí)例
- MyBatis與Hibernate的比較
- 詳解Java的Hibernate框架中的Interceptor和Collection
- Java的Hibernate框架結(jié)合MySQL的入門學(xué)習(xí)教程
相關(guān)文章
MyBatis-Plus實(shí)現(xiàn)多表聯(lián)查的方法實(shí)戰(zhàn)
這篇文章主要給大家介紹了關(guān)于MyBatis-Plus實(shí)現(xiàn)多表聯(lián)查的方法,MyBatis Plus是一款針對(duì)MyBatis框架的增強(qiáng)工具,它提供了很多方便的方法來實(shí)現(xiàn)多表聯(lián)查,需要的朋友可以參考下2023-07-07
SpringBoot在一定時(shí)間內(nèi)限制接口請(qǐng)求次數(shù)的實(shí)現(xiàn)示例
在項(xiàng)目中,接口的暴露在外面,很多人就會(huì)惡意多次快速請(qǐng)求,本文主要介紹了SpringBoot在一定時(shí)間內(nèi)限制接口請(qǐng)求次數(shù)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03
Springboot2.7+Minio8 實(shí)現(xiàn)大文件分片上傳
本文主要介紹了Springboot2.7+Minio8 實(shí)現(xiàn)大文件分片上傳,通過文件切片上傳,我們能夠提高文件上傳的速度,優(yōu)化用戶體驗(yàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購物車的方法
這篇文章主要介紹了eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購物車的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
學(xué)習(xí)Java之IO流中有哪些復(fù)雜的API
這篇文章我們要先對(duì)IO流的API有個(gè)基本的認(rèn)知,因?yàn)镮O流的類和方法太多了,我們不得不專門學(xué)習(xí)一下,所以本文就給大家詳細(xì)的講講Java?IO流中復(fù)雜的API,需要的朋友可以參考下2023-09-09
基于java中stack與heap的區(qū)別,java中的垃圾回收機(jī)制的相關(guān)介紹
本篇文章小編將為大家介紹,基于java中stack與heap的區(qū)別,java中的垃圾回收機(jī)制的相關(guān)介紹,需要的可以參考一下2013-04-04
SpringBoot整合Vue實(shí)現(xiàn)微信掃碼支付以及微信退款功能詳解
最近公司要在微信公眾號(hào)上做一個(gè)活動(dòng)預(yù)報(bào)名,活動(dòng)的門票等需要在微信中支付,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Vue實(shí)現(xiàn)微信掃碼支付以及微信退款功能的相關(guān)資料,需要的朋友可以參考下2022-05-05
Java中notify和notifyAll的區(qū)別及何時(shí)使用
本文主要介紹了Java中notify和notifyAll的區(qū)別及何時(shí)使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

