最近看看了下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);

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

看看源码怎么配置的

@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();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

看看丢了好多配置。。。基本你要加一个配置就要改一下你的配置类,这样做。。。,好了进入正题。看了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();

}

1

2

3

4

5

6

7

8

9

10

11

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

@Bean

public Interceptor getInterceptor(){

}

springboot零侵入配置mybatis插件