(以下使用ssm+jsp實現(xiàn)顯示所有電影為例子)
1.封裝一個PageBean類
package com.entity;import java.util.List;public class PageBean{
//已知數(shù)據(jù) ? ?
private int pageNum;? ? //當(dāng)前頁,從請求那邊傳過來睁搭。? ??
private int pageSize;? ? //每頁顯示的數(shù)據(jù)條數(shù)揽浙。? ??
private int totalRecord;? ? //總的記錄條數(shù)。查詢數(shù)據(jù)庫得到的數(shù)據(jù)? ? ? ??
//需要計算得來? ??
private int totalPage; ???//總頁數(shù),通過totalRecord和pageSize計算可以得來? ??
//開始索引滋捶,也就是我們在數(shù)據(jù)庫中要從第幾行數(shù)據(jù)開始拿,有了startIndex和pageSize堰酿,? ?
?//就知道了limit語句的兩個數(shù)據(jù)良狈,就能獲得每頁需要顯示的數(shù)據(jù)了? ??
private int startIndex;? ? ? ? ? ? ? ? ? ? ? ??
//將每頁要顯示的數(shù)據(jù)放在list集合中? ??
private List list;? ? ? ?
?//分頁顯示的頁數(shù),比如在頁面上顯示1,2黍特,3蛙讥,4,5頁灭衷,start就為1次慢,end就為5,這個也是算過來的 ??
private int start;? ??
private int end;? ? ? ??
//通過pageNum翔曲,pageSize经备,totalRecord計算得來tatalPage和startIndex? ?
?//構(gòu)造方法中將pageNum,pageSize部默,totalRecord獲得? ??
public PageBean(int pageNum,int pageSize,int totalRecord) {
? ? ? ?this.pageNum = pageNum;
? ? ? ?this.pageSize = pageSize;
? ? ? ?this.totalRecord = totalRecord;
? ? ? ? //totalPage 總頁數(shù) ? ? ? ?
? ? ? ? if(totalRecord%pageSize==0){
? ? ? ? ? ?//說明整除侵蒙,正好每頁顯示pageSize條數(shù)據(jù)瘤泪,沒有多余一頁要顯示少于pageSize條數(shù)據(jù)的?
? ? ? ? ? this.totalPage = totalRecord / pageSize;
?? ? ? }else{
? ? ? ? ? //不整除趟脂,就要在加一頁,來顯示多余的數(shù)據(jù)凳谦。
? ? ? ? ? ? this.totalPage = totalRecord / pageSize +1;
? ? ? ? }
? ? ? ?//開始索引
? ? ? ? this.startIndex = (pageNum-1)*pageSize ;
? ? ? ? //顯示10頁,這里自己可以設(shè)置犁功,想顯示幾頁就自己通過下面算法修改
? ? ? ? this.start = 1;
? ? ? ? this.end = 10;
? ? ? ? //顯示頁數(shù)的算法
? ? ? ? if(totalPage <=10){
? ? ? ? ? ? //總頁數(shù)都小于10氓轰,那么end就為總頁數(shù)的值了。
? ? ? ? ? ? this.end = this.totalPage;?
? ? ? ? ?}else{?
?? ? ? ? ? //總頁數(shù)大于10浸卦,那么就要根據(jù)當(dāng)前是第幾頁署鸡,來判斷start和end為多少了,
? ? ? ? ? ? this.start = pageNum - 4;
? ? ? ? ? ? this.end = pageNum + 5;
? ? ? ? ? ? if(start < 1){?
? ? ? ? ? ? ? ? ?//前4頁不符合這個規(guī)則限嫌,
? ? ? ? ? ? ? ? ?this.start = 1;
? ? ? ? ? ? ? ? ?this.end = 10;?
? ? ? ? ? ? ?}
? ? ? ? ? ? if(end > this.totalPage){
? ? ? ? ? ? ? ? //比如當(dāng)前頁是倒數(shù)第2頁或者最后一頁靴庆,也同樣不符合上面這個規(guī)則
? ? ? ? ? ? ? ? this.end = totalPage;?
?? ? ? ? ? ? ? this.start = end - 10;?
?? ? ? ? ? }
? ? ? ? }
? ? }
//getting,setting
public int getPageNum() {return pageNum;}
public void setPageNum(int pageNum) {this.pageNum = pageNum;}
public int getPageSize() {return pageSize;}
public void setPageSize(int pageSize) {this.pageSize = pageSize;}
public int getTotalRecord() {return totalRecord;}
public void setTotalRecord(int totalRecord) {this.totalRecord = totalRecord;}
public int getTotalPage() {return totalPage;}
public void setTotalPage(int totalPage) {this.totalPage = totalPage;}
public int getStartIndex() {return startIndex;}
public void setStartIndex(int startIndex) {this.startIndex = startIndex;}
public ListgetList() {return list;}
public void setList(Listlist) {this.list = list;}
public int getStart() {return start;}
public void setStart(int start) {this.start = start;}
public int getEnd() {return end;}
public void setEnd(int end) {this.end = end;}
}
2.mybatis.xml文件(數(shù)據(jù)庫操作)
//獲取所有電影
<select id="getAllMovie" resultType="com.entity.Movie">
? ? select * from movie
</select>
//獲取分頁電影
<select id="getPageMovie" parameterType="com.entity.SelectCondition" resultType="com.entity.Movie">
? ? select * from movie limit #{startIndex},#{pageSize}
</select>
3.DAO層
//獲取所有電影
public ListgetAllMovies() throws IOException{
List list=sqlSession.selectList("getAllMovie");
return list;
}
//? 獲取分頁電影
//SelectCondition為一個類,用于封裝多個查詢條件怒医,這里包含startIndex炉抒、pageSize兩個條件
public ListgetPageMovie(SelectCondition selectCondition) throws IOException{
List list= sqlSession.selectList("getPageMovie",selectCondition);
return list;
}
4.servier層
public PageBean getPageMovie(int pageNum,int pageSize) throws IOException{
//pageNum為當(dāng)前頁碼
List alllist=movieDao.getAllMovies();//獲取所有電影
int totalRecord=alllist.size();//取得電影總數(shù)
PageBean pb = new PageBean(pageNum, pageSize, totalRecord);
int startIndex=pb.getStartIndex();
SelectCondition selectCondition=new SelectCondition();
selectCondition.setStartIndex(startIndex);
selectCondition.setPageSize(pageSize);
List pageList=movieDao.getPageMovie(selectCondition);//當(dāng)頁電影信息
pb.setList(pageList);
return pb;
}
5.Controller
public String getAllMovie(HttpServletRequest request,
@RequestParam(value="pageNum",defaultValue="1") int pageNum) throws IOException{
int pageSize=10;//設(shè)置一頁顯示電影數(shù)
PageBean pb=movieService.getPageMovie(pageNum, pageSize);
request.setAttribute("pageBean", pb);
return "ShowAllMovie";
}
6.jsp頁面
<--%-- 構(gòu)建分頁導(dǎo)航 --%-->
共有${requestScope.pageBean.totalRecord}部電影,共${requestScope.pageBean.totalPage }頁
<br/>
<a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=1">首頁</a>
<--%--如果當(dāng)前頁為第一頁時稚叹,就沒有上一頁這個超鏈接顯示 --%-->
<c:if test="${requestScope.pageBean.pageNum ==1}">
? ? <c:forEach begin="${requestScope.pageBean.start}" end="${requestScope.pageBean.end}" step="1" var="i">
? ? ? ? <c:if test="${requestScope.pageBean.pageNum == i}">${i}</c:if>
? ? ? ? <c:if test="${requestScope.pageBean.pageNum != i}">
? ? ? ? ? ? <a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${i}">${i}</a>
? ? ? ? </c:if>
</c:forEach>
? ? <a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum+1}">下一頁</a>
</c:if>
<--%--如果當(dāng)前頁不是第一頁也不是最后一頁焰薄,則有上一頁和下一頁這個超鏈接顯示 --%-->
<c:if test="${requestScope.pageBean.pageNum > 1 && requestScope.pageBean.pageNum <requestScope.pageBean.totalPage}">
? ? <a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum-1}">上一頁</a>
? ? <c:forEach begin="${requestScope.pageBean.start}" end="${requestScope.pageBean.end}" step="1" var="i">
? ? ? ? <c:if test="${requestScope.pageBean.pageNum == i}">${i}</c:if>
? ? ? ? <c:if test="${requestScope.pageBean.pageNum != i}">
? ? ? ? ? ? <a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${i}">${i}</a>
? ? ? ? </c:if>
? ? </c:forEach>
? ? <a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum+1}">下一頁</a>
</c:if>
<--%-- 如果當(dāng)前頁是最后一頁,則只有上一頁這個超鏈接顯示扒袖,下一頁沒有 --%-->
<c:if test="${requestScope.pageBean.pageNum == requestScope.pageBean.totalPage}">
? ? <a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum-1}">上一頁</a>
? ? <c:forEach begin="${requestScope.pageBean.start}" end="${requestScope.pageBean.end}" step="1" var="i">
? ? ? ? <c:if test="${requestScope.pageBean.pageNum == i}">${i}</c:if>
? ? ? ? <c:if test="${requestScope.pageBean.pageNum != i}">
? ? ? ? ? ? <a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${i}">${i}</a>
? ? ? ? </c:if>
? ? </c:forEach>
</c:if>
<--%--尾頁 --%-->
<a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.totalPage}">尾頁</a>