我使用 MySQL 创建了一个用户表,关键字有user_id、user_name、account、password 、phone 、address 、role,请使用 springboot+hibernate+thymeleaf 帮我完成分页查询所有用户的功能,查询到的结果返回user-check.html,包含Controller、DAO、Entity、Service

时间: 2023-07-10 09:05:04 浏览: 52
好的,以下是示例代码。 首先是 Entity 类: ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long user_id; private String user_name; private String account; private String password; private String phone; private String address; private String role; // 省略 getter 和 setter 方法 } ``` 然后是 DAO 层: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 接下来是 Service 层: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getAllUsers() { return userRepository.findAll(); } public Page<User> getUsersByPage(int pageNum, int pageSize) { Pageable pageable = PageRequest.of(pageNum - 1, pageSize); return userRepository.findAll(pageable); } } ``` 最后是 Controller 层: ```java @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/user/check") public String checkUsers(Model model, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { Page<User> users = userService.getUsersByPage(pageNum, pageSize); model.addAttribute("users", users.getContent()); model.addAttribute("currentPage", users.getNumber() + 1); model.addAttribute("totalPages", users.getTotalPages()); return "user-check"; } } ``` 在 Thymeleaf 模板文件 user-check.html 中,可以使用类似如下的代码来显示查询到的用户列表: ```html <table> <thead> <tr> <th>user_id</th> <th>user_name</th> <th>account</th> <th>password</th> <th>phone</th> <th>address</th> <th>role</th> </tr> </thead> <tbody> <tr th:each="user : ${users}"> <td th:text="${user.user_id}"></td> <td th:text="${user.user_name}"></td> <td th:text="${user.account}"></td> <td th:text="${user.password}"></td> <td th:text="${user.phone}"></td> <td th:text="${user.address}"></td> <td th:text="${user.role}"></td> </tr> </tbody> </table> <div> <ul class="pagination"> <li th:class="${currentPage == 1} ? disabled : ''"> <a th:href="@{/user/check(pageNum=1,pageSize=${pageSize})}" aria-label="First"> <span aria-hidden="true">&laquo;</span> </a> </li> <li th:class="${currentPage == 1} ? disabled : ''"> <a th:href="@{/user/check(pageNum=${currentPage - 1},pageSize=${pageSize})}" aria-label="Previous"> <span aria-hidden="true">&lt;</span> </a> </li> <li th:class="${currentPage == totalPages} ? disabled : ''"> <a th:href="@{/user/check(pageNum=${currentPage + 1},pageSize=${pageSize})}" aria-label="Next"> <span aria-hidden="true">&gt;</span> </a> </li> <li th:class="${currentPage == totalPages} ? disabled : ''"> <a th:href="@{/user/check(pageNum=${totalPages},pageSize=${pageSize})}" aria-label="Last"> <span aria-hidden="true">&raquo;</span> </a> </li> </ul> </div> ``` 以上就是使用 Spring Boot、Hibernate 和 Thymeleaf 实现分页查询所有用户的示例代码了。

相关推荐

最新推荐

recommend-type

MySQL中临时表的基本创建与使用教程

当工作在非常大的表上时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后在这些表运行查询。...
recommend-type

轻松掌握MySQL函数中的last_insert_id()

最近一个同事问我,为什么last_insert_id()得到的结果与预期的不一样呢,于是我就认真的去研究的一下这个参数,下面是关于last_insert_id()的详细介绍,一起来学习学习吧。 首先,举个例子 wing@3306&gt;show create ...
recommend-type

mysql中find_in_set()函数的使用及in()用法详解

主要介绍了mysql中find_in_set()函数的使用以及in()用法详解,需要的朋友可以参考下
recommend-type

实验训练1 在MySQL中创建数据库和表.docx

有安装、创建库、创建表、增加、修改、删除、查询等等一共16页,有截图、很详细的步骤讲解、操作,用于国家开发大学形考考试作业用。
recommend-type

创建mysql表分区的方法

表分区是最近才知道的哦 ,以前自己做都是分表来实现上亿级别的数据了,下面我来给大家介绍一下mysql表分区创建与使用吧,希望对各位同学会有所帮助。表分区的测试使用,主要内容来自于其他博客文章以及mysql5.1的...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。