整合Spring Boot 2.x和MyBatis Plus MySQL的步骤
1. 创建Spring Boot项目
首先,我们需要创建一个新的Spring Boot项目。可以使用Spring Initializr或者手动创建一个空的Maven项目,并添加Spring Boot的依赖。
2. 添加MyBatis Plus和MySQL依赖
在pom.xml文件中,添加以下依赖:
<dependencies>
<!-- Spring Boot Web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>latest-version</version>
</dependency>
<!-- MySQL依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
请将latest-version
替换为最新版本号。这些依赖将使我们能够使用MyBatis Plus和连接MySQL数据库。
3. 配置数据库连接
在application.properties
(或application.yml
)文件中,添加以下配置:
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
请将mydatabase
、username
和password
替换为你自己的数据库名称、用户名和密码。
4. 创建实体类
创建一个实体类,表示数据库中的一张表。例如,我们创建一个User
实体类,其中包含id
和name
字段。
public class User {
private Long id;
private String name;
// 省略getter和setter方法
}
5. 创建Mapper接口
创建一个Mapper接口,用于定义数据库操作方法。例如,我们创建一个UserMapper
接口,继承自BaseMapper<User>
。
public interface UserMapper extends BaseMapper<User> {
// 可以添加自定义的数据库操作方法
}
6. 创建Service接口和实现类
创建一个Service接口和实现类,用于封装业务逻辑。例如,我们创建一个UserService
接口和UserServiceImpl
实现类。
public interface UserService {
List<User> getAllUsers();
User getUserById(Long id);
void saveUser(User user);
void updateUser(User user);
void deleteUser(Long id);
}
@Service
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
public UserServiceImpl(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public List<User> getAllUsers() {
return userMapper.selectList(null);
}
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public void saveUser(User user) {
userMapper.insert(user);
}
@Override
public void updateUser(User user) {
userMapper.updateById(user);
}
@Override
public void deleteUser(Long id) {
userMapper.deleteById(id);
}
}
7. 创建Controller
创建一个Controller类,用于处理HTTP请求。例如,我们创建一个UserController
类。
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
@PostMapping
public void saveUser(@RequestBody User user) {
userService.saveUser(user);
}
@PutMapping("/{id}")
public void updateUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
userService.updateUser(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
}
8. 启动应用程序
在Application
类中添加@SpringBootApplication
注解,并编写main
方法。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
9. 测试API接口
可以使用Postman或其他工具来测试我们创建的API接口。
整体流程图
以下是整合Spring Boot 2.x和MyBatis Plus MySQL的整体流程图:
pie
title 整合