一、基本使用
- 引入
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
- yml配置
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath*:mapper/**/*.xml
- 代码
@SpringBootApplication
@MapperScan(basePackages = "com.test.core.mapper")
public class TestApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
二、初始化流程
-
初始化的目的是构建三个对象
- SqlSessionFactory:保存了所有配置信息,构建sqlSession
- SqlSessionTemplate: 用于执行sql
- MapperScannerConfigurer:实现了BeanDefinitionRegistryPostProcessor接口,可以扫描mapper,注册到容器中
-
SqlSessionFactory
、SqlSessionTemplate
注入过程,
在pom中引入依赖的时候会引入mybatis-spring-boot-autoconfigure-2.1.0.jar
,在项目启动时,通过spring-bootspring.factories
bean查找机制加载org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
文件。
public class MybatisAutoConfiguration implements InitializingBean {
/**
MybatisProperties 保存了在yml中的配置
configurationCustomizersProvider ConfigurationCustomizer接口可以通过代码的方式自定义配置
*/
public MybatisAutoConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
//-------
}
/**
调用ConfigurationCustomizer接口对象自定义配置,前提是没有在yml文件中配置configLocation 值,即配置文件地址
*/
private void applyConfiguration(SqlSessionFactoryBean factory) {
Configuration configuration = this.properties.getConfiguration();
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
configuration = new Configuration();
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration);
}
}
factory.setConfiguration(configuration);
}
//mybatis体系核心bean,存储了配置信息
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
//-------
//由SqlSessionFactoryBean构建DefaultSqlSessionFactory对象并返回
return factory.getObject();
}
//mybatis体系核心bean,执行sql语句
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
//-------
}
}
spring事务在org.mybatis.spring.SqlSessionFactoryBean中设置
protected SqlSessionFactory buildSqlSessionFactory() throws
Exception {
/**mybatis的事务是由TransactionFactory接口对象来管理的,spring的
SpringManagedTransactionFactory实现了该接口,实现了spring的事务体系
*/
targetConfiguration.setEnvironment(new Environment(this.environment,
this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,
this.dataSource));
}
MapperScannerConfigurer
注入过程
@MapperScan 注解会引入MapperScannerRegistrar
,MapperScannerRegistrar类完成了MapperScannerConfigurer
注入
@Import(MapperScannerRegistrar.class)
@Repeatable(MapperScans.class)
public @interface MapperScan {
}
public class MapperScannerRegistrar implements
ImportBeanDefinitionRegistrar, ResourceLoaderAware {
void registerBeanDefinitions(AnnotationAttributes annoAttrs, BeanDefinitionRegistry registry, String beanName) {
//注入了MapperScannerConfigurer
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
//-------
}
}