Springboot之分页插件的使用(基于MybatiPlus)

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值