1. JPA
JPA 是 Java 中用于对象关系映射(ORM)的规范,它允许开发人员使用面向对象的方式来操作数据库。通过 JPA,我们可以轻松地进行数据库的增删改查操作,而无需编写大量的 SQL 语句。
2. Thymeleaf
Thymeleaf 是一种现代的服务器端 Java 模板引擎,它可以与 Spring MVC 等框架无缝集成。Thymeleaf 提供了丰富的模板语法,使得开发人员可以在 HTML 页面中直接嵌入动态数据,从而实现动态页面的生成。
二、项目准备
1. 创建项目
使用 Maven 或 Gradle 创建一个新的 Java Web 项目,并添加 JPA 和 Thymeleaf 的依赖。
2. 配置数据库
在项目的配置文件中配置数据库连接信息,可以使用 MySQL、PostgreSQL 等数据库。
3. 创建实体类
根据业务需求创建实体类,实体类对应数据库中的表。使用 JPA 的注解来标识实体类的属性和关系。
例如,创建一个用户实体类:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// 构造函数、Getter 和 Setter 方法
}
三、实现增删改查功能
1. 数据访问层(DAO)
创建数据访问层接口,用于执行数据库操作。使用 JPA 的 Repository 接口来实现基本的增删改查操作。
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {
}
2. 服务层(Service)
创建服务层接口和实现类,用于处理业务逻辑。在服务层中调用数据访问层的方法来执行数据库操作。
3. 控制器层(Controller)
创建控制器类,用于处理 HTTP 请求。在控制器中调用服务层的方法来执行相应的业务逻辑,并返回 Thymeleaf 模板页面。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public String listUsers(Model model) {
model.addAttribute("users", userService.findAll());
return "users/list";
}
@GetMapping("/users/add")
public String showAddUserForm(Model model) {
model.addAttribute("user", new User());
return "users/add";
}
@PostMapping("/users/add")
public String addUser(@ModelAttribute("user") User user) {
userService.save(user);
return "redirect:/users";
}
@GetMapping("/users/edit/{id}")
public String showEditUserForm(@PathVariable Long id, Model model) {
User user = userService.findById(id);
model.addAttribute("user", user);
return "users/edit";
}
@PostMapping("/users/edit/{id}")
public String editUser(@PathVariable Long id, @ModelAttribute("user") User user) {
user.setId(id);
userService.save(user);
return "redirect:/users";
}
@GetMapping("/users/delete/{id}")
public String deleteUser(@PathVariable Long id) {
userService.deleteById(id);
return "redirect:/users";
}
}
四、Thymeleaf 模板
1. 列表页面(list.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<h1>用户列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.email}"></td>
<td>
<a th:href="@{/users/edit/{id}(id=${user.id})}">编辑</a>
<a th:href="@{/users/delete/{id}(id=${user.id})}">删除</a>
</td>
</tr>
</tbody>
</table>
<a th:href="@{/users/add}">添加用户</a>
</body>
</html>
2. 添加页面(add.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<h1>添加用户</h1>
<form th:action="@{/users/add}" method="post">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required><br>
<input type="submit" value="添加">
</form>
</body>
</html>
3. 编辑页面(edit.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>编辑用户</title>
</head>
<body>
<h1>编辑用户</h1>
<form th:action="@{/users/edit/{id}(id=${user.id})}" method="post">
<input type="hidden" name="_method" value="PUT">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" th:value="${user.name}" required><br>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" th:value="${user.email}" required><br>
<input type="submit" value="保存">
</form>
</body>
</html>