给我一个springboot的经典案例,包含所有代码
时间: 2023-12-03 18:43:04 浏览: 41
一个经典的 Spring Boot 应用程序案例是一个简单的 CRUD(增删改查)Web 应用程序。下面是一个基于 Spring Boot、Spring Data JPA 和 Thymeleaf 的示例。
1. 创建一个 Spring Boot 项目
在 IntelliJ IDEA 中,选择 File > New Project,然后选择 Spring Initializr。在 Spring Initializr 中,选择 Maven Project,然后选择 Spring Boot 版本和项目元数据。
2. 添加依赖项
在 pom.xml 文件中添加以下依赖项:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
```
3. 创建实体类
在 src/main/java 目录下创建一个名为 com.example.demo.model 的包,并在其中创建一个名为 User 的类。
```java
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
// getter/setter 省略
}
```
4. 创建存储库接口
在 src/main/java 目录下创建一个名为 com.example.demo.repository 的包,并在其中创建一个名为 UserRepository 的接口。
```java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
```
5. 创建控制器
在 src/main/java 目录下创建一个名为 com.example.demo.controller 的包,并在其中创建一个名为 UserController 的类。
```java
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/")
public String index(Model model) {
model.addAttribute("users", userRepository.findAll());
return "index";
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("user", new User());
return "add";
}
@PostMapping("/save")
public String save(@ModelAttribute("user") User user) {
userRepository.save(user);
return "redirect:/";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable Long id, Model model) {
model.addAttribute("user", userRepository.findById(id));
return "edit";
}
@PostMapping("/update")
public String update(@ModelAttribute("user") User user) {
userRepository.save(user);
return "redirect:/";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable Long id) {
userRepository.deleteById(id);
return "redirect:/";
}
}
```
6. 创建 Thymeleaf 模板
在 src/main/resources/templates 目录下创建一个名为 index.html 的模板。
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>CRUD Demo</title>
</head>
<body>
<h1>CRUD Demo</h1>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.firstName}"></td>
<td th:text="${user.lastName}"></td>
<td th:text="${user.email}"></td>
<td><a th:href="@{/edit/{id}(id=${user.id})}">Edit</a></td>
<td><a th:href="@{/delete/{id}(id=${user.id})}">Delete</a></td>
</tr>
</tbody>
</table>
<br />
<a th:href="@{/add}">Add User</a>
</body>
</html>
```
在 src/main/resources/templates 目录下创建一个名为 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}" th:object="${user}" method="post">
<label for="firstName">First Name:</label>
<input type="text" th:field="*{firstName}" />
<br />
<label for="lastName">Last Name:</label>
<input type="text" th:field="*{lastName}" />
<br />
<label for="email">Email:</label>
<input type="email" th:field="*{email}" />
<br />
<input type="submit" value="Save" />
</form>
</body>
</html>
```
在 src/main/resources/templates 目录下创建一个名为 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}" th:object="${user}" method="post">
<input type="hidden" th:field="*{id}" />
<label for="firstName">First Name:</label>
<input type="text" th:field="*{firstName}" />
<br />
<label for="lastName">Last Name:</label>
<input type="text" th:field="*{lastName}" />
<br />
<label for="email">Email:</label>
<input type="email" th:field="*{email}" />
<br />
<input type="submit" value="Update" />
</form>
</body>
</html>
```
7. 运行应用程序
右键单击 DemoApplication 类,选择 Run DemoApplication。在浏览器中访问 http://localhost:8080/ 即可查看应用程序。
完整的代码可以在 GitHub 上找到:https://github.com/luqmanshofa/spring-boot-crud-demo。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)