以下是一个使用 servlet+jsp+mybatis 分页插件实现首页、下一页、上一页和尾页效果的示例代码:

  1. 创建一个名为'PageHelper'的分页插件,实现 PaginationInterceptor 接口,并在实现类中重写 intercept() 方法来进行分页处理。
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import java.util.Properties;

@Intercepts(@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}))
public class PageHelper implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());

        BoundSql boundSql = statementHandler.getBoundSql();
        String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");

        // 判断是否需要分页
        if (originalSql != null && originalSql.toLowerCase().contains("select")) {
            // 获取分页参数
            Object parameterObject = boundSql.getParameterObject();
            if (parameterObject != null) {
                Page page = (Page) metaObject.getValue("delegate.boundSql.parameterObject.page");
                if (page != null) {
                    PageHelper.startPage(page.getPageNum(), page.getPageSize());
                }
            }
        }

        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // 读取配置信息,如果有需要可以自定义一些参数
    }
}
  1. 在 mybatis-config.xml 中配置分页插件。
<plugins>
    <plugin interceptor="com.example.PageHelper">
        <!-- 自定义配置参数 -->
    </plugin>
</plugins>
  1. 创建一个名为'Page'的分页实体类,包含当前页码和每页显示数量等属性。
public class Page {
    private int pageNum;
    private int pageSize;
    
    // getter and setter methods
}
  1. 在 servlet 中处理分页请求,将分页实体类设置到 request 中,然后转发到首页 jsp 页面。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    int pageNum = Integer.parseInt(request.getParameter("pageNum"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    
    Page page = new Page();
    page.setPageNum(pageNum);
    page.setPageSize(pageSize);
    
    request.setAttribute("page", page);
    request.getRequestDispatcher("index.jsp").forward(request, response);
}
  1. 在 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"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>分页示例</title>
</head>
<body>
    <h1>分页示例</h1>
    
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>名称</th>
                <!-- 其他字段 -->
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${page.resultList}" var="item">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <!-- 其他字段 -->
                </tr>
            </c:forEach>
        </tbody>
    </table>
    
    <div>
        <%-- 首页 --%>
        <c:if test="${page.pageNum > 1}">
            <a href="${pageContext.request.contextPath}/page?pageNum=1&pageSize=${page.pageSize}">首页</a>
        </c:if>
        
        <%-- 上一页 --%>
        <c:if test="${page.pageNum > 1}">
            <a href="${pageContext.request.contextPath}/page?pageNum=${page.pageNum - 1}&pageSize=${page.pageSize}">上一页</a>
        </c:if>
        
        <%-- 下一页 --%>
        <c:if test="${page.pageNum < page.totalPage}">
            <a href="${pageContext.request.contextPath}/page?pageNum=${page.pageNum + 1}&pageSize=${page.pageSize}">下一页</a>
        </c:if>
        
        <%-- 尾页 --%>
        <c:if test="${page.pageNum < page.totalPage}">
            <a href="${pageContext.request.contextPath}/page?pageNum=${page.totalPage}&pageSize=${page.pageSize}">尾页</a>
        </c:if>
    </div>
</body>
</html>

以上代码中的分页插件使用了 PageHelper,你可以根据自己的需求选择其他的分页插件。这个示例中的分页插件是基于 mybatis 的,如果你使用其他的持久层框架,可能需要做一些调整。

Servlet+JSP+Mybatis 分页插件实现首页、下一页、上一页、尾页效果

原文地址: https://www.cveoy.top/t/topic/p6Km 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录