Java实现简单的分页效果

本文介绍了如何使用Java实现简单的分页功能。通过定义PageBean实体类,包含总记录数、总页数、每页记录数、当前页和查询结果列表等属性。在Servlet层获取当前页数,调用Service层,再由DAO层执行数据库操作,获取总记录数并进行分页查询。最后在前台页面展示分页结果。

实现分页功能,在查询的时候我们需要多少条数据就查询多少条数据。具体如下:
首先我们定义一个PageBean的实体类,类中有如下属性

  • totalCount ------- 总共有多少条数据
  • totalPage--------- 共有多少页
  • rows--------------- 每页显示多少条数据
  • currentPage----- 当前页
  • list------------------ 查询到的内容
    totalCount 、list可以从数据库中获取得到,currentPage根据前台获取,rows自定义(根据自己的需要),totalPage可以根据总的条数除每页显示条数得到,由于不够一页的也会单独显示一页,所以我们向上取整。
    具体代码:
    PageBean
public class PageBean {


    private int totalCount;//总条数
    private int totalPage;//总页数
    private int rows;//每页显示行数
    private int currentPage;//当前页码
    private List<Student> list; // 查询得到的内容

    public PageBean(int totalCount, int rows, int currentPage, List<Student> list) {
        this.totalCount = totalCount;
        this.rows = rows;
        this.currentPage = currentPage;
        this.list = list;
    }

    public PageBean() {
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return (int)Math.ceil(totalCount/(rows*1.0));
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

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

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

servlet层:获取当前页,调用service.getPageBean()传入每页条数和当前页面

		StudentService service =  new StudentServiceImpl();
        response.setContentType("text/html;charset=utf-8");
        String currentPage = request.getParameter("currentPage");
        if(currentPage == null){
            currentPage = "1";
        }
        PageBean pageBean = service.getPageBean(3, Integer.parseInt(currentPage));

        request.getSession().setAttribute("pb", pageBean);

        response.sendRedirect("/index.jsp");

service层

public PageBean getPageBean(int rows, int currentPage) {
		StudentDao dao = new StudentDaoImpl();
        List<Student> list = dao.getStudentsByPage(rows, currentPage);
        int count = dao.getTotalCount();
        PageBean pageBean = new PageBean(count, rows, currentPage, list);
        return pageBean;
    }

dao层

1、获取总条数

public int getTotalCount() {
        int row = 0;
        //1、获取连接
        Connection connection = null;
        try {
            connection = JdbcUtils.getConnection();
            //2、定义sql语句
            String sql = "select * from student";
            //3、获取预编译执行中
            PreparedStatement ps = connection.prepareStatement(sql);
            //4、执行sql语句
            ResultSet resultSet = ps.executeQuery();
            resultSet.last();
            row = resultSet.getRow();
            JdbcUtils.close(ps, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

2、分页

public List<Student> getStudentsByPage(int rows, int currentPage) {

        ArrayList<Student> list = new ArrayList<>();
        //1、获取连接
        Connection connection = null;
        try {
            connection = JdbcUtils.getConnection();
            //2、定义sql语句
            String sql = "select * from student limit ?,?";
            //3、获取预编译执行中
            PreparedStatement ps = connection.prepareStatement(sql);
            //3.1 设置参数
            ps.setInt(1, (currentPage-1)*rows);
            ps.setInt(2, rows);
            //4、执行sql语句
            ResultSet resultSet = ps.executeQuery();
            while(resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                int age = resultSet.getInt(3);
                String gender = resultSet.getString(4);

                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            JdbcUtils.close(ps, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

前台页面展示

<div class="container">
  <h3 style="text-align: center">用户信息列表</h3>
  <table border="1" class="table table-bordered table-hover">

    <tr class="success">
    <th>编号</th>
    <th>姓名</th>
    <th>性别</th>
    <th>年龄</th>
    <th>操作</th>
  </tr>
    <c:forEach items="${pb.list}" var="stu">
      <tr>
        <td>${stu.id}</td>
        <td>${stu.name}</td>
        <td>${stu.gender}</td>
        <td>${stu.age}</td>
      </tr>
    </c:forEach>
  </table>
  <div>
    <nav aria-label="Page navigation">
      <ul class="pagination">
        <c:if test="${pb.currentPage <= 1}">
          <li class="disabled">
            <a href="#" aria-label="Previous">
              <span aria-hidden="true">&laquo;</span>
            </a>
          </li>
        </c:if>
        <c:if test="${pb.currentPage > 1}">
          <li>
          <a href="/studentsServlet?currentPage=${pb.currentPage - 1}" aria-label="Previous">
            <span aria-hidden="true">&laquo;</span>
          </a>
          </li>
        </c:if>
        <c:forEach begin="1" end="${pb.totalPage}" var="i" step="1">
          <li><a href="/studentsServlet?currentPage=${i}">${i}</a></li>
        </c:forEach>
        <c:if test="${pb.currentPage < pb.totalPage}">
          <li>
          <a href="/studentsServlet?currentPage=${pb.currentPage + 1}" aria-label="Next">
            <span aria-hidden="true">&raquo;</span>
          </a>
          </li>
        </c:if>
        <c:if test="${pb.currentPage >= pb.totalPage}">
          <li class="disabled">
            <a href="#" aria-label="Next">
              <span aria-hidden="true">&raquo;</span>
            </a>
          </li>
        </c:if>

        <span style="font-size: 25px;margin-left: 5px;">
                    共${pb.totalCount}条记录,共${pb.totalPage}页
                </span>

      </ul>
    </nav>
  </div>

</div>

这样一个简单的分页效果就实现了。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值