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 操作,大大简化了数据库操作的流程。
Service 层如何调用 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 自带的?
控制层调用的服务层方法是我们自己写的,而 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。
一个简单的 Service 层调用 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() 方法,实现了从数据库中查询用户信息的功能。
针对您的代码示例:
对于代码 @Service public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
您可以直接继承 MyBatis Plus 提供的 ServiceImpl 类,它已经实现了常见的 CRUD 操作,可以直接使用。例如:
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {
@Autowired
private StudentMapper studentMapper;
// 可以直接使用继承的方法
}
在上述代码中,我们定义了一个 StudentServiceImpl 的实现类,通过继承 MyBatis Plus 提供的 ServiceImpl 类,可以直接使用其中的 CRUD 操作。同时,我们也可以通过注入 StudentMapper 来进行自定义的数据库操作。
使用继承的详细说明
使用继承是指在 Service 层中,我们可以直接继承 MyBatis Plus 提供的 ServiceImpl 类,它已经实现了常见的 CRUD 操作,可以直接使用。
例如,在我们的示例中,如果我们想要实现一个 StudentService 来操作学生表,我们可以这样定义:
public interface StudentService extends IService<Student> {
// 自定义方法
}
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {
// 可以直接使用继承的方法
}
在上述代码中,StudentService 继承了 MyBatis Plus 提供的 IService 接口,并通过泛型指定了操作的实体类为 Student。而 StudentServiceImpl 则继承了 MyBatis Plus 提供的 ServiceImpl 类,并通过泛型指定了 Mapper 接口为 StudentMapper,操作的实体类为 Student。
这样,我们就可以直接使用 ServiceImpl 类中已经实现好的常见 CRUD 操作,同时也可以自定义方法来实现我们的业务需求。在使用时,我们只需要注入 StudentService 即可。
总结
通过本文的介绍,您应该已经了解了 Spring Boot 集成 MyBatis Plus 后,请求从控制层到数据库操作的完整流程,以及如何利用 MyBatis Plus 提供的 Mapper 接口进行数据库操作。希望本文能帮助您更好地理解和使用 MyBatis Plus。
原文地址: https://www.cveoy.top/t/topic/lB8b 著作权归作者所有。请勿转载和采集!