(以下使用ssm+jsp实现显示所有电影为例子)
1.封装一个PageBean类
package com.entity;import java.util.List;public class PageBean{
//已知数据
private int pageNum; //当前页,从请求那边传过来。
private int pageSize; //每页显示的数据条数。
private int totalRecord; //总的记录条数。查询数据库得到的数据
//需要计算得来
private int totalPage; //总页数,通过totalRecord和pageSize计算可以得来
//开始索引,也就是我们在数据库中要从第几行数据开始拿,有了startIndex和pageSize,
//就知道了limit语句的两个数据,就能获得每页需要显示的数据了
private int startIndex;
//将每页要显示的数据放在list集合中
private List list;
//分页显示的页数,比如在页面上显示1,2,3,4,5页,start就为1,end就为5,这个也是算过来的
private int start;
private int end;
//通过pageNum,pageSize,totalRecord计算得来tatalPage和startIndex
//构造方法中将pageNum,pageSize,totalRecord获得
public PageBean(int pageNum,int pageSize,int totalRecord) {
this.pageNum = pageNum;
this.pageSize = pageSize;
this.totalRecord = totalRecord;
//totalPage 总页数
if(totalRecord%pageSize==0){
//说明整除,正好每页显示pageSize条数据,没有多余一页要显示少于pageSize条数据的
this.totalPage = totalRecord / pageSize;
}else{
//不整除,就要在加一页,来显示多余的数据。
this.totalPage = totalRecord / pageSize +1;
}
//开始索引
this.startIndex = (pageNum-1)*pageSize ;
//显示10页,这里自己可以设置,想显示几页就自己通过下面算法修改
this.start = 1;
this.end = 10;
//显示页数的算法
if(totalPage <=10){
//总页数都小于10,那么end就为总页数的值了。
this.end = this.totalPage;
}else{
//总页数大于10,那么就要根据当前是第几页,来判断start和end为多少了,
this.start = pageNum - 4;
this.end = pageNum + 5;
if(start < 1){
//前4页不符合这个规则,
this.start = 1;
this.end = 10;
}
if(end > this.totalPage){
//比如当前页是倒数第2页或者最后一页,也同样不符合上面这个规则
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文件(数据库操作)
//获取所有电影
<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为当前页码
List alllist=movieDao.getAllMovies();//获取所有电影
int totalRecord=alllist.size();//取得电影总数
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);//当页电影信息
pb.setList(pageList);
return pb;
}
5.Controller
public String getAllMovie(HttpServletRequest request,
@RequestParam(value="pageNum",defaultValue="1") int pageNum) throws IOException{
int pageSize=10;//设置一页显示电影数
PageBean pb=movieService.getPageMovie(pageNum, pageSize);
request.setAttribute("pageBean", pb);
return "ShowAllMovie";
}
6.jsp页面
<--%-- 构建分页导航 --%-->
共有${requestScope.pageBean.totalRecord}部电影,共${requestScope.pageBean.totalPage }页
<br/>
<a href="${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=1">首页</a>
<--%--如果当前页为第一页时,就没有上一页这个超链接显示 --%-->
<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>
<--%--如果当前页不是第一页也不是最后一页,则有上一页和下一页这个超链接显示 --%-->
<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>
<--%-- 如果当前页是最后一页,则只有上一页这个超链接显示,下一页没有 --%-->
<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>