用java做一个用户管理网页有增删改查按钮并连接数据库并附原代码
时间: 2024-05-16 19:12:13 浏览: 17
好的,这个问题我可以回答。以下是一份使用 Java 开发的用户管理网页,具备增删改查功能并连接 PostgreSQL 数据库的代码示例:
首先,我们需要在 Maven 中添加以下依赖项:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
```
然后,我们可以创建一个名为 `User` 的实体类来表示用户信息,代码如下所示:
```
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String name;
@Email
private String email;
// 省略其他属性和 getter/setter 方法
}
```
在 `UserRepository` 接口中,我们可以使用 Spring Data JPA 自动实现基本的 CRUD 操作,代码如下所示:
```
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
```
接下来,我们可以使用 Spring MVC 框架来实现控制器和视图,代码如下所示:
```
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/users")
public String listUsers(Model model) {
List<User> users = userRepository.findAll();
model.addAttribute("users", users);
return "users/list";
}
@GetMapping("/users/new")
public String newUser(Model model) {
model.addAttribute("user", new User());
return "users/form";
}
@PostMapping("/users")
public String saveUser(@Valid User user, BindingResult result) {
if (result.hasErrors()) {
return "users/form";
}
userRepository.save(user);
return "redirect:/users";
}
@GetMapping("/users/{id}/edit")
public String editUser(@PathVariable Long id, Model model) {
User user = userRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user ID: " + id));
model.addAttribute("user", user);
return "users/form";
}
@DeleteMapping("/users/{id}")
public String deleteUser(@PathVariable Long id) {
userRepository.deleteById(id);
return "redirect:/users";
}
}
```
最后,我们可以使用 Thymeleaf 模板引擎来渲染视图,代码如下所示:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>User Management</title>
</head>
<body>
<h1>User Management</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</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>
<form th:action="@{/users/__${user.id}__}" method="post" th:object="${user}">
<button type="submit">Delete</button>
</form>
</td>
</tr>
</tbody>
</table>
<a href="/users/new">Add User</a>
</body>
</html>
```
当用户访问 `/users` 路径时,就可以显示所有用户的列表。当用户点击“Add User”按钮时,就可以跳转到添加用户的页面。当用户提交表单时,就可以将用户信息保存到数据库中。当用户点击列表中的“Delete”按钮时,就可以将对应的用户信息从数据库中删除。
希望这份代码示例能对你有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)