SpringBoot 整合 JPA 实现增删查改功能

本文介绍了如何在SpringBoot应用中整合JPA来实现对Mysql数据库的增删查改功能。首先,详细讲述了准备工作,包括安装Mysql和初始化数据库。接着,通过添加相关依赖、配置数据源及JPA配置,完成了整合步骤。然后,展示了编码实现过程,包括实体类和Repository接口的编写。最后,进行了简单的测试,并指出JPA底层使用的是Hibernate框架,后续会进一步探讨另一种在国内广泛应用的持久层框架。
摘要由CSDN通过智能技术生成
实现网页端增删查改可以分为以下几个步骤: 1. 创建一个Spring Boot项目,引入相关依赖,如Spring Web、Thymeleaf、Spring Data JPA等。 2. 创建实体类,用于映射数据库表,同时定义相关属性和方法。 3. 创建Repository接口,用于定义查询和持久化数据的方法,这里使用Spring Data JPA。 4. 创建控制器(Controller)类,用于处理HTTP请求和响应,同时定义相关的路由和方法,如查询、新增、修改、删除等。 5. 创建视图(View)层,使用Thymeleaf模板引擎,实现页面的展示和交互。 6. 启动项目,测试页面的增删查改功能。 下面是一个简单的示例代码: 1. 实体类 ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; private String email; // 省略getter和setter方法 } ``` 2. Repository接口 ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 3. 控制器(Controller)类 ```java @Controller public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/") public String index(Model model) { List<User> userList = userRepository.findAll(); model.addAttribute("userList", userList); return "index"; } @GetMapping("/add") public String add() { return "add"; } @PostMapping("/save") public String save(User user) { userRepository.save(user); return "redirect:/"; } @GetMapping("/edit/{id}") public String edit(@PathVariable Long id, Model model) { User user = userRepository.findById(id).orElse(null); model.addAttribute("user", user); return "edit"; } @PostMapping("/update") public String update(User user) { userRepository.save(user); return "redirect:/"; } @GetMapping("/delete/{id}") public String delete(@PathVariable Long id) { userRepository.deleteById(id); return "redirect:/"; } } ``` 4. 视图(View)层 index.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>User List</title> </head> <body> <h1>User List</h1> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Email</th> <th>Operation</th> </tr> </thead> <tbody> <tr th:each="user : ${userList}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td th:text="${user.email}"></td> <td> <a th:href="@{/edit/{id}(id=${user.id})}">Edit</a> <a th:href="@{/delete/{id}(id=${user.id})}">Delete</a> </td> </tr> </tbody> </table> <a th:href="@{/add}">Add User</a> </body> </html> ``` add.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Add User</title> </head> <body> <h1>Add User</h1> <form th:action="@{/save}" method="post"> <label>Name:</label> <input type="text" name="name"/><br/> <label>Age:</label> <input type="text" name="age"/><br/> <label>Email:</label> <input type="text" name="email"/><br/> <input type="submit" value="Submit"/> </form> <a th:href="@{/}">Back</a> </body> </html> ``` edit.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Edit User</title> </head> <body> <h1>Edit User</h1> <form th:action="@{/update}" method="post"> <input type="hidden" name="id" th:value="${user.id}"/> <label>Name:</label> <input type="text" name="name" th:value="${user.name}"/><br/> <label>Age:</label> <input type="text" name="age" th:value="${user.age}"/><br/> <label>Email:</label> <input type="text" name="email" th:value="${user.email}"/><br/> <input type="submit" value="Submit"/> </form> <a th:href="@{/}">Back</a> </body> </html> ``` 注意:以上代码仅作为示例,实际开发中需要根据需求进行修改和完善。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值