美文网首页
2018-03-04 web用户项目(三):实现用户信息分页查询

2018-03-04 web用户项目(三):实现用户信息分页查询

作者: 紫杉叶子 | 来源:发表于2018-03-04 12:16 被阅读0次

    笔记如下

    • 思路:
      1.每一页里有十条记录,每页就是一个PageBean,封装了总记录数,当前是第几页,每页条数,当前是第几页的数据
    2018-03-01_214242.png

    2.两个公式


    2018-03-01_214242.png
    • *jsp
    <body>
        <c:if test="${empty pageBean.customers}">
            当前页没有数据
        </c:if>
        <c:if test="${not empty pageBean.customers}">
            <h3 style="text-align: center; ">当前是${pageBean.pageNum}页数据</h3>
            <table border="1" width="100%"> 
                <tr>
                    <th>客户姓名</th>
                    <th>客户性别</th>
                    <th>客户生日</th>
                    <th>客户邮箱</th>
                    <th>客户手机</th>
                    <th>客户爱好</th>
                    <th>客户类型</th>
                    <th>客户描述</th>
                </tr>
                <c:forEach items="${pageBean.customers}" var="customer">
                    <tr>
                        
                        <td>${customer.name}</td>
                        <td>${customer.gender}</td>
                        <td>${customer.birthday}</td>
                        <td>${customer.email}</td>
                        <td>${customer.cellphone}</td>
                        <td>${customer.preference}</td>
                        <td>${customer.type}</td>
                        <td>${customer.description}</td>
                    </tr>
                
                </c:forEach>
            </table>
            <br/>
            <div style="text-align: center; ">
                <c:if test="${pageBean.pageNum != 1 }">
                    <a href="${pageContext.request.contextPath}/pagequery?pagenum=1">首页</a> 
                    <a  href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.pageNum-1}">上一页</a> 
                </c:if>
                <!-- 参考百度左五右四 -->
                <c:forEach begin="${pageBean.pageNum-5>0?pageBean.pageNum-5:1}" end="${pageBean.pageNum+4<pageBean.totalPageNum?pageBean.pageNum+4:pageBean.totalPageNum}" var="i">
                    <c:if test="${pageBean.pageNum == i}">
                        <font color="red">${i}</font>
                    </c:if><c:if test="${pageBean.pageNum != i}">
                        
                        <a href="${pageContext.request.contextPath}/pagequery?pagenum=${i}">${i}</a>
                    </c:if>
                
                </c:forEach>
                
                <c:if test="${pageBean.pageNum != pageBean.totalPageNum }">
                    <a  href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.pageNum+1}">下一页</a> 
                    <a href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.totalPageNum}">尾页</a> 
                </c:if>
            </div>
            
        </c:if>
        
    </body>
    
    • web层(PageQueryServlet.java)
    //拿到需要看到页是那一页
            String pagenum = request.getParameter("pagenum");
            
            //调用业务层去查询目标页的数据
            CustomerService cs = new CustomerService();
            
            //返回当前页的目标数据是不够的,还要做导航条
            //为了做分页,需要引入一个新的javaBean
            PageBean pageBean =  cs.pageQuery(pagenum);
            
            //将pagebean存到request域中
            request.setAttribute("pageBean", pageBean);
            request.getRequestDispatcher("/bean.jsp").forward(request, response);
    
    • 业务层(CustomerService.java)
    //完成分页查询的业务方法:
        public PageBean pageQuery(String num) {
            
            int numberPerPage = 10;//默认每页十条
            
            int pageNum = Integer.parseInt(num);//当前页是那一页
            
              
              //从数据库里查找总记录数
            int totalRecordsCount = cdao.getToltalCount();
            
            //运用公式
            int totalPageNum = (totalRecordsCount + numberPerPage-1)/numberPerPage;//总页数
            
            //select * from customers limit ?,?
            int startIndex = (pageNum-1)*numberPerPage;
    
            //从数据库里查找10条记录
            List<Customer> customers = cdao.getCurrectPageData(startIndex,numberPerPage);
            
            //封装数据
            PageBean pageBean = new PageBean();
            pageBean.setNumberPerPage(numberPerPage);
            pageBean.setCustomers(customers);
            pageBean.setPageNum(pageNum);
            pageBean.setTotalPageNum(totalPageNum);
            pageBean.setTotalRecordsCount(totalRecordsCount);
            
            return pageBean;
        }
    

    dao层(CustomerDaoImpl.java)

        //获得总记录条数
        @Override
        public int getToltalCount() {
            
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
            
            try {
             long count =  (long) runner.query("select count(*) from customers", new ScalarHandler());
             return (int)count;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            
        }
        
        //查询目标页的数据返回
        @Override
        public List<Customer> getCurrectPageData(int startIndex, int numberPerPage) {
            
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
            
            String sql = "select * from customers limit ?,?";
            try {
                return runner.query(sql, new BeanListHandler<Customer>(Customer.class),startIndex,numberPerPage);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            
            
            
        }
    

    实现效果:


    4.png

    相关文章

      网友评论

          本文标题:2018-03-04 web用户项目(三):实现用户信息分页查询

          本文链接:https://www.haomeiwen.com/subject/depffftx.html