SpringBoot整合Mysql实现简单的增删改查

1. 添加依赖

在项目的pom.xml文件中添加Spring Boot和MySQL的依赖:

java 复制代码
<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>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

2. 配置application.properties

在项目的resources目录下创建或修改application.properties文件,配置MySQL数据库连接信息:

java 复制代码
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update

3. 创建实体类

创建一个实体类,用于映射数据库表。例如,创建一个User实体类:

java 复制代码
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 Integer age;

    // getter和setter方法
}

4. 创建Repository接口

创建一个继承自JpaRepository的接口,用于操作数据库。例如,创建一个UserRepository接口:

java 复制代码
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}

5. 创建Service类

创建一个Service类,用于处理业务逻辑。例如,创建一个UserService类:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> findAll() {
        return userRepository.findAll();
    }

    public User save(User user) {
        return userRepository.save(user);
    }

    public void deleteById(Long id) {
        userRepository.deleteById(id);
    }

    public User update(User user) {
        return userRepository.save(user);
    }
}

6. 创建Controller类

创建一个Controller类,用于处理HTTP请求。例如,创建一个UserController类:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    @PostMapping
    public User save(@RequestBody User user) {
        return userService.save(user);
    }

    @PutMapping("/{id}")
    public User update(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return userService.update(user);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id) {
        userService.deleteById(id);
    }
}

7. 运行项目

启动Spring Boot应用,访问http://localhost:8080/users,即可实现增删改查功能。

相关推荐
captain3761 小时前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
小此方2 小时前
Re:Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
深蓝AI3 小时前
Python 3.13 的 Queue.shutdown:异步消费者终于能正常收工了
后端
smile_life_3 小时前
MySQL 设置 防止账户被暴力破解
数据库·mysql
武子康3 小时前
MultiNet6 空闲崩溃:`EXCVADDR=0x8` 后又出现 `CORRUPT HEAP`
人工智能·后端·agent
滕州市燕猫虎计算机科技工作室个体工商户4 小时前
MySQL、MVCC、多版本并发控制
数据库·mysql·mvcc·数据库事务
武子康5 小时前
MultiNet7 每轮都 clean,ESP32-P4 内存为什么还在掉?
人工智能·后端·agent
超级架构师5 小时前
PRAXO-CORE 2.5.0:用 conformance 和显式边界构建 Go Agent 框架
开发语言·人工智能·后端·golang·ai编程·智能体框架·praxo-core
陈随易5 小时前
AI 运维服务器,安全又可靠的方法
前端·后端·程序员
Decay__6 小时前
09_MySQL 闪回与时间点恢复 —— binlog 回放、mysqlbinlog、PITR 与误删恢复
运维·数据库·sql·mysql·架构