Spring mybatis源码篇章-SqlSessionFactoryBean
通过实例结合源码的方式解读,其中涉及到的文件来自于笔者的Github毕设项目,引用的jar包为
mybatis-spring-1.3.0.jar
Mybatis
Mybatis是基于ORM(Object relation mapping)思想而开发的框架插件,本质原理用一句笔者的话便是使用了JAVA连接数据库的方式来执行相应的SQL(PreparedStatement),并在此基础上提供了丰富的动态SQL配置以及缓存等概念。(后续文章会提及)
其基本上是企业与传统数据库(Oracle/Mysql等)交互常用的数据库持久层框架,以JAVA语言来说,其与Spring的搭配使用也是广为流传。
本文将从源码的角度浅析Mybatis与Spring的亲密搭配
范例
笔者本文只关注SqlSessionFactoryBean的配置,样例如下
<!--数据源,引用common-pool包的数据源类-->
<bean id="dataSource-mysql" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="1" />
<property name="maxActive" value="20" />
</bean>
<!--sqlSessionFactoryBean创建-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource-mysql" />
<property name="typeAliasesPackage" value="com.du.wx.model" />
<property name="mapperLocations" value="classpath:com/du/wx/resources/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- joggle代表接口 -->
<property name="basePackage" value="com.du.wx.mapper.joggle" />
</bean>
由上述的配置可知,在配置SqlSessionFactory数据库会话工厂Bean对象时必须依赖相应的数据源属性dataSource
SqlSessionFactoryBean
笔者分层次来解析此类
基础内部属性
优先观察SqlSessionFactoryBean的内部属性,代码片段如下
private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class);
private Resource configLocation;
private Configuration configuration;
private Resource[] mapperLocations;
private DataSource dataSource;
private TransactionFactory transactionFactory;
private Properties configurationProperties;
private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
private SqlSessionFactory sqlSessionFactory;
//EnvironmentAware requires spring 3.1
private String environment = SqlSessionFactoryBean.class.getSimpleName();
private boolean failFast;
private Interceptor[] plugins;
private TypeHandler<?>[] typeHandlers;
private String typeHandlersPackage;
private Class<?>[] typeAliases;
private String typeAliasesPackage;
private Class<?> typeAliasesSuperType;
//issue #19. No default provider.
private DatabaseIdProvider databaseIdProvider;
private Class<? extends VFS> vfs;
private Cache cache;
private ObjectFactory objectFactory;
private ObjectWrapperFactory objectWrapperFactory;
常用的属性如下:
configLocation mybatis主配置文件路径,支持classpath语法
mapperLocations 指定mybatis的mapper配置文件,支持classpath语法
dataSource 数据源
typeAliasesPackage 指定model层类名的别名扫描包,这与mapper配置中的parameterType和resultType搭配使用
InitializingBean复写方法
紧接着看下其复写的afterPropertiesSet()方法,代码如下
@Override
public void afterPropertiesSet() throws Exception {
//datasource not allowed to be null.
notNull(dataSource, "Property 'dataSource' is required");
notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
"Property 'configuration' and 'configLocation' can not specified with together");
this.sqlSessionFactory = buildSqlSessionFactory();
}
与前文提及的对应,必须确保dataSource属性已经得到配置。
实例化步骤
最后查看关键实例方法buildSqlSessionFactory()方法,由于片段过长,笔者截取几段来进行讲解
Mybatis主文件加载
Configuration configuration;
XMLConfigBuilder xmlConfigBuilder = null;
if (this.configuration != null) {
configuration = this.configuration;
if (configuration.getVariables() == null) {
configuration.setVariables(this.configurationProperties);
} else if (this.configurationProperties != null) {
configuration.getVariables().putAll(this.configurationProperties);
}
} else if (this.configLocation != null) {
//如果spring配置中configLocation属性不为空,则加载指定的Mybatis配置
xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();
} else {
//否则则采用默认的Mybatis配置,@see Configuration
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Property `configuration` or 'configLocation' not specified, using default MyBatis Configuration");
}
configuration = new Configuration();
configuration.setVariables(this.configurationProperties);
}
通过上述配置表明,内部属性configLocation是非必须配置项
model别名映射扫描
if (hasLength(this.typeAliasesPackage)) {
//截取typeAliasesPackage属性的包,支持,或;
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray) {
//对应方法的目的是通过扫描包得到其包以及子包目录下的所有Class,然后为每个class注册别名
configuration.getTypeAliasRegistry().registerAliases(packageToScan,
typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
}
}
}
即对内部属性typeAliasesPackage支持,;作为分隔符以加载多个目录,主要是为了方便编程人员使用parameterType/resultType等属性
mapper配置文件加载
if (!isEmpty(this.mapperLocations)) {
//mapperLocations
for (Resource mapperLocation : this.mapperLocations) {
if (mapperLocation == null) {
continue;
}
try {
//对扫描的每个配置文件进行解析,并保存其中的必要参数,后续会分析
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
configuration, mapperLocation.toString(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
} catch (Exception e) {
throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
} finally {
ErrorContext.instance().reset();
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
}
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
}
}
关键处理XML配置的mapper文件,用到了mybatis包中的org.apache.ibatis.builder.xml.XMLMapperBuilder类进行解析。这个后续分析
Configuration-所有关于Mybatis的信息均存储在Mybatis包中的org.apache.ibatis.session.Configuration类上
return this.sqlSessionFactoryBuilder.build(configuration);
Spring呢,就通过SqlSessionFactoryBuilder类来创建相应的SqlSessionFactory对象。具体的由于篇幅过长,就放在后文分析。
总结
从上文便可以看出来Spring框架是如何整合Mybatis的,就是这个Mybatis的关键类Configuration。而相应的开关数据库等一系列的功能都是通过Spring的SqlSessionFactory对象来操控的。这也是两者结合最核心的地方
Spring mybatis源码篇章-SqlSessionFactoryBean的更多相关文章
- Spring mybatis源码篇章-sql mapper配置文件绑定mapper class类
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-MybatisDAO文件解析(二) 背景知识 MappedStatement是mybatis操作sql ...
- Spring mybatis源码篇章-MybatisDAO文件解析(二)
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-MybatisDAO文件解析(一) 默认加载mybatis主文件方式 XMLConfigBuilder ...
- Spring mybatis源码篇章-MybatisDAO文件解析(一)
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 加载指定的mybatis主文件 Mybatis模板文件,其中的属性 ...
- Spring mybatis源码篇章-XMLLanguageDriver解析sql包装为SqlSource
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-MybatisDAO文件解析(二) 首先了解下sql mapper的动态sql语法 具体的动态sql的 ...
- Spring mybatis源码篇章-MapperScannerConfigurer关联dao接口
前言:Spring针对Mybatis的XML方式的加载MappedStatement,通过引入MapperScannerConfigurer扫描类来关联相应的dao接口以供Service层调用.承接前 ...
- Spring mybatis源码篇章-动态SQL基础语法以及原理
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis的XML文件加载 前话 前文通过Spring中配置mapperLocations属性来进行对m ...
- Spring mybatis源码篇章-Mybatis的XML文件加载
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis主文件加载 前话 前文主要讲解了Mybatis的主文件加载方式,本文则分析不使用主文件加载方式 ...
- Spring mybatis源码篇章-Mybatis主文件加载
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 前话 本文承接前文的内容继续往下扩展,通过Spring与Mybatis的 ...
- Spring mybatis源码篇章-NodeHandler实现类具体解析保存Dynamic sql节点信息
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-XMLLanguageDriver解析sql包装为SqlSource SqlNode接口类 publi ...
随机推荐
- Sqlserver 2005 跨数据库 导入数据
--Sqlserver 2005 跨数据库 导入数据:--SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/O ...
- AI相关 TensorFlow -卷积神经网络 踩坑日记之一
上次写完粗浅的BP算法 介绍 本来应该继续把 卷积神经网络算法写一下的 但是最近一直在踩 TensorFlow的坑.所以就先跳过算法介绍直接来应用场景,原谅我吧. TensorFlow 介绍 TF是g ...
- if else的错误用法!
在学习C语言的时候,我们会了解和认识if else 的用法, 并在程序里面使用它来作为判断条件! if(表达式) //先定义一个条件,如果成立,则循环if里面的语句. { 语句 } el ...
- AppDelegate减负之常用三方封装 - 友盟推送篇
之前分享过集成友盟推送的方法, 需要的朋友可以查看一下链接: http://www.cnblogs.com/zhouxihi/p/6533058.html 一般开发中我们比较多使用的三方有友盟推送, ...
- MOF编译器无法连接VMI服务器。原因可能是语义错误的解决方案
安装数据库时报错. 我这个是因为安装SQL SERVER时,没有卸载vs. 一般解决方法: WIN 7安装VS和SQL SERVER的顺序应该是先安装SQL SERVER 然后安装VS,当要重装SQL ...
- PHP xmapp 下面安装 Composer-Setup.exe
1.打开PHP配置文件E:\xampp\php\php.ini确认以下模块已开启(移除前面的分号). extension=php_openssl.dll, (php.ini文档里面开启一次就OK了) ...
- Android高仿qq及微信底部菜单的几种实现方式
最近项目没那么忙,想着开发app的话,有很多都是重复,既然是重复的,那就没有必要每次都去写,所以就想着写一个app通用的基本框架,这里说的框架不是什么MVC,MVP,MVVM这种,而是app开发的通用 ...
- mongodb远程连接配置
mongodb远程连接配置如下: 1.修改配置文件mongodb.conf 命令:vim /etc/mongodb.conf 把 bind_ip=127.0.0.1 这一行注释掉或者是修改成 bind ...
- 以芯片直读方式得到的全盘镜像解析及ext4日志区域解析
之前在centos中分析了/dev/sda1下的结构,但当对象是一块以芯片直读方式作出来的全盘镜像呢? 这次以安卓手机的全盘镜像为对象,尝试按照ext4文件系统结构手动解析,加强对ext4文件系统.E ...
- 关于MySQL用户会话及连接线程
0.概念理解:用户会话和连接线程是什么关系? 用户会话和用户连接线程是一一对应的关系,一个会话就一个用户连接线程. 问题描述: 如果系统因为执行了一个非常大的dml或者ddl操作导致系统hang住,我 ...