一、了解mybatis-plus-boot-starter
mybatisplus官方文档:https://baomidou.com/pages/24112f/
源码下载:https://gitee.com/baomidou/mybatis-plus
mybatisplus架构图
使用maven依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisPlus}</version>
</dependency>
二、mybatisplus是如何启动的呢
1我们平时使用依赖的是spring-boot-starter,它依赖了mybatis-plus-spring-boot-autoconfigure,而他的build.gradle可以看到spring-cloud-commons的依赖配置.
2所以spring启动时候加载mybatisplus的过程在mybatis-plus-spring-boot-autoconfigure中的com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration中有@Configuration
的配置
3在mybatis启动中
在mybatis启动中我们关注1)配置读取,2)SqlSessionFactory的生成3)获取SqlSession
4在mybatisplus中把数据源DataSource生成交给了spring处理,也方便了后面dynamic-datasource-spring-boot-starter做多数据源.
5mybatisplus写了com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean
代替了new SglSessionFactoryBuilder().build( Resources.getResourceAsStream(“config/mybaitsConfig.xml”))
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
//1.设置数据源,原来mybatis是在configuration中的
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
//2.加载配置优先spring配置,如果没有则用mybatis-configuration.xml(properties是从yaml中加载过来的)
if (StringUtils.hasText(this.properties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
//3.设置MybatisSqlSessionFactoryBean的Configuration变量
applyConfiguration(factory);
//设置mybatis的配置
if (this.properties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(this.properties.getConfigurationProperties());
}
//设置拦截器,有spring管理拦截器的生成
if (!ObjectUtils.isEmpty(this.interceptors)) {
factory.setPlugins(this.interceptors);
}
if (this.databaseIdProvider != null) {
factory.setDatabaseIdProvider(this.databaseIdProvider);
}
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
}
if (this.properties.getTypeAliasesSuperType() != null) {
factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
}
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.typeHandlers)) {
factory.setTypeHandlers(this.typeHandlers);
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
factory.setMapperLocations(this.properties.resolveMapperLocations());
}
this.getBeanThen(TransactionFactory.class, factory::setTransactionFactory);
Class<? extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultScriptingLanguageDriver();
if (!ObjectUtils.isEmpty(this.languageDrivers)) {
factory.setScriptingLanguageDrivers(this.languageDrivers);
}
Optional.ofNullable(defaultLanguageDriver).ifPresent(factory::setDefaultScriptingLanguageDriver);
applySqlSessionFactoryBeanCustomizers(factory);
GlobalConfig globalConfig = this.properties.getGlobalConfig();
// 注入填充器
this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
//
this.getBeanThen(AnnotationHandler.class, globalConfig::setAnnotationHandler);
this.getBeanThen(PostInitTableInfoHandler.class, globalConfig::setPostInitTableInfoHandler);
//注入主键生成器
this.getBeansThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerators(i));
//注入sql注入器
this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
//注入ID生成器
this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
//设置进入MybatisSqlSessionFactoryBean
factory.setGlobalConfig(globalConfig);
//生成SqlSessionFactory,重点看buildSqlSessionFactory()
return factory.getObject();
}
进入buildSqlSessionFactory()查看生成SqlSessionFactory的过程