Spring Boot 集成 MySQL 、 Spring Data JPA 和Mybatis-Plus依赖。创建一个实体类 Student,包含 id、studentId(学号)、name(姓名)等属性。在数据库中创建Student表,使用Mybatis-Plus连接数据库,实现对 Student数据的增删改查操作,其中添加学生信息时,学号和姓名使用自己的信息。
时间: 2025-04-05 18:02:15 浏览: 49
### Spring Boot集成MySQL、Spring Data JPA和MyBatis-Plus并实现Student实体类的CRUD操作
#### 1. 添加依赖
为了在Spring Boot项目中集成MySQL、Spring Data JPA以及MyBatis-Plus,需要在`pom.xml`文件中引入相应的依赖。
以下是必要的Maven依赖配置:
```xml
<dependencies>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL Connector Java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- MyBatis Plus Starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<!-- Jasypt Starter (用于加密数据库密码) -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
```
上述代码片段展示了如何添加所需的依赖项[^1]。
---
#### 2. 配置文件设置 (`application.yml`)
在项目的资源目录下创建或编辑`application.yml`文件,配置MySQL连接参数以及其他必要选项。
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC
username: root
password: EncryptedPasswordHere # 使用Jasypt加密后的密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
level:
com.example.mapper: DEBUG
```
注意:如果启用了Jasypt,则需确保数据库密码已加密处理。
---
#### 3. 创建Student实体类
定义一个名为`Student`的Java类作为实体模型。该类应映射到数据库中的表结构。
```java
package com.example.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("student") // 映射到数据库中的'student'表
public class Student {
@TableId
private Long studentId; // 主键字段
private String name; // 学生姓名
}
```
此部分实现了基本的数据对象建模[^3]。
---
#### 4. Mapper接口设计
利用MyBatis-Plus自动生成的基础功能来简化开发流程。编写如下所示的Mapper接口即可完成数据访问层的设计。
```java
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Student;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
// 自动继承增删改查方法无需额外编码
}
```
通过扩展`BaseMapper<T>`可以快速获得一系列内置的操作支持。
---
#### 5. Service服务逻辑构建
接下来定义业务逻辑层的服务接口及其具体实现形式。
```java
// 定义Service接口
package com.example.service;
import com.example.entity.Student;
import com.baomidou.mybatisplus.extension.service.IService;
public interface IStudentService extends IService<Student> {}
// 实现Service接口
package com.example.service.impl;
import com.example.entity.Student;
import com.example.mapper.StudentMapper;
import com.example.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {}
```
以上步骤完成了基于MyBatis-Plus框架下的基础架构搭建工作。
---
#### 6. Controller控制器暴露API端点
最后一步是在Web应用层面公开RESTful风格的HTTP请求入口供外部调用者交互使用。
```java
package com.example.controller;
import com.example.entity.Student;
import com.example.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private IStudentService studentService;
@PostMapping
public boolean add(@RequestBody Student student){
return studentService.save(student);
}
@DeleteMapping("/{id}")
public boolean deleteById(@PathVariable Long id){
return studentService.removeById(id);
}
@PutMapping
public boolean update(@RequestBody Student student){
return studentService.updateById(student);
}
@GetMapping("/{id}")
public Student findById(@PathVariable Long id){
return studentService.getById(id);
}
}
```
至此已经成功集成了所需技术栈,并提供了完整的增删改查能力。
---
###
阅读全文
相关推荐











