Spring Boot 集成 MyBatis Plus 请求流程详解:从控制层到数据库操作
Spring Boot 集成 MyBatis Plus 请求流程详解:从控制层到数据库操作
当 Spring Boot 集成 MyBatis Plus 后,请求的流程通常如下:
- 用户发出请求,请求会先进入 Controller 层。
- Controller 层会调用 Service 层来处理请求。
- Service 层会调用 MyBatis Plus 提供的 Mapper 接口来进行数据库操作,例如查询数据、插入数据、更新数据等。
- MyBatis Plus 会自动生成 SQL 语句,并将结果封装为 Java 对象返回给 Service 层。
- Service 层将处理结果返回给 Controller 层。
- Controller 层再将结果返回给用户。
在这个流程中,MyBatis Plus 的作用是将 Java 对象和数据库表进行映射,提供了方便的 CRUD 操作,大大简化了数据库操作的流程。
服务层如何调用 MyBatis Plus 提供的 Mapper 操作数据库
在 Spring Boot 集成 MyBatis Plus 中,Service 层可以通过注入 MyBatis Plus 提供的 Mapper 接口来进行数据库操作。具体步骤如下:
- 在 Service 层中声明一个 Mapper 接口的成员变量:
@Autowired
private UserMapper userMapper;
其中,UserMapper 是通过 MyBatis Plus 自动生成的 Mapper 接口,用于操作用户表。
- 在 Service 层中定义相应的方法,利用 Mapper 接口进行数据库操作:
public User getUserById(Long id) {
return userMapper.selectById(id);
}
public int addUser(User user) {
return userMapper.insert(user);
}
public int updateUser(User user) {
return userMapper.updateById(user);
}
public int deleteUser(Long id) {
return userMapper.deleteById(id);
}
上述方法分别用于查询、插入、更新和删除用户数据,通过调用 MyBatis Plus 提供的 Mapper 接口来完成相应的数据库操作。
控制层调用服务层的方法
控制层调用的服务层方法是我们自己写的,而 MyBatis Plus 提供的 Mapper 接口是用于在服务层中进行数据库操作的工具。我们可以在服务层中自己定义相应的方法,利用 MyBatis Plus 提供的 Mapper 接口进行数据库操作。例如,在服务层中定义查询用户的方法 getUserById(),就可以利用 MyBatis Plus 提供的 selectById() 方法来查询用户数据。这样可以方便地完成数据库操作,提高开发效率。
使用继承简化数据库操作
如果服务层是接口,我们可以通过 MyBatis Plus 提供的 MapperFactoryBean 来生成 Mapper 接口的实现类。具体步骤如下:
- 在 Service 层中定义一个接口,用于声明数据库操作的方法:
public interface UserService {
User getUserById(Long id);
int addUser(User user);
int updateUser(User user);
int deleteUser(Long id);
}
- 在 Service 层中通过 MapperFactoryBean 生成 Mapper 接口的实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public int addUser(User user) {
return userMapper.insert(user);
}
@Override
public int updateUser(User user) {
return userMapper.updateById(user);
}
@Override
public int deleteUser(Long id) {
return userMapper.deleteById(id);
}
@Bean
public MapperFactoryBean<UserMapper> userMapper() throws Exception {
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
factoryBean.setSqlSessionFactory(sqlSessionFactoryBean().getObject());
return factoryBean;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
return sessionFactoryBean;
}
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource('jdbc:mysql://localhost:3306/test', 'root', 'password');
}
}
其中,MapperFactoryBean 用于生成 Mapper 接口的实现类,SqlSessionFactoryBean 用于创建 SqlSessionFactory,DataSource 用于配置数据库连接信息。
通过以上步骤,我们就可以在 Service 层中通过注入 UserService 接口来调用数据库操作的方法,而这些方法的具体实现则是通过 MyBatis Plus 提供的 Mapper。
示例:服务层调用 MyBatis Plus 查询数据
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
}
在上述代码中,我们定义了一个 UserService 的实现类 UserServiceImpl,其中的 getUserById() 方法利用 MyBatis Plus 提供的 selectById() 方法进行了查询操作。在方法中,我们通过注入 UserMapper 接口来调用 selectById() 方法,实现了从数据库中查询用户信息的功能。
使用继承简化数据库操作
我们可以直接继承 MyBatis Plus 提供的 ServiceImpl 类,它已经实现了常见的 CRUD 操作,可以直接使用。例如:
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {
@Autowired
private StudentMapper studentMapper;
// 可以直接使用继承的方法
}
在上述代码中,我们定义了一个 StudentServiceImpl 的实现类,通过继承 MyBatis Plus 提供的 ServiceImpl 类,可以直接使用其中的 CRUD 操作。同时,我们也可以通过注入 StudentMapper 来进行自定义的数据库操作。
控制层调用服务层的方法
控制层可以通过注入服务层的接口来调用服务层的方法。例如:
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping('/users/{id}')
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
// 其他方法省略
}
在上述代码中,我们在控制层 UserController 中注入了 UserService 接口,并在 getUserById() 方法中调用了 UserService 接口中的 getUserById() 方法,从而实现了查询指定 id 用户的功能。
注意,这里的 getUserById() 方法是我们在服务层中自定义的方法,而不是 MyBatis Plus 自带的方法。如果要使用 MyBatis Plus 自带的方法,可以直接调用 ServiceImpl 中的方法,例如:
@GetMapping('/users/{id}')
public User getUserById(@PathVariable Long id) {
return userService.getById(id);
}
在上述代码中,我们直接调用了 ServiceImpl 中的 getById() 方法,从而实现了查询指定 id 用户的功能。
通过本文的讲解,相信您已经对 Spring Boot 集成 MyBatis Plus 的请求流程有了更深入的理解,并掌握了如何使用 MyBatis Plus 提供的 Mapper 进行数据库操作。希望本文能够帮助您更好地进行 Spring Boot 应用开发。
原文地址: https://www.cveoy.top/t/topic/lB8g 著作权归作者所有。请勿转载和采集!