1. Spring 与 MyBatis 的整合概述
Spring与MyBatis整合主要通过Spring提供的SqlSessionFactoryBean
、MapperFactoryBean
以及MyBatis的核心组件来实现。这些组件在Spring容器启动时初始化,并在应用运行时提供数据库操作的功能。
2. 核心组件解析
2.1 SqlSessionFactoryBean
SqlSessionFactoryBean
是Spring与MyBatis整合的核心组件之一。它负责创建SqlSessionFactory
,SqlSessionFactory
用于生成SqlSession
,SqlSession
是MyBatis执行SQL语句的核心对象。
@Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); }
SqlSessionFactoryBean
通过DataSource
创建SqlSessionFactory
,并将其注册到Spring容器中。它的核心方法是afterPropertiesSet
和buildSqlSessionFactory
。
2.2 MapperFactoryBean
MapperFactoryBean
用于生成MyBatis的Mapper接口代理对象,并将其注册到Spring容器中。它实现了Spring的FactoryBean
接口,可以生成自定义的Bean。
public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> { private Class<T> mapperInterface; @Override public T getObject() throws Exception { return getSqlSession().getMapper(this.mapperInterface); } @Override public Class<?> getObjectType() { return this.mapperInterface; } }
MapperFactoryBean
通过SqlSession
创建Mapper接口的代理对象,并返回给Spring容器。Spring容器在需要时会调用getObject
方法获取Mapper实例。
3. 源码解析
3.1 SqlSessionFactoryBean 的初始化
在Spring容器启动过程中,SqlSessionFactoryBean
的afterPropertiesSet
方法会被调用。这个方法调用了buildSqlSessionFactory
来创建SqlSessionFactory
。
@Override public void afterPropertiesSet() throws Exception { if (this.sqlSessionFactory == null) { afterPropertiesSet(); } } protected SqlSessionFactory buildSqlSessionFactory() throws Exception { Configuration configuration = new Configuration(); // 配置相关属性 return new SqlSessionFactoryBuilder().build(configuration); }
buildSqlSessionFactory
方法创建了MyBatis的Configuration
对象,并调用SqlSessionFactoryBuilder
的build
方法创建SqlSessionFactory
。
3.2 MapperFactoryBean 的工作原理
MapperFactoryBean
在Spring容器启动时会被初始化,并调用getObject
方法生成Mapper接口的代理对象。
@Override public T getObject() throws Exception { return getSqlSession().getMapper(this.mapperInterface); }
getSqlSession
方法获取当前的SqlSession
,getMapper
方法使用JDK动态代理生成Mapper接口的代理对象。
4. 事务管理
Spring与MyBatis整合时,事务管理是一个重要的部分。Spring通过DataSourceTransactionManager
管理事务。DataSourceTransactionManager
依赖于DataSource
,而DataSource
由Spring管理。
@Bean public DataSourceTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }
Spring通过AOP拦截需要事务管理的方法,利用TransactionInterceptor
实现事务的开启、提交和回滚。
5. 实际应用
以下是Spring整合MyBatis的实际应用示例:
5.1 配置类
@Configuration @MapperScan("com.example.mapper") public class MyBatisConfig { @Bean public DataSource dataSource() { // 配置数据源 } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } @Bean public DataSourceTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
5.2 Mapper接口
public interface UserMapper { User selectUserById(Long id); }
5.3 服务类
@Service public class UserService { @Autowired private UserMapper userMapper; @Transactional public User getUserById(Long id) { return userMapper.selectUserById(id); } }
6. 总结
Spring与MyBatis的整合通过SqlSessionFactoryBean
、MapperFactoryBean
等核心组件,实现了自动化的数据库操作和事务管理。理解这些底层源码的工作原理,能够帮助开发者更好地进行问题排查和性能优化。