mybatis+springboot定制化

转载自: https://blog.csdn.net/qq_16032831/article/details/72835419  

最近看看了下springboot的自动配置源代码,然后看了一下自己的配置文件,感觉配置配的估计要把springboot的开发者给气死了,要知道springboot的强大之处就在于自动配置。一定要从以前的xml配置的思想转变过来。网上大部分的配置mybatis的插件的就是覆盖SqlSessionFactory配置,更有甚者重写了n多配置,一般都不推荐覆盖框架自己的实现。看了下SqlSessionFactory的自动配置的代码,发现很大问题。
这是我以前的覆盖方式:

@Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(MybatisProperties properties) {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(druidSource);
        if (properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(properties.getConfigurationProperties());
        }
        if (StringUtils.hasLength(properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(properties.getTypeAliasesPackage());
        }
        if (StringUtils.hasLength(properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(properties.getTypeHandlersPackage());
        }
        // 获取资源文件
        if (!ObjectUtils.isEmpty(properties.resolveMapperLocations())) {
            factory.setMapperLocations(properties.resolveMapperLocations());
        }
        // 分页插件
        factory.setPlugins(new Interceptor[] { getInterceptor() });
        try {
            // 设置sql文件路径
            return factory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
--------------------- 
作者:ZhaoYM_jz 
来源:CSDN 
原文:https://blog.csdn.net/qq_16032831/article/details/72835419 
版权声明:本文为博主原创文章,转载请附上博文链接!

看看源码怎么配置的

@Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
      factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    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);
    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());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
      factory.setMapperLocations(this.properties.resolveMapperLocations());
    }

    return factory.getObject();
  }
--------------------- 
作者:ZhaoYM_jz 
来源:CSDN 
原文:https://blog.csdn.net/qq_16032831/article/details/72835419 
版权声明:本文为博主原创文章,转载请附上博文链接!

看看丢了好多配置。。。基本你要加一个配置就要改一下你的配置类,这样做。。。,好了进入正题。看了n久想想这个拦截器到底改怎么配。看了下传统的xml,本来想在SqlSessionFactoryBean去注入拦截器,但看了下它的实现就放弃了这个想法,因为它是new 的一个SqlSessionFactoryBean,所以上下文中根本就没有SqlSessionFactoryBean。后来看了下MybatisAutoConfiguration的构造方法发现了问题,

public MybatisAutoConfiguration(MybatisProperties properties,
                                  ObjectProvider<Interceptor[]> interceptorsProvider,
                                  ResourceLoader resourceLoader,
                                  ObjectProvider<DatabaseIdProvider> databaseIdProvider,
                                  ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
    this.properties = properties;
    this.interceptors = interceptorsProvider.getIfAvailable();
    this.resourceLoader = resourceLoader;
    this.databaseIdProvider = databaseIdProvider.getIfAvailable();
    this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
  }
--------------------- 
作者:ZhaoYM_jz 
来源:CSDN 
原文:https://blog.csdn.net/qq_16032831/article/details/72835419 
版权声明:本文为博主原创文章,转载请附上博文链接!

所以 画龙点睛之笔就在于 springboot 的autoConfiguration 当在的这段代码:

  

this.interceptors = interceptorsProvider.getIfAvailable();

this.interceptors = interceptorsProvider.getIfAvailable(); 这个方法意思很明确就是试图去寻找实现了这个接口的类。所以问题就解决了。
@Bean
public Interceptor getInterceptor(){
}

posted @ 2018-11-21 11:50  白云是世界的公民  阅读(285)  评论(0)    收藏  举报