用springboot写一个管理系统
时间: 2023-07-18 12:03:00 浏览: 47
好的,我可以给你提供一个简单的管理系统的示例,它使用了Spring Boot,Thymeleaf模板和MySQL数据库。这个系统可以用来管理用户信息,包括添加、编辑、删除和搜索用户。
首先,我们需要创建一个Spring Boot项目。你可以使用Spring Initializr来创建一个空的项目,然后将以下内容添加到pom.xml中,以便我们可以使用Thymeleaf和MySQL:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
接下来,我们需要创建一个用户实体类,包括id,姓名,年龄和电子邮件属性。可以在src/main/java目录下创建一个名为User.java的类:
```java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Name is required")
@Size(max = 50)
private String name;
@NotNull(message = "Age is required")
@Min(18)
private Integer age;
@NotBlank(message = "Email is required")
@Size(max = 100)
@Email
private String email;
// getters and setters
}
```
然后我们需要创建一个用户存储库接口,它将使用Spring Data JPA来管理用户实体。可以在src/main/java目录下创建一个名为UserRepository.java的接口:
```java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByNameContainingIgnoreCase(String name);
}
```
在这个接口中,我们定义了一个方法来根据姓名搜索用户。
接下来,我们需要创建一个控制器来处理用户管理。可以在src/main/java目录下创建一个名为UserController.java的类:
```java
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/")
public String index(Model model) {
List<User> users = userRepository.findAll();
model.addAttribute("users", users);
return "index";
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("user", new User());
return "form";
}
@PostMapping("/save")
public String save(@Valid @ModelAttribute("user") User user, BindingResult result) {
if (result.hasErrors()) {
return "form";
}
userRepository.save(user);
return "redirect:/";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
User user = userRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user id: " + id));
model.addAttribute("user", user);
return "form";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user id: " + id));
userRepository.delete(user);
return "redirect:/";
}
@GetMapping("/search")
public String search(@RequestParam(name = "name", required = false) String name, Model model) {
List<User> users;
if (StringUtils.isBlank(name)) {
users = userRepository.findAll();
} else {
users = userRepository.findByNameContainingIgnoreCase(name);
}
model.addAttribute("users", users);
return "index";
}
}
```
在这个控制器中,我们定义了以下方法:
- index:用于显示所有用户的列表。
- add:用于添加新用户的表单。
- save:用于保存新用户或更新现有用户。
- edit:用于编辑现有用户的表单。
- delete:用于删除现有用户。
- search:用于根据名称搜索用户。
最后,我们需要创建一些Thymeleaf模板来渲染用户管理页面。可以在src/main/resources/templates目录下创建以下模板文件:
- index.html:用于显示所有用户的列表。
- form.html:用于添加/编辑用户的表单。
index.html模板如下:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>User Management</title>
</head>
<body>
<h1>User Management</h1>
<form th:action="@{/search}" method="get">
<input type="text" name="name" placeholder="Search by name">
<button type="submit">Search</button>
</form>
<table border="1">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Email</th>
<th>Actions</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.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 New User</a>
</body>
</html>
```
form.html模板如下:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>User Form</title>
</head>
<body>
<h1>User Form</h1>
<form th:object="${user}" th:action="@{/save}" method="post">
<div>
<label>Name:</label>
<input type="text" th:field="*{name}">
<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></span>
</div>
<div>
<label>Age:</label>
<input type="number" th:field="*{age}">
<span th:if="${#fields.hasErrors('age')}" th:errors="*{age}"></span>
</div>
<div>
<label>Email:</label>
<input type="email" th:field="*{email}">
<span th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></span>
</div>
<button type="submit">Save</button>
</form>
<a th:href="@{/}">Back to list</a>
</body>
</html>
```
现在,你可以运行这个应用程序并访问http://localhost:8080/来查看用户管理页面。