使用servlet+jsp+mybatis使用分页插件写出首页上一页下一页尾页
首先,需要在pom.xml文件中添加MyBatis分页插件的依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
接下来,创建一个类来处理分页的逻辑:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
public class PaginationHelper<T> {
private int currentPage;
private int pageSize;
private int totalCount;
private List<T> data;
public PaginationHelper(int currentPage, int pageSize, int totalCount, List<T> data) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.data = data;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public int getTotalCount() {
return totalCount;
}
public int getTotalPages() {
return (int) Math.ceil((double) totalCount / pageSize);
}
public boolean hasPreviousPage() {
return currentPage > 1;
}
public boolean hasNextPage() {
return currentPage < getTotalPages();
}
public List<T> getData() {
return data;
}
}
然后,在Servlet中调用MyBatis分页插件查询数据:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
// 使用PageHelper进行分页设置
PageHelper.startPage(currentPage, pageSize);
// 调用MyBatis查询数据
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
List<Data> dataList = sqlSession.selectList("com.example.mapper.DataMapper.selectData");
// 使用PageInfo获取分页信息
PageInfo<Data> pageInfo = new PageInfo<>(dataList);
int totalCount = (int) pageInfo.getTotal();
// 封装分页数据
PaginationHelper<Data> paginationHelper = new PaginationHelper<>(currentPage, pageSize, totalCount, dataList);
// 将分页数据存入request中
request.setAttribute("paginationHelper", paginationHelper);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
最后,在index.jsp中显示分页数据:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>分页示例</title>
</head>
<body>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<c:forEach items="${paginationHelper.data}" var="data">
<tr>
<td>${data.id}</td>
<td>${data.name}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<a href="${request.contextPath}/?currentPage=1&pageSize=${paginationHelper.pageSize}">首页</a>
<c:if test="${paginationHelper.hasPreviousPage}">
<a href="${request.contextPath}/?currentPage=${paginationHelper.currentPage - 1}&pageSize=${paginationHelper.pageSize}">上一页</a>
</c:if>
<c:if test="${paginationHelper.hasNextPage}">
<a href="${request.contextPath}/?currentPage=${paginationHelper.currentPage + 1}&pageSize=${paginationHelper.pageSize}">下一页</a>
</c:if>
<a href="${request.contextPath}/?currentPage=${paginationHelper.totalPages}&pageSize=${paginationHelper.pageSize}">尾页</a>
</div>
</body>
</html>
以上代码示例实现了使用Servlet+JSP+MyBatis分页插件来实现首页、上一页、下一页和尾页的功能。在Servlet中调用MyBatis分页插件进行分页查询,然后将分页数据封装到PaginationHelper中,最后在index.jsp中显示分页数据和分页链接
原文地址: https://www.cveoy.top/t/topic/ioO1 著作权归作者所有。请勿转载和采集!