springmvc4+hibernate4分頁查詢功能實(shí)現(xiàn)
Springmvc+hibernate成為現(xiàn)在很多人用的框架整合,最近自己也在學(xué)習(xí)摸索,由于我們?cè)陂_發(fā)項(xiàng)目中很多項(xiàng)目都用到列表分頁功能,在此參考網(wǎng)上一些資料,以springmvc4+hibnerate4邊學(xué)邊總結(jié),得出分頁功能代碼,雖然不一定通用,對(duì)于初學(xué)者來說有參考價(jià)值。
分頁實(shí)現(xiàn)的基本過程:
一、分頁工具類
思路:
1.編寫Page類,定義屬性,應(yīng)該包括:查詢結(jié)果集合、查詢記錄總數(shù)、每頁顯示記錄數(shù)、當(dāng)前第幾頁等屬性。
2.編寫Page類,定義方法,應(yīng)該包括:總頁數(shù)、當(dāng)前頁開始記錄、首頁、下一頁、上一頁、末頁等方法
代碼如下:
package cn.myic.model;
import java.util.List;
public class Page<E> {
// 結(jié)果集
private List<E> list;
// 查詢記錄總數(shù)
private int totalRecords;
// 每頁多少條記錄
private int pageSize;
// 第幾頁
private int pageNo;
/**
* @return 總頁數(shù)
* */
public int getTotalPages(){
return (totalRecords+pageSize-1)/pageSize;
}
/**
* 計(jì)算當(dāng)前頁開始記錄
* @param pageSize 每頁記錄數(shù)
* @param currentPage 當(dāng)前第幾頁
* @return 當(dāng)前頁開始記錄號(hào)
*/
public int countOffset(int currentPage,int pageSize){
int offset = pageSize*(currentPage-1);
return offset;
}
/**
* @return 首頁
* */
public int getTopPageNo(){
return 1;
}
/**
* @return 上一頁
* */
public int getPreviousPageNo(){
if(pageNo<=1){
return 1;
}
return pageNo-1;
}
/**
* @return 下一頁
* */
public int getNextPageNo(){
if(pageNo>=getBottomPageNo()){
return getBottomPageNo();
}
return pageNo+1;
}
/**
* @return 尾頁
* */
public int getBottomPageNo(){
return getTotalPages();
}
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
二、Dao層方法
思路:定義一個(gè)分頁查詢的方法,設(shè)置參數(shù):當(dāng)頁頁號(hào)和每頁顯示多少條記錄
代碼如下:
/**
* 分頁查詢
* @param hql 查詢的條件
* @param offset 開始記錄
* @param length 一次查詢幾條記錄
* @return 返回查詢記錄集合
*/
@SuppressWarnings("unchecked")
@Override
public List<Course> queryForPage(int offset, int length) {
// TODO Auto-generated method stub
List<Course> entitylist=null;
try{
Query query = getSession().createQuery("from Course");
query.setFirstResult(offset);
query.setMaxResults(length);
entitylist = query.list();
}catch(RuntimeException re){
throw re;
}
return entitylist;
}
三、Service層方法
思路:
1.定義一個(gè)分頁查詢的方法,設(shè)置參數(shù):當(dāng)頁頁號(hào)和每頁顯示多少條記錄,返回查詢結(jié)果的分頁類對(duì)象(Page)
2.通過Dao層,獲取查詢實(shí)體的總記錄數(shù)
3.獲取當(dāng)前頁開始記錄數(shù)
4.通過Dao層,獲取分頁查詢結(jié)果集
5.Set入page對(duì)象
代碼如下:
/**
* 分頁查詢
* @param currentPage 當(dāng)前頁號(hào):現(xiàn)在顯示的頁數(shù)
* @param pageSize 每頁顯示的記錄條數(shù)
* @return 封閉了分頁信息(包括記錄集list)的Bean
* */
@SuppressWarnings("unchecked")
@Override
public Page queryForPage(int currentPage,int pageSize) {
// TODO Auto-generated method stub
Page page = new Page();
//總記錄數(shù)
int allRow = courseDao.getAllRowCount();
//當(dāng)前頁開始記錄
int offset = page.countOffset(currentPage,pageSize);
//分頁查詢結(jié)果集
List<Course> list = courseDao.queryForPage(offset, pageSize);
page.setPageNo(currentPage);
page.setPageSize(pageSize);
page.setTotalRecords(allRow);
page.setList(list);
return page;
}
四、Controller層方法
Controller層的設(shè)計(jì),操作翻頁查詢時(shí),只需要傳遞當(dāng)前頁號(hào)參數(shù)即可。
代碼如下:
@RequestMapping(value = "/showAll.do")
public String findAllCourse(HttpServletRequest request,
HttpServletResponse response) {
try {
String pageNo = request.getParameter("pageNo");
if (pageNo == null) {
pageNo = "1";
}
Page page = courseService.queryForPage(Integer.valueOf(pageNo), 10);
request.setAttribute("page", page);
List<Course> course = page.getList();
request.setAttribute("courses", course);
} catch (Exception e) {
e.printStackTrace();
}
return "course/course_list";
}
五、View層jsp展示
jsp頁面分頁的幾個(gè)按鈕,根據(jù)當(dāng)前頁號(hào)的判斷顯示。
代碼如下:
<tr>
<td colspan="6" align="center" bgcolor="#5BA8DE">共${page.totalRecords}條記錄 共${page.totalPages}頁 當(dāng)前第${page.pageNo}頁<br>
<a href="${path}/course/showAll.do?pageNo=${page.topPageNo }"><input type="button" name="fristPage" value="首頁" /></a>
<c:choose>
<c:when test="${page.pageNo!=1}">
<a href="${path}/course/showAll.do?pageNo=${page.previousPageNo }"><input type="button" name="previousPage" value="上一頁" /></a>
</c:when>
<c:otherwise>
<input type="button" disabled="disabled" name="previousPage" value="上一頁" />
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.pageNo != page.totalPages}">
<a href="${path}/course/showAll.do?pageNo=${page.nextPageNo }"><input type="button" name="nextPage" value="下一頁" /></a>
</c:when>
<c:otherwise>
<input type="button" disabled="disabled" name="nextPage" value="下一頁" />
</c:otherwise>
</c:choose>
<a href="${path}/course/showAll.do?pageNo=${page.bottomPageNo }"><input type="button" name="lastPage" value="尾頁" /></a>
</td>
</tr>
頁面效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何實(shí)現(xiàn)海量數(shù)據(jù)判重
在海量數(shù)據(jù)如何確定一個(gè)值是否存在?這是一道非常經(jīng)典的面試場(chǎng)景題,那怎么回答這個(gè)問題呢?下面小編就來和大家詳細(xì)的聊一聊,感興趣的可以一起學(xué)習(xí)一下2023-09-09
SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鏈路追蹤功能
Spring Boot默認(rèn)使用LogBack日志系統(tǒng),并且已經(jīng)引入了相關(guān)的jar包,所以我們無需任何配置便可以使用LogBack打印日志。這篇文章主要介紹了SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鏈路追蹤功能,需要的朋友可以參考下2019-10-10
Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)
這篇文章主要介紹了Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot結(jié)合redis實(shí)現(xiàn)搜索欄熱搜功能及文字過濾
本文主要介紹了springboot結(jié)合redis實(shí)現(xiàn)搜索欄熱搜功能及文字過濾,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
如何使用HttpClient發(fā)送java對(duì)象到服務(wù)器
這篇文章主要介紹了如何使用HttpClient發(fā)送java對(duì)象到服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

