spring+springmvc+hibernate,实现分页功能

来源:互联网 发布:韩子高网络剧有下部吗 编辑:程序博客网 时间:2024/05/19 12:38

效果图

核心代码如下:

pagebean.java

import java.util.List;public class PageBean<T> {    //已知数据    private int pageNum;    //当前页,从请求那边传过来。    private int pageSize;    //每页显示的数据条数。    private int totalRecord;    //总的记录条数。查询数据库得到的数据        //需要计算得来    private int totalPage;    //总页数,通过totalRecord和pageSize计算可以得来    //开始索引,也就是我们在数据库中要从第几行数据开始拿,有了startIndex和pageSize,    //就知道了limit语句的两个数据,就能获得每页需要显示的数据了    private int startIndex;                        //将每页要显示的数据放在list集合中    private List<T> 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 ;        //显示5页,这里自己可以设置,想显示几页就自己通过下面算法修改        this.start = 1;        this.end = 5;        //显示页数的算法        if(totalPage <=5){            //总页数都小于5,那么end就为总页数的值了。            this.end = this.totalPage;        }else{            //总页数大于5,那么就要根据当前是第几页,来判断start和end为多少了,            this.start = pageNum - 2;            this.end = pageNum + 2;                        if(start < 0){                //比如当前页是第1页,或者第2页,那么就不如和这个规则,                this.start = 1;                this.end = 5;            }            if(end > this.totalPage){                //比如当前页是倒数第2页或者最后一页,也同样不符合上面这个规则                this.end = totalPage;                this.start = end - 5;            }        }    }    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 List<T> getList() {        return list;    }    public void setList(List<T> list) {        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;    }    }

controller层

@RequestMapping("findAllStudentWithPage.do")public ModelAndView findAllStudentWithPage(HttpServletRequest request, HttpServletResponse response){System.out.println("findAllStudentWithPage");String pageNum = request.getParameter("pageNum");if(pageNum == null){pageNum="1";}PageBean<Student> pb = service.findAllStudentWithPage(Integer.parseInt(pageNum),pageSize);ModelAndView model = new ModelAndView();model.addObject("pageBean", pb);model.setViewName("queryAll");return model;}

service层

public PageBean<Student> findAllStudentWithPage(int pageNum, int pageSize){List<Student> allStudent = stuDaoImpl.queryAll();int totalRecord = allStudent.size();PageBean<Student> pb = new PageBean<Student>(pageNum, pageSize, totalRecord);int startIndex = pb.getStartIndex();pb.setList(stuDaoImpl.findAll(startIndex,pageSize));return pb;}

dao层

public List<Student> findAll(int startIndex, int pageSize) {System.out.println(" enter findAll by pageNum and pageSize");Session session = sessionFactory.openSession();session.beginTransaction();//HQL语句在查询时不能使用limit关键字Query query = session.createQuery("from Student").setFirstResult(startIndex).setMaxResults(pageSize);List<Student> list = query.list();for(Student s : list){System.out.println(s);}session.close();return list;}

前台页面

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script></head><body>**************分割线*****************************<table id="show_tab" border="1"><tr class="trhead" id="show_tab_one"><th>id</th><th>username</th></tr><c:forEach items="${requestScope.pageBean.list}" var="li"><tr class="trhead" id="show_tab_one"><th>${li.id}</th><th>${li.username}</th></tr></c:forEach></table><%-- 构建分页导航 --%>共有${requestScope.pageBean.totalRecord}个员工,共${requestScope.pageBean.totalPage }页,当前为${requestScope.pageBean.pageNum}页<br /><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?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}"><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?pageNum=${i}">${i}</a></c:if></c:forEach><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?pageNum=${requestScope.pageBean.pageNum+1}">下一页</a></c:if><%--如果当前页不是第一页也不是最后一页,则有上一页和下一页这个超链接显示 --%><c:iftest="${requestScope.pageBean.pageNum > 1 && requestScope.pageBean.pageNum < requestScope.pageBean.totalPage}"><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?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}"><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?pageNum=${i}">${i}</a></c:if></c:forEach><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?pageNum=${requestScope.pageBean.pageNum+1}">下一页</a></c:if><%-- 如果当前页是最后一页,则只有上一页这个超链接显示,下一页没有 --%><c:iftest="${requestScope.pageBean.pageNum == requestScope.pageBean.totalPage}"><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?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}"><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?pageNum=${i}">${i}</a></c:if></c:forEach></c:if><%--尾页 --%><ahref="${pageContext.request.contextPath}/findAllStudentWithPage.do?pageNum=${requestScope.pageBean.totalPage}">尾页</a></body></html>





原创粉丝点击