Springmvc+hibernate成為現(xiàn)在很多人用的框架整合,最近自己也在學習摸索,由于我們在開發(fā)項目中很多項目都用到列表分頁功能,在此參考網(wǎng)上一些資料,以springmvc4+hibnerate4邊學邊總結(jié),得出分頁功能代碼,雖然不一定通用,對于初學者來說有參考價值。
分頁實現(xiàn)的基本過程:
一、分頁工具類
思路:
1.編寫Page類,定義屬性,應該包括:查詢結(jié)果集合、查詢記錄總數(shù)、每頁顯示記錄數(shù)、當前第幾頁等屬性。
2.編寫Page類,定義方法,應該包括:總頁數(shù)、當前頁開始記錄、首頁、下一頁、上一頁、末頁等方法
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
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; } /** * 計算當前頁開始記錄 * @param pageSize 每頁記錄數(shù) * @param currentPage 當前第幾頁 * @return 當前頁開始記錄號 */ 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層方法
思路:定義一個分頁查詢的方法,設(shè)置參數(shù):當頁頁號和每頁顯示多少條記錄
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * 分頁查詢 * @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.定義一個分頁查詢的方法,設(shè)置參數(shù):當頁頁號和每頁顯示多少條記錄,返回查詢結(jié)果的分頁類對象(Page)
2.通過Dao層,獲取查詢實體的總記錄數(shù)
3.獲取當前頁開始記錄數(shù)
4.通過Dao層,獲取分頁查詢結(jié)果集
5.Set入page對象
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/** * 分頁查詢 * @param currentPage 當前頁號:現(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(); //當前頁開始記錄 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è)計,操作翻頁查詢時,只需要傳遞當前頁號參數(shù)即可。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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頁面分頁的幾個按鈕,根據(jù)當前頁號的判斷顯示。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<tr> <td colspan= "6" align= "center" bgcolor= "#5BA8DE" >共${page.totalRecords}條記錄 共${page.totalPages}頁 當前第${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> |
頁面效果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/luihengk/p/5248041.html