美文网首页学习资源JavaEE 学习专题程序员
分页实现(接上篇客户关系管理案例)

分页实现(接上篇客户关系管理案例)

作者: 小小蒜头 | 来源:发表于2017-08-02 19:20 被阅读43次
    分页结构.jpg

    1. 首先来建立一个QueryInfo(封装查询信息)的实体

    package cn.itcast.domain;
    
    public class QueryInfo {
        private int currentPage = 1;//用户当前页
        private int pageSize = 5;//默认一页5条数据
        private int startIndex;//记住用户当前看的页的数据在数据库的起始位置
    
        public int getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }
    
        public int getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
    
        //由于startIndex是由currentPage与pageSize计算而得所以没必要有set方法
        public int getStartIndex() {
            this.startIndex = (this.getCurrentPage() - 1) * this.getPageSize();
            return startIndex;
        }
    
    }
    

    2. 封装查询结果的QueryResult的实体(用户访问数据库取出该页数据)

    package cn.itcast.domain;
    
    import java.util.List;
    
    public class QueryResult {
        private List list;//记住用户看的页的数据
        private int totalRecord;//记住总记录数
    
        public List getList() {
            return list;
        }
    
        public void setList(List list) {
            this.list = list;
        }
    
        public int getTotalRecord() {
            return totalRecord;
        }
    
        public void setTotalRecord(int totalRecord) {
            this.totalRecord = totalRecord;
        }
    }
    

    3. 封装显示页面数据PageBean

    package cn.itcast.domain;
    
    import java.util.List;
    
    public class PageBean {
        private List list;
        private int totalRecord;
        private int pageSize;
        private int totalPage;//这个由内部的totalRecord和pageSize所得,所以没必要有set方法
        private int currentPage;
        private int previousPage;//上一页(同totalPage)
        private int nextPage;//下一页(同totalPage)
        private int[] pageBar;//页码条,它根据总页数来生成页码条
    
        public List getList() {
            return list;
        }
    
        public void setList(List list) {
            this.list = list;
        }
    
        public int getTotalRecord() {
            return totalRecord;
        }
    
        public void setTotalRecord(int totalRecord) {
            this.totalRecord = totalRecord;
        }
    
        public int getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
    
        public int getTotalPage() {
            //100 5 20
            //101 5 21
            if (this.totalRecord% this.pageSize == 0) {
                this.totalPage = this.totalRecord / this.pageSize;
            } else {
                this.totalPage = this.totalRecord / this.pageSize + 1;
            }
            return totalPage;
        }
    
        public int getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }
    
        public int getPreviousPage() {
            if (this.currentPage - 1 < 1) {
                this.previousPage = 1;
            } else {
                this.previousPage = this.currentPage - 1;
            }
            return previousPage;
        }
    
        public int getNextPage() {
            if (this.currentPage + 1 >= this.totalPage) {
                this.nextPage = this.totalPage;
            } else {
                this.nextPage = this.currentPage + 1;
            }
            return nextPage;
        }
    
        public int[] getPageBar() {
            int pageBar[] = new int[this.totalPage];
            for (int i = 0; i < this.totalPage; i++) {
                pageBar[i] = i;
            }
            this.pageBar = pageBar;//一调用页面,就得到需要显示的页码条
            return pageBar;
        }
    }
    

    4. 改造Dao层CustomerDaoImpl,在里面添加pageQuery方法,最后将这个方法抽取到接口CustomerDao里去

    //获取到页面数据和大小
        public QueryResult pageQuery(int startIndex, int pageSize) {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
    
            QueryResult qr = new QueryResult();
            try {
                conn = JdbcUtils.getConnection();
                //取出某一页的数据
                String sql = "select * from customer limit ?,?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1, startIndex);
                ps.setInt(2, pageSize);
                rs = ps.executeQuery();
                List list = new ArrayList();
                while (rs.next()) {
                    Customer customer = new Customer();
                    customer.setBirthday(rs.getDate("birthday"));
                    customer.setCellphone(rs.getString("cellphone"));
                    customer.setDescription(rs.getString("description"));
                    customer.setEmail(rs.getString("email"));
                    customer.setGender(rs.getString("gender"));
                    customer.setId(rs.getString("id"));
                    customer.setName(rs.getString("name"));
                    customer.setPreference(rs.getString("preference"));
                    customer.setType(rs.getString("type"));
                    list.add(customer);
                }
                qr.setList(list);//把页面数据给QueryResult
                sql = "select count(*) from customer";//返回表的总记录数
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery();
                //总记录数就只有一条记录,可以用命令语句查看
                if (rs.next()) {
                    qr.setTotalRecord(rs.getInt(1));//一行一列取第一列
                }
                return qr;
            } catch (Exception e) {
                throw new DaoException(e);
            } finally {
                JdbcUtils.release(conn, ps, rs);
            }
        }
    

    5. 改造Service层BusinessServiceImpl,在里面添加pageQuery方法,最后将这个方法抽取到接口BusinessService里去

    @Override
        public PageBean pageQuery(QueryInfo queryInfo) {
            //1.调dao获取到页面数据
            QueryResult qr = dao.pageQuery(queryInfo.getStartIndex(), queryInfo.getPageSize());
    
            //2.根据dao查询结果,生成页面显示需要PageBean
            PageBean bean = new PageBean();
            bean.setCurrentPage(queryInfo.getCurrentPage());
            bean.setList(qr.getList());
            bean.setPageSize(queryInfo.getPageSize());
            bean.setTotalRecord(qr.getTotalRecord());
            return bean;
        }
    

    6. 改造Servlet,ListCustomerServlet

    package cn.itcast.web.controller;
    
    import cn.itcast.domain.PageBean;
    import cn.itcast.domain.QueryInfo;
    import cn.itcast.service.BusinessService;
    import cn.itcast.service.impl.BusinessServiceImpl;
    import cn.itcast.utils.WebUtils;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.List;
    
    //处理用户分页请求
    @WebServlet(name = "ListCustomerServlet", urlPatterns = "/servlet/ListCustomerServlet")
    public class ListCustomerServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                QueryInfo info = WebUtils.request2Bean(request, QueryInfo.class);
    
                BusinessService service = new BusinessServiceImpl();
                PageBean pageBean = service.pageQuery(info);
    
                request.setAttribute("pageBean", pageBean);
                request.getRequestDispatcher("/WEB-INF/jsp/listcustomer.jsp").forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
                request.setAttribute("message", "查看客户失败");
                request.getRequestDispatcher("/message.jsp").forward(request, response);
            }
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    }
    

    7.同时, listcustomer.jsp也要做出相应的更改

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>列出所有客户</title>
    </head>
    <body style="text-align: center">
    <table frame="border" width="85%" align="center">
        <tr>
            <td>客户姓名</td>
            <td>性别</td>
            <td>生日</td>
            <td>手机</td>
            <td>邮箱</td>
            <td>爱好</td>
            <td>类型</td>
            <td>备注</td>
            <td>操作</td>
        <tr>
    
            <c:forEach var="c" items="${requestScope.pageBean.list}">
        <tr>
            <td>${c.name }</td>
            <td>${c.gender }</td>
            <td>${c.birthday }</td>
            <td>${c.cellphone }</td>
            <td>${c.email }</td>
            <td>${c.preference }</td>
            <td>${c.type }</td>
            <td>${c.description }</td>
            <td>
                <a href="#">修改</a>
                <a href="#">删除</a>
            </td>
        <tr>
            </c:forEach>
    </table>
    <br>
    <script type="text/javascript">
    
        function gotoPage(currentPage) {
            window.location.href = '${pageContext.request.contextPath}/servlet/ListCustomerServlet?currentPage=' + currentPage;
        }
    
    </script>
    
    共[${pageBean.totalRecord}]条记录,每页[${pageBean.pageSize}]条,共[${pageBean.totalPage}]页,
    当前第[${pageBean.currentPage}]页,  
    <a href="javascript:;" onclick="gotoPage(${pageBean.previousPage})">上一页</a>
    <c:forEach var="pageNum" items="${pageBean.pageBar}">
        <a href="javascript:;" onclick="gotoPage(${pageNum})">${pageNum}</a>
    </c:forEach>
    <a href="javascript:;" onclick="gotoPage(${pageBean.nextPage})">下一页</a>
    </body>
    </html>
    

    关于分页实现还有很多小细节需要处理,后面会接着更新。。。

    源代码:https://github.com/yvettee36/Customer1.git

    接上篇:客户关系管理案例
    连下篇:完成客户关系管理案例

    相关文章

      网友评论

        本文标题:分页实现(接上篇客户关系管理案例)

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