强大的Hibernate分页实现(jsp可以多种表现形式)
1、项目中使用QueryResult(自己定义的类)实现分类,此类用List记录了每页显示的结果,用一个long类型记录了符合条件的结果总数:
QueryResult代码
1 package com.gdpu.page.vo;
2
3 import java.util.List;
4
5 publicclass QueryResult<T> {
6 private List<T> resultlist;
7 privatelong totalrecord;
8
9 public List<T> getResultlist() {
10 return resultlist;
11 }
12 publicvoid setResultlist(List<T> resultlist) {
13 this.resultlist = resultlist;
14 }
15 publiclong getTotalrecord() {
16 return totalrecord;
17 }
18 publicvoid setTotalrecord(long totalrecord) {
19 this.totalrecord = totalrecord;
20 }
21 }
2
3 import java.util.List;
4
5 publicclass QueryResult<T> {
6 private List<T> resultlist;
7 privatelong totalrecord;
8
9 public List<T> getResultlist() {
10 return resultlist;
11 }
12 publicvoid setResultlist(List<T> resultlist) {
13 this.resultlist = resultlist;
14 }
15 publiclong getTotalrecord() {
16 return totalrecord;
17 }
18 publicvoid setTotalrecord(long totalrecord) {
19 this.totalrecord = totalrecord;
20 }
21 }
2、用一个PageView封装了分页数据(从QueryResult里List拿出来的),页码开始索引和结束索引(是一个封装了开始索引和结束索引的类PageIndex,这两个字段就像是百度搜索下端的页码开始和结束索引),总页数totalpage(通过总记录数totalrecord和每页最大记录数maxresult计得),每页记录数maxresult(使用默认值),当前页码(和maxresult触发记录开始索引生成,Hibernate分页setFirstResult(...)参数),总记录数totalrecord(从QueryResult的记录拿出来),页码数量pagecode(比如第一页、上一页、下一页、末页的数量):
PageView代码
1 package com.gdpu.page.vo;
2
3 import java.util.List;
4
5 publicclass PageView<T> {
6 /** 分页数据 **/
7 private List<T> records;
8 /** 页码开始索引和结束索引 **/
9 private PageIndex pageindex;
10 /** 总页数 **/
11 privatelong totalpage =1;
12 /** 每页显示记录数 **/
13 privateint maxresult =10;
14 /** 当前页 **/
15 privateint currentpage =1;
16 /** 总记录数 **/
17 privatelong totalrecord;
18 /** 页码数量 **/
19 privateint pagecode =3;
20 /** 要获取记录的开始索引 **/
21 publicint getFirstResult() {
22 return (this.currentpage-1)*this.maxresult;
23 }
24 publicint getPagecode() {
25 return pagecode;
26 }
27
28 publicvoid setPagecode(int pagecode) {
29 this.pagecode = pagecode;
30 }
31 public PageView(){}
32
33 public PageView(int maxresult, int currentpage) {
34 this.maxresult = maxresult;
35 this.currentpage = currentpage;
36 }
37
38 publicvoid setQueryResult(QueryResult<T> qr){
39 setTotalrecord(qr.getTotalrecord());
40 setRecords(qr.getResultlist());
41 }
42
43 publiclong getTotalrecord() {
44 return totalrecord;
45 }
46 publicvoid setTotalrecord(long totalrecord) {
47 this.totalrecord = totalrecord;
48 setTotalpage(this.totalrecord%this.maxresult==0?this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
49 }
50 public List<T> getRecords() {
51 return records;
52 }
53 publicvoid setRecords(List<T> records) {
54 this.records = records;
55 }
56 public PageIndex getPageindex() {
57 return pageindex;
58 }
59 publiclong getTotalpage() {
60 return totalpage;
61 }
62 publicvoid setTotalpage(long totalpage) {
63 this.totalpage = totalpage;
64 this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
65 }
66 publicint getMaxresult() {
67 return maxresult;
68 }
69 publicint getCurrentpage() {
70 return currentpage;
71 }
72 publicvoid setCurrentpage(int currentpage){
73 this.currentpage=currentpage;
74 }
75 }
2
3 import java.util.List;
4
5 publicclass PageView<T> {
6 /** 分页数据 **/
7 private List<T> records;
8 /** 页码开始索引和结束索引 **/
9 private PageIndex pageindex;
10 /** 总页数 **/
11 privatelong totalpage =1;
12 /** 每页显示记录数 **/
13 privateint maxresult =10;
14 /** 当前页 **/
15 privateint currentpage =1;
16 /** 总记录数 **/
17 privatelong totalrecord;
18 /** 页码数量 **/
19 privateint pagecode =3;
20 /** 要获取记录的开始索引 **/
21 publicint getFirstResult() {
22 return (this.currentpage-1)*this.maxresult;
23 }
24 publicint getPagecode() {
25 return pagecode;
26 }
27
28 publicvoid setPagecode(int pagecode) {
29 this.pagecode = pagecode;
30 }
31 public PageView(){}
32
33 public PageView(int maxresult, int currentpage) {
34 this.maxresult = maxresult;
35 this.currentpage = currentpage;
36 }
37
38 publicvoid setQueryResult(QueryResult<T> qr){
39 setTotalrecord(qr.getTotalrecord());
40 setRecords(qr.getResultlist());
41 }
42
43 publiclong getTotalrecord() {
44 return totalrecord;
45 }
46 publicvoid setTotalrecord(long totalrecord) {
47 this.totalrecord = totalrecord;
48 setTotalpage(this.totalrecord%this.maxresult==0?this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
49 }
50 public List<T> getRecords() {
51 return records;
52 }
53 publicvoid setRecords(List<T> records) {
54 this.records = records;
55 }
56 public PageIndex getPageindex() {
57 return pageindex;
58 }
59 publiclong getTotalpage() {
60 return totalpage;
61 }
62 publicvoid setTotalpage(long totalpage) {
63 this.totalpage = totalpage;
64 this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
65 }
66 publicint getMaxresult() {
67 return maxresult;
68 }
69 publicint getCurrentpage() {
70 return currentpage;
71 }
72 publicvoid setCurrentpage(int currentpage){
73 this.currentpage=currentpage;
74 }
75 }
PageIndex代码
1 package com.gdpu.page.vo;
2
3 publicclass PageIndex {
4 privatelong startindex;
5 privatelong endindex;
6
7 public PageIndex(long startindex, long endindex) {
8 this.startindex = startindex;
9 this.endindex = endindex;
10 }
11 publiclong getStartindex() {
12 return startindex;
13 }
14 publicvoid setStartindex(long startindex) {
15 this.startindex = startindex;
16 }
17 publiclong getEndindex() {
18 return endindex;
19 }
20 publicvoid setEndindex(long endindex) {
21 this.endindex = endindex;
22 }
23
24 publicstatic PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
25 long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
26 long endpage = currentPage+viewpagecount/2;
27 if(startpage<1){
28 startpage =1;
29 if(totalpage>=viewpagecount) endpage = viewpagecount;
30 else endpage = totalpage;
31 }
32 if(endpage>totalpage){
33 endpage = totalpage;
34 if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
35 else startpage =1;
36 }
37 returnnew PageIndex(startpage, endpage);
38 }
39 }
2
3 publicclass PageIndex {
4 privatelong startindex;
5 privatelong endindex;
6
7 public PageIndex(long startindex, long endindex) {
8 this.startindex = startindex;
9 this.endindex = endindex;
10 }
11 publiclong getStartindex() {
12 return startindex;
13 }
14 publicvoid setStartindex(long startindex) {
15 this.startindex = startindex;
16 }
17 publiclong getEndindex() {
18 return endindex;
19 }
20 publicvoid setEndindex(long endindex) {
21 this.endindex = endindex;
22 }
23
24 publicstatic PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
25 long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
26 long endpage = currentPage+viewpagecount/2;
27 if(startpage<1){
28 startpage =1;
29 if(totalpage>=viewpagecount) endpage = viewpagecount;
30 else endpage = totalpage;
31 }
32 if(endpage>totalpage){
33 endpage = totalpage;
34 if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
35 else startpage =1;
36 }
37 returnnew PageIndex(startpage, endpage);
38 }
39 }
3、Action里代码表现形式如下:
Action的分页代码表现形式
1 private List<CgProject> findAllCgProjectDepRole() {
2 pageView=new PageView<CgProject>(10,currentpage);//默认是10条记录,第一页;
3 Teacher teacher=(Teacher)session.getAttribute("teacher");
4
5 QueryResult<CgProject> queryResult=null;
6 if(teacher.getRole()==1){
7 queryResult=chengguoService.findAllCgProject(pageView.getFirstResult(),pageView.getMaxresult());
8 }else{
9 queryResult=chengguoService.findAllCgProjectByTeacherId(teacher.getTeacherId(),pageView.getFirstResult(),pageView.getMaxresult());
10 }
11 pageView.setQueryResult(queryResult);//触发生成页码等等
12 request.setAttribute("pageView", pageView);
13 return queryResult.getResultlist();
14 }
2 pageView=new PageView<CgProject>(10,currentpage);//默认是10条记录,第一页;
3 Teacher teacher=(Teacher)session.getAttribute("teacher");
4
5 QueryResult<CgProject> queryResult=null;
6 if(teacher.getRole()==1){
7 queryResult=chengguoService.findAllCgProject(pageView.getFirstResult(),pageView.getMaxresult());
8 }else{
9 queryResult=chengguoService.findAllCgProjectByTeacherId(teacher.getTeacherId(),pageView.getFirstResult(),pageView.getMaxresult());
10 }
11 pageView.setQueryResult(queryResult);//触发生成页码等等
12 request.setAttribute("pageView", pageView);
13 return queryResult.getResultlist();
14 }
4、jsp分页部分表现形式如下:
jsp分页部分表现形式
1 <font >
2 当前页:第${pageView.currentpage}页 | 总记录数:${pageView.totalrecord}条 | 每页显示:${pageView.maxresult}条 | 总页数:${pageView.totalpage}页</font>
3 <c:choose>
4 <c:when test="${pageView.currentpage==1}"><b><font color="#FFFFFF">第1页</font></c:when>
5 <c:otherwise><a href="javascript:topage('1')">第1页</a></c:otherwise>
6 </c:choose>
7 <c:if test="${pageView.currentpage>2}"><a href="javascript:topage('${pageView.currentpage-1}')">上一页</a></c:if>
8 <c:if test="${pageView.currentpage!=1&&pageView.currentpage!=pageView.totalpage}"><b><font color="#FFFFFF">第${pageView.currentpage}页</font></b></c:if>
9 <c:if test="${pageView.currentpage<pageView.totalpage}"><a href="javascript:topage('${pageView.currentpage+1}')">下一页</a></c:if>
10 <c:choose>
11 <c:when test="${pageView.currentpage<pageView.totalpage}"><a href="javascript:topage('${pageView.totalpage}')">末页</a></c:when>
12 <c:otherwise><b><font color="#FFFFFF">末页</font></c:otherwise>
13 </c:choose>
2 当前页:第${pageView.currentpage}页 | 总记录数:${pageView.totalrecord}条 | 每页显示:${pageView.maxresult}条 | 总页数:${pageView.totalpage}页</font>
3 <c:choose>
4 <c:when test="${pageView.currentpage==1}"><b><font color="#FFFFFF">第1页</font></c:when>
5 <c:otherwise><a href="javascript:topage('1')">第1页</a></c:otherwise>
6 </c:choose>
7 <c:if test="${pageView.currentpage>2}"><a href="javascript:topage('${pageView.currentpage-1}')">上一页</a></c:if>
8 <c:if test="${pageView.currentpage!=1&&pageView.currentpage!=pageView.totalpage}"><b><font color="#FFFFFF">第${pageView.currentpage}页</font></b></c:if>
9 <c:if test="${pageView.currentpage<pageView.totalpage}"><a href="javascript:topage('${pageView.currentpage+1}')">下一页</a></c:if>
10 <c:choose>
11 <c:when test="${pageView.currentpage<pageView.totalpage}"><a href="javascript:topage('${pageView.totalpage}')">末页</a></c:when>
12 <c:otherwise><b><font color="#FFFFFF">末页</font></c:otherwise>
13 </c:choose>
5、至于Hibernate的分页就是调用Session的setFirstResult(int ...)、setMaxResult(int ...)或用hibernateTemplate(需自己扩展)都无所谓。可用此方法分页:http://blog.sina.com.cn/s/blog_7096a4800100pk99.html
此分页源码请到以下连接下载:http://download.csdn.net/detail/fzx888888/4141140