逻辑流程
首先要定义一个Bean来装分页的东西,里面具体数据:
- 每页展示的数据 ----- 数据库查询的数据
- 每页的数据条数 ----- 自定义或前端传入
- 总页数 ----- 总条数/每页显示的条数(结果向上取整)
- 当前页码 ----- 前端传入
- 总条数 ----- 数据库查询
核心是数据库语句:limit
limit 值1,值2 :表示从值1这个下标开始查,查值2这么多条数据
值1:(当前页码-1)* 每页的数据条数
值2:每页显示的数据条数
代码实现
注意:代码采用的MVC模式,前端运用了BootStrap和jstl标签库和EL表达式。封装了jdbc,用的连接池,可以看上篇博客
JDBC之数据库连接池(Druid): https://blog.youkuaiyun.com/m0_38048304/article/details/87629448
定义Bean:
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() {}
//由于这个是当前运算出来的,所以只需要get方法就行了
public int getTotalPage() {
return (int)Math.ceil(totalCount/(rows*1.0));
}
//get,set方法省略
}
对数据库操作:
获取显示的内容:
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 newstudents 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()){
String id = resultSet.getString(1);
String name = resultSet.getString(2);
String age = resultSet.getString(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;
}
获取总条数:
public int getTotalCount() {
int row = 0;
//1、获取连接
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
//2、定义sql语句
String sql = "select * from newstudents";
//3、获取预编译执行中
PreparedStatement ps = connection.prepareStatement(sql);
//4、执行sql语句
ResultSet resultSet = ps.executeQuery();
resultSet.last();
row = resultSet.getRow();
System.out.println(row);
JdbcUtils.close(ps, connection);
} catch (SQLException e) {
e.printStackTrace();
}
return row;
}
获取Bean:
public PageBean getPageBean(int rows, int currentPage) {
List<Student> list = dao.getStudentsByPage(rows, currentPage);
int count = dao.getTotalCount();
PageBean pageBean = new PageBean(count, rows, currentPage, list);
return pageBean;
}
servlet传数据到前端:
response.setContentType("text/html;charset=utf-8");
String currentPage = request.getParameter("currentPage");
if(currentPage == null){
currentPage = "1";
}
PageBean pageBean = service.getPageBean(5, Integer.parseInt(currentPage));
request.getSession().setAttribute("pb", pageBean);
response.sendRedirect("/index.jsp");
前端接收,显示:
<!-- 表格显示的数据
由于这里需要的数据是Bean里的list,所以这里的items就是pb.list
-->
<c:forEach items="${pb.list}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.gender}</td>
<td>${stu.age}</td>
<td><a class="btn btn-default btn-sm" href="updateServlet?id=${stu.id}">修改</a>
<a class="btn btn-default btn-sm" href="javascript:void(0)" onclick="del(${stu.id})">删除</a></td>
</tr>
</c:forEach>
<!-- 最下面的页码、<< 、>> 按钮的实现 -->
<ul class="pagination">
<li>
<!-- 用 C:if 来控制第一页和最后一页按钮功能的失效 -->
<c:if test="${pb.currentPage-1 == 0}">
<a href="#" aria-label="Previous" class="btn disabled">
<span aria-hidden="true">«</span>
</a>
</c:if>
<c:if test="${pb.currentPage-1 != 0}">
<a href="/studentsServlet1?currentPage=${pb.currentPage-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</c:if>
</li>
<c:forEach begin="1" end="${pb.totalPage}" var="i" step="1">
<li><a href="/studentsServlet1?currentPage=${i}">${i}</a></li>
</c:forEach>
<li>
<c:if test="${pb.currentPage == pb.totalPage}">
<a href="#" aria-label="Next" class="btn disabled">
<span aria-hidden="true">»</span>
</a>
</c:if>
<c:if test="${pb.currentPage < pb.totalPage}">
<a href="/studentsServlet1?currentPage=${pb.currentPage+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:if>
</li>
<span style="font-size: 25px;margin-left: 5px;">
共${pb.totalCount}条记录,共${pb.totalPage}页
</span>
</ul>
效果: