学习Thymeleaf简单分页

文章介绍了如何在项目中整合MybatisPlus并配置分页插件,通过编写MybatisPlusConfig类设置分页拦截器。接着展示了如何进行分页查询数据库中的User数据,包括获取当前页、总页数和总记录数,并在前端展示。最后,文章提到了数据统计信息的显示以及前后页和页码选择的实现,呈现了最终的分页效果。
摘要由CSDN通过智能技术生成

链接如下:

https://blog.csdn.net/weixin_56163435/article/details/122498169

整合MybatisPlus后才能使用。

1.首先要编写分页插件 MybatisPlusConfig。

@Configuration

public class MybatisPlusConfig {

@Bean

public MybatisPlusInterceptor mybatisPlusInterceptor() {

MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));

return interceptor;

}

}

2.分页查询,遍历数据库中的数据

@GetMapping("/dynamic_table")

public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Model model){

// List<User> list = userService.list();

// model.addAttribute("users",list);

Page<User> userPage = new Page<>(pn,2);

Page<User> page = userService.page(userPage,null);

long current = page.getCurrent();

long pages = page.getPages();

long total = page.getTotal();

List<User> records = page.getRecords();

model.addAttribute("page",page);

return "table/dynamic_table";

}

<tr class="gradeX" th:each="user,stats:${page.records}">

<td th:text="${stats.count}">Trident</td>

<td th:text="${user.id}">id</td>

<td class=" " th:text="${user.name}"></td>

<td class=" " th:text="${user.age}"></td>

<td class="center hidden-phone">[[${user.email}]]</td>

<td class="center hidden-phone sorting_1">X</td>

</tr>

3.数据统计

<div class="dataTables_info" id="editable-sample_info">

当前第 [[${page.current}]] 页

总计 [[${page.pages}]] 页

共 [[${page.total}]] 条记录

</div>

4.前一页,后一页,和页面选择

<ul>

<li th:if="${page.current>1}" class="prev"><a th:href="@{/dynamic_table(pn=${(page.current)-1})}">← 前一页</a></li>

<li th:if="${page.current==1}" class="prev disabled"><a>← 前一页</a></li>

<li th:class="${num == page.current?'active':''}" class="active" th:each="num:${#numbers.sequence(1,page.pages)}">

<a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>

</li>

<li th:if="${page.current<page.pages}" class="next"><a th:href="@{/dynamic_table(pn=${(page.current)+1})}">下一页 → </a></li>

<li th:if="${page.current==page.pages}" class="next disabled"><a>下一页 →</a></li>

</ul>

5.最终效果

————————————————

版权声明:本文为CSDN博主「小张的悠闲时光」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_56163435/article/details/122498169

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值