一. MybatisProperties

在使用 mybatis 时, 还需要对mapper进行配置:

mybatis:
mapper-locations: classpath:mapper/**Mapper.xml

这些配置其实是映射到 mybatis-spring-boot-autoconfigure 包下的  MybatisProperties 文件中的.

@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties { public static final String MYBATIS_PREFIX = "mybatis"; /**
* Location of MyBatis xml config file.
*/
private String configLocation; /**
* Locations of MyBatis mapper files.
*/
private String[] mapperLocations; /**
* Packages to search type aliases. (Package delimiters are ",; \t\n")
*/
private String typeAliasesPackage; /**
* Packages to search for type handlers. (Package delimiters are ",; \t\n")
*/
private String typeHandlersPackage; /**
* Indicates whether perform presence check of the MyBatis xml config file.
*/
private boolean checkConfigLocation = false; /**
* Execution mode for {@link org.mybatis.spring.SqlSessionTemplate}.
*/
private ExecutorType executorType; /**
* Externalized properties for MyBatis configuration.
*/
private Properties configurationProperties; /**
* A Configuration object for customize default settings. If {@link #configLocation}
* is specified, this property is not used.
*/
@NestedConfigurationProperty
private Configuration configuration;
......
}

二. MybatisAutoConfiguration

MybatisAutoConfiguration 是 mybatis-spring-boot-autoconfigure 中的一个配置类.

在这个配置类中, 主要加入了两个组件: SqlSessionFactory 和 SqlSessionTemplate

1. SqlSessionFactory

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

(从名字可以看出来, SqlSessionFactory 是 SqlSession的一个工厂类. SqlSession 这个类很重要, 里面定义了 数据库的一套增删改查操作的规范.)

MybatisProperties 中的配置会在这个方法中进行解析.

1. 创建了 Configuration 配置类(后面会用到, 这个类也很重要)的实例

2. 通过 factory.getObject() 方法创建了 SqlSessionFactory 类的实例

创建的过程中, 又调用了 protected SqlSessionFactory buildSqlSessionFactory() 方法进行创建, 创建过程比较多, 留给后面在解析.

2. SqlSessionTemplate

  @Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
ExecutorType executorType = this.properties.getExecutorType();
if (executorType != null) {
return new SqlSessionTemplate(sqlSessionFactory, executorType);
} else {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

SqlSessionTemplate 这个类 实现了 SqlSession 接口,  也就是说, 他也有一套 数据库的 增删改查的方法. 先看一下构造过程:

  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
} |
|
\|/ public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
this(sqlSessionFactory, executorType,
new MyBatisExceptionTranslator(
sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true));
} |
|
\|/ public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
PersistenceExceptionTranslator exceptionTranslator) { notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
notNull(executorType, "Property 'executorType' is required"); this.sqlSessionFactory = sqlSessionFactory;
this.executorType = executorType;
this.exceptionTranslator = exceptionTranslator;
this.sqlSessionProxy = (SqlSession) newProxyInstance(
SqlSessionFactory.class.getClassLoader(),
new Class[] { SqlSession.class },
new SqlSessionInterceptor());
}

这里初始化了一个很重要的属性:  sqlSessionProxy. 

sqlSessionProxy 是为 SqlSession 创建了一个代理类, 且代理方法为:  SqlSessionInterceptor

看一个 实现了 SqlSession 接口中的  selectOne 方法:

  @Override
public <T> T selectOne(String statement, Object parameter) {
return this.sqlSessionProxy.<T> selectOne(statement, parameter);
}

可以看到, 在调用 selectOne 方法的时候, 其实就是调用了 SqlSessionInterceptor 的 invoke 方法.

最新文章

  1. 基于jQuery的Validate表单验证
  2. NSUserDefaults:熟悉与陌生(转)
  3. ElasticSearch 2 (10) - 在ElasticSearch之下(深入理解Shard和Lucene Index)
  4. Qt调用dll中的功能函数
  5. ACM2054_A=B
  6. 数据结构(线段树):BZOJ 1018: [SHOI2008]堵塞的交通traffic
  7. #include&lt;math.h&gt;
  8. android上下文
  9. Autofac 入门
  10. C 语言学习准备
  11. JAVA基础知识总结:七
  12. 影响JavaScript应用可扩展性因素
  13. pytest七:assert断言
  14. tf中计算图 执行流程学习【转载】
  15. Codeforces 888G Xor-MST - 分治 - 贪心 - Trie
  16. VMWare Workstation 11的安装
  17. IntelliJ IDEA 优化总结
  18. Slitaz 中文定制手册
  19. IO流(4)重命名
  20. Halcon中缩放Region或XLD的方法研究

热门文章

  1. 字符串匹配算法--暴力匹配(Brute-Force-Match)C语言实现
  2. oracle 处理锁表,创建新的数据库实例
  3. Spring核心知识
  4. POJ 1330(LCA/倍增法模板)
  5. 140. 单词拆分 II
  6. Jquery实现点击当前radio button设置选中属性,其它设置非选中属性
  7. iframe宽高自适应
  8. C++-hihoCode-1105/1109-[STL][堆][prime]
  9. k8s集群问题记录
  10. beego登录退出与检查登录过滤器