springboot之mybatis整合配置源碼過程分析
源碼分析
1.添加依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2.源碼
查看mybatis的配置MybatisAutoConfiguration源碼,部分源碼如下:
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
//給創(chuàng)建的工廠設置數(shù)據(jù)源
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
//判斷properties對象的屬性ConfigLocation值是否存在
//查看properties是MybatisProperties類的對象
/**
//根據(jù)上面的代碼,以及下面的代碼片段,可以看出來configLocation是設置MyBatis全局配置文件的路徑
//從下面代碼可以看出yml中設置的key值為:mybatis.config-location
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {
public static final String MYBATIS_PREFIX = "mybatis";
//Location of MyBatis xml config file.
private String configLocation;
}
*/
//此處標記A
if (StringUtils.hasText(this.properties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
//也是從MybatisProperties類的對象properties中獲取configuration配置的值
//如果yml中沒有配置mybatis.configuration值,則下面configuration獲取為null
//此處標記B
Configuration configuration = this.properties.getConfiguration();
//如果yml中沒有配置configuration的值,并且configLocation也未配置時,創(chuàng)建一個空的configuration對象
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
configuration = new Configuration();
}
//由于上一步創(chuàng)建了一個空的configuration對象,則configuration != null
//configurationCustomizers這個List<ConfigurationCustomizer>是否不為空
//滿足上面條件,則執(zhí)行下面方法
/**
public interface ConfigurationCustomizer {
//Customize the given a {@link Configuration} object.
//@param configuration the configuration object to customize
void customize(Configuration configuration);
}
*/
//此處標記C
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration);
}
}
//給工廠設置配置信息
factory.setConfiguration(configuration);
....................
}
3.實例
配置駝峰命名映射關系的方法
先找到控制駝峰命名的屬性:
由下面代碼可以看出:
控制駝峰命名的屬性是在MybatisProperties類下的configuration對象中的mapUnderscoreToCamelCase屬性
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {
public static final String MYBATIS_PREFIX = "mybatis";
/**
* A Configuration object for customize default settings. If {@link #configLocation}
* is specified, this property is not used.
*/
@NestedConfigurationProperty
private Configuration configuration;
.........
}
public class Configuration {
protected Environment environment;
protected boolean safeRowBoundsEnabled;
protected boolean safeResultHandlerEnabled = true;
//控制是否開啟駝峰命名
protected boolean mapUnderscoreToCamelCase;
..........
}
方法一:
從上面MybatisAutoConfiguration源碼片段分析:
標記A處如果在yml中設置了mybatis.config-location值,直接從mybatis的全局配置文件中取
mybatis: config-location: classpath:mybatis-config.xml
以及根據(jù)上面找到的控制駝峰命名的屬性位置,則可在mybatis全局配置文件進行如下賦值
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>
</configuration>
方法二:
標記A處未設置mybatis.config-location值,
則標記B處直接從MybatisProperties類下的configuration對象獲取配置信息,這也就是直接通過yml配置
mybatis:
configuration:
mapUnderscoreToCamelCase: true
方法三:
上面兩種方法較為常見,最后一種方法:
當沒有設置mybatis.config-location,也沒有在yml中配置mybatis.configuration時,我們可以看出,
在標記B處,會走進if方法,創(chuàng)建一個空的configuration對象
而在標記C處,只要保證集合List不為空,也是能實現(xiàn)configuration的設值
從源碼看出,ConfigurationCustomizer為一個接口,其customize方法的入?yún)⑹莄onfiguration
那我們可以自己實現(xiàn)其方法,對configuration進行賦值
@Component
public class MyConfigurationCustomizer implements ConfigurationCustomizer {
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
}
注意:實現(xiàn)其方法后,要將實現(xiàn)類注入到IOC容器中,這樣spring才會將這些對象放入到List
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java try catch finally異常處理組合詳解
這篇文章主要介紹了Java try catch finally異常處理組合詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05
spring cloud alibaba Nacos 注冊中心搭建過程詳解
這篇文章主要介紹了spring cloud alibaba Nacos 注冊中心搭建過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10
Java中List.contains(Object?object)方法使用
本文主要介紹了Java中List.contains(Object?object)方法,使用List.contains(Object?object)方法判斷ArrayList是否包含一個元素對象,感興趣的可以了解一下2022-04-04
Mybatis-plus foreach拼接字符串查詢無數(shù)據(jù)返回問題
這篇文章主要介紹了Mybatis-plus foreach拼接字符串查詢無數(shù)據(jù)返回問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

