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,即可实现增删改查功能。

相关推荐
heze0916 小时前
sqli-labs-Less-18自动化注入方法
mysql·网络安全·自动化
浮尘笔记16 小时前
Go语言临时对象池:sync.Pool的原理与使用
开发语言·后端·golang
梦梦代码精17 小时前
BuildingAI vs Dify vs 扣子:三大开源智能体平台架构风格对比
开发语言·前端·数据库·后端·架构·开源·推荐算法
REDcker18 小时前
RESTful API设计规范详解
服务器·后端·接口·api·restful·博客·后端开发
危险、18 小时前
一套提升 Spring Boot 项目的高并发、高可用能力的 Cursor 专用提示词
java·spring boot·提示词
sunnyday042618 小时前
基于Netty构建WebSocket服务器实战指南
服务器·spring boot·websocket·网络协议
没有bug.的程序员20 小时前
Java 序列化:Serializable vs. Protobuf 的性能与兼容性深度对比
java·开发语言·后端·反射·序列化·serializable·protobuf
我爱娃哈哈21 小时前
SpringBoot + Spring Security + RBAC:企业级权限模型设计与动态菜单渲染实战
spring boot·后端·spring
oh LAN21 小时前
提升性能:数据库与 Druid 连接池优化指南
数据库·mysql
小王不爱笑1321 天前
SpringBoot 配置文件
java·spring boot·后端