MybatisPlus的分页插件
1、准备工作
1.导入分页插件依赖和MybatisPlus依赖以及thymeleaf
(这里使用thymeleaf进行前后端交互)
<!-- thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 分页查询 -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
<!-- mybatis-plus环境 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2.配置分页拦截器
@Configuration
public class MyBatisConfig {
//分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(100L);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
2、前端控制器
@RequestMapping("/findAllUser")
public String findAllUser(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Model model) {
// List<User> list = userService.list();
// model.addAttribute("userList", list);
Page<User> page = new Page<User>(pn,2);
//分页查询的结果
Page<User> page2 = userService.page(page);
model.addAttribute("page", page2);
return "/table/userTable";
}
(pn为传进来的当前页面,2为一页显示记录数)
3、显示页面
<div class="dynamic_data">
<table>
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>姓名</th>
<th>地址</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr th:each="user,stat:${page.records}">
<td th:text="${stat.count}"></td>
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.address}"></td>
<td th:text="${user.age}"></td>
</tr>
</tbody>
</table>
<!-- 分页条 -->
<div>
<ul>
<li><a href="#">←</a></li>
<li th:class="${num == page.current?'active':''}" th:each="num:${#numbers.sequence(1,page.pages)}">
<!-- 动态链接加参数 -->
<a th:href="@{/user/findAllUser(pn=${num})}">[[${num}]]</a>
</li>
<li><a href="#">→</a></li>
</ul>
<br>
当前 第[[${page.current}]]/ [[${page.pages}]]页 共有[[${page.total}]]条记录
</div>
</div>