springboot+thymeleaf简单的分页查询

一.thymeleaf介绍及常用标签

Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。它与 JSP,Velocity,FreeMaker 等模板引擎类似,也可以轻易地与 Spring MVC 等 Web 框架集成。与其它模板引擎相比,Thymeleaf 最大的特点是,即使不启动 Web 应用,也可以直接在浏览器中打开并正确显示模板页面 。

Thymeleaf 简介

Thymeleaf 是新一代 Java 模板引擎,支持 HTML 原型,以直接被浏览器打开,此时浏览器会忽略未定义的 Thymeleaf 标签属性,展示 thymeleaf 模板的静态页面效果。当在应用程序中会动态地替换掉页面设置的标签属性。

Thymeleaf 的特点

  • 动静结合:Thymeleaf 既可以直接使用浏览器打开,查看页面的静态效果,也可以通过 Web 应用程序进行访问,查看动态页面效果。

  • 开箱即用:Thymeleaf 提供了 Spring 标准方言以及一个与 SpringMVC 完美集成的可选模块,可以快速地实现表单绑定、属性编辑器、国际化等功能。

  • 多方言支持:它提供了 Thymeleaf 标准和 Spring 标准两种方言,可以直接套用模板实现 JSTL、 OGNL 表达式;必要时,开发人员也可以扩展和创建自定义的方言。

  • 与 SpringBoot 完美整合:SpringBoot 为 Thymeleaf 提供了的默认配置,并且还为 Thymeleaf 设置了视图解析器,因此 Thymeleaf 可以与 Spring Boot 完美整合。

Thymeleaf 通过在 html 标签中,增加额外属性来达到“模板+数据”的展示方式

常用标签介绍:

(11条消息) Thymeleaf常用th标签_我爱圆溜溜的博客-CSDN博客_thymeleaf th

二.代码实现 

注:此处使用的分页工具类只需提供页码及每页条数 

1.导入两个jar包

2.配置文件添加如下代码

3.service实现类

 1)无条件分页

2) 根据名称查询

4.Controller层

1)无条件分页

2) 根据名称查询

5.前端展示

 

   

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在pom.xml中引入相应的依赖,这里以MySQL数据库为例: ```xml <!-- SpringBoot Web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot Mybatis模块 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 其次,需要配置Mybatis和数据库连接,可以在application.yml中配置: ```yml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useSSL=false&characterEncoding=utf8 username: root password: root mybatis: config-location: classpath:mybatis-config.xml mapper-locations: classpath:mapper/*.xml ``` 其中,`datasource`为数据源的基本信息,`mybatis`则是Mybatis的配置信息,包括配置文件的位置和Mapper文件的位置。 接下来,需要编写Mapper接口和对应的Mapper XML文件。以User表为例: ```java public interface UserMapper { List<User> findUserByPage(@Param("start") Integer start, @Param("pageSize") Integer pageSize); } ``` ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="findUserByPage" resultType="com.example.demo.entity.User"> select * from user limit #{start},#{pageSize} </select> </mapper> ``` 其中,`findUserByPage`方法为分页查询方法,`start`为起始位置,`pageSize`为每页数量。 最后,编写Controller层和前端页面。以UserController为例: ```java @Controller public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/user") public String findUserByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, Model model) { Integer start = (pageNum - 1) * pageSize; List<User> userList = userMapper.findUserByPage(start, pageSize); PageInfo pageInfo = new PageInfo(userList); model.addAttribute("pageInfo", pageInfo); return "user"; } } ``` 其中,`findUserByPage`方法接收两个参数:`pageNum`和`pageSize`,表示当前页和每页数量。通过计算获得起始位置,调用Mapper接口进行分页查询,并通过`PageInfo`类将查询结果传递给前端页面。 在前端页面中通过`th:each`循环遍历查询结果,并通过`th:href`生成分页链接。以user.html为例: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>User List</title> </head> <body> <table border="1"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> </thead> <tbody> <tr th:each="user : ${pageInfo.list}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td th:text="${user.gender}"></td> </tr> </tbody> </table> <div> <a th:href="@{/user?pageNum=1}">首页</a> <a th:href="@{/user?pageNum=${pageInfo.prePage}}">上一页</a> <a th:href="@{/user?pageNum=${pageInfo.nextPage}}">下一页</a> <a th:href="@{/user?pageNum=${pageInfo.pages}}">尾页</a> </div> </body> </html> ``` 其中,`pageInfo.list`为查询结果列表,通过`th:each`循环遍历生成表格数据。底部的分页链接则通过`th:href`生成相应的链接。 到这里,一个简单分页查询就完成了。需要注意的是,以上代码仅为示例,具体实现方式可能会有所不同,需要按照实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值