spring-MybatisAutoConfiguration源码解读

mybatis配置的时候需要注意一下几点:

  1. 当yml中配置了config-location的时候,java代码配置的配置将不再生效
  2. 当mybatis.config-location=classpath:config/config.xml
    mybatis.configuration.map-underscore-to-camel-case=true
    config-location和configuration.**的配置不能共存

换而言之, yml、xml、java都能进行配置
yml、xml的优先级高于java代码 ==== 符合了spring的设计,配置大于编码
yml和xml不能共存

ConfigurationCustomizer.class
public interface ConfigurationCustomizer {
    void customize(Configuration var1);
}

MybatisAutoConfiguration.class
@Configuration//配置类
//SqlSessionFactory、SqlSessionFactoryBean存在的时候配置生效
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
//DataSource类存在时生效
@ConditionalOnBean({DataSource.class})
//启用配置属性MybatisProperties类
@EnableConfigurationProperties({MybatisProperties.class})
//在DataSourceAutoConfiguration配置之后配置
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class MybatisAutoConfiguration {
    //日志
    private static final Logger logger = LoggerFactory.getLogger(MybatisAutoConfiguration.class);
//配置类
    private final MybatisProperties properties;
    private final Interceptor[] interceptors;
//资源加载器
    private final ResourceLoader resourceLoader;
    private final DatabaseIdProvider databaseIdProvider;
//重点:放置接口进行配置的list
    private final List<ConfigurationCustomizer> configurationCustomizers;

    public MybatisAutoConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
        this.properties = properties;
        this.interceptors = (Interceptor[])interceptorsProvider.getIfAvailable();
        this.resourceLoader = resourceLoader;
        this.databaseIdProvider = (DatabaseIdProvider)databaseIdProvider.getIfAvailable();
//拿到当前工程下边所有申明@bean的ConfigurationCustomizer的实例
        this.configurationCustomizers = (List)configurationCustomizersProvider.getIfAvailable();
    }
//构造行数执行之后进行检查
    @PostConstruct
    public void checkConfigFileExists() {
        if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
            Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
            Assert.state(resource.exists(), "Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)");
        }

    }

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
//获取mybatis.config-location=classpath:config/config.xml配置
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }
//此处永远为空
        org.apache.ibatis.session.Configuration configuration = this.properties.getConfiguration();
//在yml不存在config-location属性是创建configuration 
        if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
            configuration = new org.apache.ibatis.session.Configuration();
        }
//生效java代码的配置
        if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
            Iterator var4 = this.configurationCustomizers.iterator();

            while(var4.hasNext()) {
//迭代拿到申明@bean接口的实例
                ConfigurationCustomizer customizer = (ConfigurationCustomizer)var4.next();
                customizer.customize(configuration);
            }
        }
        factory.setConfiguration(configuration);
        if (this.properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
        }

        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 (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
//设置mybatis的mapper xml
        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            factory.setMapperLocations(this.properties.resolveMapperLocations());
        }
        return factory.getObject();
    }
}


MybatisProperties .class
@ConfigurationProperties(
    prefix = "mybatis"
)
//一堆get set方法
public class MybatisProperties {
    public static final String MYBATIS_PREFIX = "mybatis";
    private String configLocation;
    private String[] mapperLocations;
    private String typeAliasesPackage;
    private String typeHandlersPackage;
    private boolean checkConfigLocation = false;
    private ExecutorType executorType;
    private Properties configurationProperties;
    @NestedConfigurationProperty//这个注解相当于把Configuration类里的属性 嵌套进来了
    private Configuration configuration;
}

    原文作者:peipeicn
    原文地址: https://www.jianshu.com/p/104bdfcecb9f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞