分页技术

来源:互联网 发布:oracl 显示所有数据库 编辑:程序博客网 时间:2024/05/20 14:41

(一)为什么使用分页技术

  Jsp页面显示数据,数据库有多少条记录,就显示多少条?

  1. 页面冗长,不好排版
  2. 页面打开慢,因为查询的数据多

  使用分页技术解决:每次显示指定的记录数据,点击首页、上一页、下一页、尾页实现页面数据的更换!

  (二)分页原理:

    


  (三)分页过程

    1.设计javaBean  

    分页的javabean对象,用户封装分页数据、业务。  

public class PageBean {private int pageCount = 3;//每页显示多少行, 默认3行private int currentPage=1;// 当前页, 默认显示第一页,即1-3行private int totalCount;// 总记录数private int totalPage;// 总页数: 由总记录数/每页显示多少行,得出private List<Employee> listEmp; // 一页显示的记录的集合public int getTotalPage() {// 根据总记录数,计算总页数(7,3)if (totalCount % pageCount == 0) {totalPage = totalCount / pageCount;} else {totalPage = totalCount / pageCount + 1;}return totalPage;}    ......}
  2.实现dao

  员工信息,数据访问操作。

/** * 员工信息,数据访问操作 * @author Shuaigexiaobo * */public class EmployeeDao {private Connection con;private PreparedStatement pstmt;private ResultSet rs;/** * 根据页数查询数据, 查询到数据后,设置到pageBean的属性中  * @param pageBean */public void getPageBeanData(PageBean pageBean){// 集合保存查询到的数据List<Employee> list = new ArrayList<Employee>();// 查询开始行int start = (pageBean.getCurrentPage()-1) * pageBean.getPageCount(); int count = pageBean.getPageCount();//分页sql语句String sql = "SELECT * FROM employee LIMIT ?,?";try {// 获取连接con = JdbcUtil.getConnection();// 获取stmt对象pstmt = con.prepareStatement(sql);// 设置值pstmt.setInt(1, start);pstmt.setInt(3, count);// 执行查询的sql语句rs = pstmt.executeQuery();while (rs.next()) {// 遍历每一行记录,封转为对象,添加到集合中Employee emp = new Employee();emp.setEmpNo(rs.getInt("empNo"));emp.setEmpName(rs.getString("empName"));emp.setSalary(rs.getDouble("salary"));emp.setAge(rs.getInt("age"));// 设置部门信息emp.setDeptId(rs.getInt("deptid"));emp.setDeptName(rs.getString("deptName"));// 添加到集合list.add(emp);}// 把封装后的list集合,设置到javabean对象属性中pageBean.setListEmp(list);// 调用方法,查询总记录数this.getTotalCount(pageBean);} catch (Exception e) {throw new RuntimeException(e);} finally {JdbcUtil.closeAll(con, pstmt, rs);}}// 分页用: 查询总记录数public void getTotalCount(PageBean pageBean) {// 查询sql语句//分页sql语句String sql = "SELECT count(1) FROM employee"try {// 获取连接con = JdbcUtil.getConnection();// 获取stmt对象pstmt = con.prepareStatement(sql);// 执行查询rs = pstmt.executeQuery();// 读取结果if (rs.next()){pageBean.setTotalCount(rs.getInt(1));}} catch (Exception e) {throw new RuntimeException(e);} finally {JdbcUtil.closeAll(con, pstmt, rs);}}}

3.Servlet的编写

public class IndexServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 获取sessionHttpSession session = request.getSession();// 实例化dao对象EmployeeDao empDao = new EmployeeDao();// 分页Servlet处理:1. 创建分页javabean对象; 2. 保存到域中; 3. 跳转// 获取分数参数:当前页String curr = request.getParameter("currentPage");int currentPage = (curr == null || curr == "") ? 1 : Integer.parseInt(curr);// 1. 创建分页对象// 先获取PageBean pageBean = (PageBean) session.getAttribute("pageBean");// 判断: 第一次运行时候,创建分页对象if (pageBean == null) {pageBean = new PageBean();}// 把页面查询参数,即页数,设置到pageBean中pageBean.setCurrentPage(currentPage);// 2. 调用方法,进行分页查询empDao.getPageBeanData(pageBean);// 保存到域中session.setAttribute("pageBean", pageBean);// 3. 跳转response.sendRedirect(request.getContextPath() + "/index.jsp");}}

4.前端页面的显示

<body>    <!-- 分页页面列表处理、显示: -->            <table border="1" width="80%" align="center" cellpadding="3" cellspacing="0">    <tr>    <td>编号</td>    <td>员工名称</td>    <td>年龄</td>    <td>薪水</td>    <td>部门名称</td>    </tr>    <!-- 循环遍历:pageBean中的list -->    <c:if test="${not empty sessionScope.pageBean.listEmp}">    <c:forEach var="emp" items="${sessionScope.pageBean.listEmp}">    <tr>    <td>${emp.empNo }</td>    <td>${emp.empName }</td>    <td>${emp.age }</td>    <td>${emp.salary }</td>    <td>${emp.deptName }</td>    </tr>    </c:forEach>        </c:if>        <tr>    <td colspan="5" align="center">    <!-- 当前页为首页:不能点击首页、上一页;      当前页为末页: 不能点击下一页、末页    当前页其他页:    首页、上下、末页都可以点击     -->          <c:choose>     <c:when test="${sessionScope.pageBean.currentPage == 1}">     首页  上一页     <a href="${pageContext.request.contextPath }/indexServlet?currentPage=${sessionScope.pageBean.currentPage + 1}">下一页</a>    <a href="${pageContext.request.contextPath }/indexServlet?currentPage=${sessionScope.pageBean.totalPage}">末页</a>     </c:when>     <c:when test="${sessionScope.pageBean.currentPage == sessionScope.pageBean.totalPage}">     <a href="${pageContext.request.contextPath }/indexServlet?currentPage=1">首页</a>    <a href="${pageContext.request.contextPath }/indexServlet?currentPage=${sessionScope.pageBean.currentPage - 1}">上一页</a>     下一页  末页     </c:when>     <c:otherwise>    <a href="${pageContext.request.contextPath }/indexServlet?currentPage=1">首页</a>    <a href="${pageContext.request.contextPath }/indexServlet?currentPage=${sessionScope.pageBean.currentPage - 1}">上一页</a>    <a href="${pageContext.request.contextPath }/indexServlet?currentPage=${sessionScope.pageBean.currentPage + 1}">下一页</a>    <a href="${pageContext.request.contextPath }/indexServlet?currentPage=${sessionScope.pageBean.totalPage}">末页</a>     </c:otherwise>     </c:choose>            </td>        </tr></table></body>