SpringBoot实现增删改查开源案例
时间: 2023-06-25 13:02:07 浏览: 179
Spring Boot 是一款非常流行的 Java Web 开发框架,它可以快速开发和构建可靠的 Web 应用程序。下面是一个简单的增删改查开源案例,供参考:
1. 创建数据库表
首先,我们需要创建一个名为 `user` 的数据库表,表中包含以下字段:
```sql
CREATE TABLE user (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
age INT(3) NOT NULL,
PRIMARY KEY (id)
);
```
2. 创建 Spring Boot 项目
使用 Spring Initializr 创建一个空的 Spring Boot 项目,添加以下依赖:
```xml
<dependencies>
<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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
```
其中,`spring-boot-starter-web` 是 Spring Boot 的 Web 组件,`spring-boot-starter-data-jpa` 是 Spring Data JPA 的依赖,`h2` 是一个嵌入式数据库,用于测试和开发。
3. 创建实体类
在项目中创建一个名为 `User` 的实体类,用于映射数据库表中的数据:
```java
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer age;
// getters and setters
}
```
4. 创建 Repository
在项目中创建一个名为 `UserRepository` 的接口,继承 `JpaRepository`,并且定义一些查询方法:
```java
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByAge(Integer age);
List<User> findByNameContaining(String name);
}
```
5. 创建控制器
在项目中创建一个名为 `UserController` 的控制器,定义一些接口用于增删改查:
```java
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("")
public ResponseEntity<List<User>> findAll() {
List<User> users = userRepository.findAll();
return ResponseEntity.ok(users);
}
@PostMapping("")
public ResponseEntity<User> create(@RequestBody User user) {
User savedUser = userRepository.save(user);
return ResponseEntity.ok(savedUser);
}
@PutMapping("/{id}")
public ResponseEntity<User> update(@PathVariable Long id, @RequestBody User user) {
User savedUser = userRepository.findById(id).orElse(null);
if (savedUser == null) {
return ResponseEntity.notFound().build();
}
savedUser.setName(user.getName());
savedUser.setAge(user.getAge());
User updatedUser = userRepository.save(savedUser);
return ResponseEntity.ok(updatedUser);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
User savedUser = userRepository.findById(id).orElse(null);
if (savedUser == null) {
return ResponseEntity.notFound().build();
}
userRepository.delete(savedUser);
return ResponseEntity.ok().build();
}
}
```
6. 运行应用程序
在终端中运行以下命令:
```
mvn spring-boot:run
```
然后访问 `http://localhost:8080/users` 即可看到所有用户数据,使用 Postman 等工具可以测试接口的增删改查功能。
至此,一个简单的 Spring Boot 实现增删改查的开源案例就完成了。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)