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>