SpringBoot + thymeleaf + pagehelper分页插件实现分页

本文介绍如何使用MyBatis分页插件PageHelper实现Spring Boot应用中的分页功能,包括pom.xml配置依赖、Controller层分页逻辑及Thymeleaf模板展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom.xml文件

<!-- Mybatis 分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.13</version>
</dependency>
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>3.1</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatisplus-spring-boot-starter</artifactId>
    <version>1.0.5</version>
</dependency>

Controller类

@GetMapping("/lists")
public String index(Model model, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    //查询所有数据方法
    List<SysLogEntity> list = logService.getUserList();  
    PageInfo pageInfo = new PageInfo(list);
    model.addAttribute("pageInfo", pageInfo);
     //获得当前页
    model.addAttribute("pageNum", pageInfo.getPageNum());
    //获得一页显示的条数
    model.addAttribute("pageSize", pageInfo.getPageSize());
    //是否是第一页
    model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
    //获得总页数
    model.addAttribute("totalPages", pageInfo.getPages());
    //是否是最后一页
    model.addAttribute("isLastPage", pageInfo.isIsLastPage());

    model.addAttribute("active", "operationLog");
    model.addAttribute("title", "日志管理");
    return "index";
}

 

html页面

   红色字体代表的是后端访问方法的地址

<!DOCTYPE html>
<!-- saved from url=(0044)http://192.168.1.1/firewall/port_forward.asp -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!--    <meta name="viewport" content="width=device-width, initial-scale=1.0">-->
    <title>日志管理</title>
</head>
<body>
<div class="container">
    <table class="table table-striped">
        <tr>
            <!--            <th>id</th>-->
            <th>用户名</th>
            <th>IP地址</th>
            <th>日志类型</th>
            <th>方法</th>
            <th>创建时间</th>
            <th>详情</th>
        </tr>
        <tr th:each="SysLogEntity : ${pageInfo.list}">
            <!--            <td th:text="${SysLogEntity.id}"></td>-->
            <td th:text="${SysLogEntity.username}"></td>
            <td th:text="${SysLogEntity.requestIp}"></td>
            <td th:text="${SysLogEntity.logType}"></td>
            <td th:text="${SysLogEntity.method}"></td>
            <td th:text="${SysLogEntity.createTime}"></td>
            <td th:text="${SysLogEntity.description}"></td>
        </tr>
    </table>
    <nav>
        <ul class="pagination">
            <li>
                <a th:if="${not pageInfo.isFirstPage}" th:href="@{${'lists'}(pageNum=${pageNum-1},pageSize=${pageSize})}"
                   aria-label="Previous">
                    <span aria-hidden="true">首页</span>
                </a>
                <a th:if="${isFirstPage}" href="javascript:void(0);" aria-label="Previous">
                    <span aria-hidden="true">首页</span>
                </a>
            </li>

            <li th:each="pageNo : ${#numbers.sequence(1, totalPages)}">
                <a th:if="${pageNum eq pageNo}" href="javascript:void(0);">
                    <span th:text="${pageNo}"></span>
                </a>

                <a th:if="${not (pageNum eq pageNo)}" th:href="@{${'lists'}(pageNum=${pageNo},pageSize=${pageSize})}">
                    <span th:text="${pageNo}"></span>
                </a>
            </li>

            <li>
                <a th:if="${not isLastPage}" th:href="@{${'lists'}(pageNum=${pageNum+1},pageSize=${pageSize})}"
                   aria-label="Next">
                    <span aria-hidden="true">尾页</span>
                </a>
                <a th:if="${isLastPage}" href="javascript:void(0);" aria-label="Next">
                    <span aria-hidden="true">尾页</span>
                </a>
            </li>
        </ul>
    </nav>
</div>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值