mybatis - MybatisAutoConfiguration
一. 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 方法.
mybatis - MybatisAutoConfiguration的更多相关文章
- Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:
Parameter 0 of method orderSqlSessionFactory in com.config.MultipleDBConfig required a single bean, ...
- MyBatis good
命名解析:为了减少输入量,MyBatis 对所有的命名配置元素(包括语句,结果映射,缓存等)使用了如下的命名解析规则. 完全限定名(比如“com.mypackage.MyMapper.selectAl ...
- (转) Spring Boot MyBatis 连接数据库
最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...
- SpringBoot与Mybatis整合方式01(源码分析)
前言:入职新公司,SpringBoot和Mybatis都被封装了一次,光用而不知道原理实在受不了,于是开始恶补源码,由于刚开始比较浅,存属娱乐,大神勿喷. 就如网上的流传的SpringBoot与Myb ...
- 2018-01-08 学习随笔 SpirngBoot整合Mybatis进行主从数据库的动态切换,以及一些数据库层面和分布式事物的解决方案
先大概介绍一下主从数据库是什么?其实就是两个或N个数据库,一个或几个主负责写(当然也可以读),另一个或几个从只负责读.从数据库要记录主数据库的具体url以及BigLOG(二进制日志文件)的参数.原理就 ...
- 4. 整合MyBatis
mybatis既有jdbc的灵活,有具有orm工具的方便,是一套很好用的工具,这儿就使用mybatis来作为数据访问工具,具体添加过程如下: 1. 添加mybatis依赖,并更新项目 <depe ...
- 最简单的SpringBoot整合MyBatis教程
前面两篇文章和读者聊了Spring Boot中最简单的数据持久化方案JdbcTemplate,JdbcTemplate虽然简单,但是用的并不多,因为它没有MyBatis方便,在Spring+Sprin ...
- springboot集成下,mybatis的mapper代理对象究竟是如何生成的
前言 开心一刻 中韩两学生辩论. 中:端午节是属于谁的? 韩:韩国人! 中:汉字是谁发明的? 韩:韩国人! 中:中医是属于谁的? 韩:韩国人! 中:那中国人到底发明过什么? 韩:韩国人! 前情回顾 M ...
- springboot集成mybatis源码分析-启动加载mybatis过程(二)
1.springboot项目最核心的就是自动加载配置,该功能则依赖的是一个注解@SpringBootApplication中的@EnableAutoConfiguration 2.EnableAuto ...
随机推荐
- LED Decorative Light Supplier - LED Neon Application: 5 Advantages
In the past 100 years, lighting has gone a long way. LED decorative lighting is now designed to meet ...
- Java必须知道的知识点
junit用法,before,beforeClass,after, afterClass的执行顺序 分布式锁 nginx的请求转发算法,如何配置根据权重转发 用hashmap实现redis有什么问题( ...
- The number of set(位运算+状态dp)一道十分美妙的题目哦!!!!!!
Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use the ...
- 在多租户(容器)数据库中如何创建PDB:方法2 克隆本地PDB
基于版本:19c (12.2.0.3) AskScuti 创建方法:克隆本地PDB(从本地其他PDB创建新的PDB) 对应路径:Creating a PDB --> Cloning --> ...
- C++ 中字符串查找、字符串截取、字符串替换
参照:C++基础-string截取.替换.查找子串函数 1.字符串查找 s.find(s1) //查找s中第一次出现s1的位置,并返回(包括0) s.rfind(s1) //查找s中最后次出现s1的位 ...
- 在Unity3d中使用Google.ProtocolBuffers
通过Nuget下载Google.ProtocolBuffers,在目录中找到net35下的文件,放入unity3d中作为插件 PersonMessage.Builder personBuilder = ...
- 记manjaro图形驱动删除后的一次补救
#一.前言 众所周知,NVIDIA的闭源驱动在Linux上的兼容性不是很好,再加上我不玩游戏,于是我就想卸载独显只留核显.我以为我装了独显和核显两种驱动,原本想直接删除独显驱动,没想到删除的是bumb ...
- python UI自动化之切换iframe
python+selenium写UI自动化的时候,经常会遇到需要切换iframe的情况,这里介绍几种切换iframe的方式 1.使用id定位 driver.switch_to.frame(" ...
- php设计模式之观察者模式实例代码
php提供的两个接口,一个被观察者接口SplSubject,一个或多个观察者接口SPLObserver,和一个可以储存对象的类SplObjectStorage.被观察者有三个方法,需要实现这三个方法, ...
- MarkDown的黄金搭档Typora编辑器
让你成为热爱写作的程序员 学习编程的小伙伴,要养成记笔记的好习惯,并发布到博客上去与同行分享你的学习经验,那么传统的文本编辑器或多或少会不尽人意,效率低,而且码字体验与写代码完全不一样. 下面推荐一款 ...