day_1_15 JavaWeb系列学习总结之JavaWeb实现分页查询

首先先看效果图:
分页查询.png

然后提供了一个Java Bean实体类对象
(PageBean.java)

import java.util.List;

/**
 * 封装了 分页查询的所有参数
 *
 */
public class PageBean<T> {
    private Integer cp; // 当前页
    private Integer tp; // 总页数
    private Integer tr; // 总记录数
    private Integer pr; // 每页记录数为5
    private List<T> list; // 每页数据

    public Integer getCp() {
        return cp;
    }

    public void setCp(Integer cp) {
        this.cp = cp;
    }

    // 得到总页数
    public Integer getTp() {
        int count = tr / pr;
        return (tr % pr == 0) ? count : count + 1;
    }

    public Integer getTr() {
        return tr;
    }

    public void setTr(Integer tr) {
        this.tr = tr;
    }

    public Integer getPr() {
        return pr;
    }

    public void setPr(Integer pr) {
        this.pr = pr;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}

在dao层提供对数据库的操作:()

     /**
     * 查询所有用户
     *
     * @return 返回封装了所有客户的list集合
     * @throws Exception
     */
    public PageBean<Customer> findAllCustomer(PageBean pageBean) throws Exception {
        String sql = "select * from customer limit ?, ?";
        Object[] objs = {(pageBean.getCp() - 1) * pageBean.getPr(),
                pageBean.getPr()}; // 第一个参数为 5 * (当前页数 - 1); 后一个参数为5(每页的记录数)
        List<Customer> list = qr.query(sql, new BeanListHandler<Customer>(Customer.class), objs);
        pageBean.setList(list);

        // 查询总记录数
        sql = "select count(*) from customer";
        Number n = (Number) qr.query(sql, new ScalarHandler());
        Integer tr = n.intValue();
        pageBean.setTr(tr);

        return pageBean;
    }

然后是页面的展示:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>客户列表</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

</head>

<body>
<h3 align="center">客户列表</h3>

<table border="1" width="70%" align="center">
    <tr>
        <th>客户姓名</th>
        <th>性别</th>
        <th>生日</th>
        <th>手机</th>
        <th>邮箱</th>
        <th>描述</th>
        <th>操作</th>
    </tr>

    <c:forEach var="customer" items="${requestScope.pageBean.list }">
        <input type="hidden" value="${customer.cid }" id="cid"/>
        <input type="hidden" value="${customer.cname }" id="cname">

        <tr>
            <td>${customer.cname }</td>
            <td>${customer.gender }</td>
            <td>${customer.birthday }</td>
            <td>${customer.cellPhone }</td>
            <td>${customer.email }</td>
            <td>${customer.description }</td>
            <td>
                <a href="${pageContext.request.contextPath }/CustomerServlet?method=findCustomerById&cid=${customer.cid}">编辑</a>
                <a href="javascript:_delete('${customer.cid }')">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>

<center style="font-size:2em;">
    第${pageBean.cp }页/共${pageBean.tp }页

    <a href="${pageContext.request.contextPath }/CustomerServlet?method=findAllCustomer&cp=1"/>首页</a>

    <%--如果当前页为1  那么 不能够再上一页 --%>
    <c:choose>
        <c:when test="${pageBean.cp>1 }">
            <a href="${pageContext.request.contextPath }/CustomerServlet?method=findAllCustomer&cp=${pageBean.cp-1}"/>上一页</a>
        </c:when>
        <c:otherwise>
            上一页
        </c:otherwise>
    </c:choose>

    <%-- 如果总页数小于10 那么第一个数字为1, 最后一个数字为总页数 --%>
    <c:if test="${pageBean.tp<=10 }">
        <c:set var="begin" value="1"/>
        <c:set var="end" value="${pageBean.tp }"/>
    </c:if>

    <%--
        如果总页数大于10, 那么第一个数字为cp-5, 最后一个数字为cp+4
     --%>
    <c:if test="${pageBean.tp>10 }">
        <c:set var="begin" value="${pageBean.cp-5 }"/>
        <c:set var="end" value="${pageBean.cp+4 }"/>

        <c:if test="${begin<1 }">
            <c:set var="begin" value="1"/>
            <c:set var="end" value="10"/>
        </c:if>
        <c:if test="${end>pageBean.tp }">
            <c:set var="begin" value="${pageBean.tp-9}"/>
            <c:set var="end" value="${pageBean.tp }"/>
        </c:if>
    </c:if>

    <c:forEach begin="${begin }" end="${end }" var="i">
        <c:choose>
            <c:when test="${pageBean.cp eq i }">
                ${i}
            </c:when>
            <c:otherwise>
                <a href="${pageContext.request.contextPath }/CustomerServlet?method=findAllCustomer&cp=${i}">${i}</a>
            </c:otherwise>
        </c:choose>
    </c:forEach>

    <%-- 如果当前页等于总页数, 那么不能再下一页 --%>
    <c:choose>
        <c:when test="${pageBean.cp < pageBean.tp }">
            <a href="${pageContext.request.contextPath }/CustomerServlet?method=findAllCustomer&cp=${pageBean.cp+1}"/>下一页</a>
        </c:when>
        <c:otherwise>
            下一页
        </c:otherwise>
    </c:choose>

    <a href="${pageContext.request.contextPath }/CustomerServlet?method=findAllCustomer&cp=${pageBean.tp}"/>尾页</a>
</center>

<script type="text/javascript">
    function _delete(cid) {
        var flag = window.confirm("确认要删除吗 !!!");
        if (flag == true) {
            location.href = "${pageContext.request.contextPath}/CustomerServlet?method=deleteCustomerById&cid=" + cid;
        }
    }
</script>

</body>
</html>

结果演示:
演示.gif

完整代码地址(含SQL脚本)

https://github.com/menglanyingfei/Java/tree/master/JavaWebTrain/day_1_15/customer

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值