注册
进spring,详见 mybatis与spring的整合之MapperFactoryBean,而
SqlSessionFactoryBean则是
解析
映射接口对应的sql配置文件(xml文件)
在基本的 MyBatis
中,session 工厂
可以使用 SqlSessionFactoryBuilder 来创建。而在 MyBatis-Spring 中
,则使用 SqlSessionFactoryBean 来替代。
SqlSessionFactoryBean引入sql配置
文件有两种形式
t.id,
t.app_name,
t.title,
t.`order`,
t.address_type,
t.address_list
.
.
.
引入
Resource[] mapperLocations
spring内置的属性编辑器
会将mybatis-mapper/*.xml
转换为Resource
数组
/*
*/
引入
mapperLocations
仅仅是sql配置文件,会被解析放入Configuration中
configLocation
可以设置其他东西,比如二级缓存
、实体类别名
、数据源
(DataSource)等,可以配置多个config.xml
实现多数据源
配置。它会被解析为Configuration对象,这是构建SqlSessionFactory所必须的。
MyBatis
的配置文件包含了会深深影响 MyBatis
行为的设置和属性信息,这里可直接看官网:configuration配置
SqlSessionFactoryBean实现了FactoryBean,重写了getObject
接口 ,通过该方法返回SqlSessionFactory
对象
public SqlSessionFactory getObject() throws Exception {
if (this.sqlSessionFactory == null) {
afterPropertiesSet();
}
return this.sqlSessionFactory;
}
SqlSessionFactoryBean实现了InitializingBean接口,重写了afterPropertiesSet
方法
public void afterPropertiesSet() throws Exception {
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
必须手动显示注入
sqlSessionFactoryBuilder
初始化为SqlSessionFactoryBuilder,可不用手动注入
configuration
是bean
,configLocation
是配置文件
,两者不能同时配置
configuration
,一个bean
configLocation
,一个xml
配置文件new
一个configuration
对象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) {
xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
}
configuration = new Configuration();
if (this.configurationProperties != null) {
configuration.setVariables(this.configurationProperties);
}
}
configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));
if (xmlConfigBuilder != null) {
try {
xmlConfigBuilder.parse();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
}
} catch (Exception ex) {
throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
} finally {
ErrorContext.instance().reset();
}
}
如果configLocation
属性值不为空,则xmlConfigBuilder
会通过configLocation
对象构建,这里就会开始解析
从根目录configuration
开始解析,后面就不具体看了
public Configuration parse() {
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
如果mapperLocations
属性值不为空,则在这里开始解析
if (!isEmpty(this.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 + "'");
}
}
}
从根目录mapper
开始解析,后面就不具体看了
public void parse() {
if (!configuration.isResourceLoaded(resource)) {
configurationElement(parser.evalNode("/mapper"));
configuration.addLoadedResource(resource);
bindMapperForNamespace();
}
// 解析ResultMap
parsePendingResultMaps();
// 二级缓存
parsePendingCacheRefs();
// sql语句
parsePendingStatements();
}
SqlSessionFactory可以注入一些其他属性,比如缓存、别名等,有的话都会被设置进configuration
对象中
return this.sqlSessionFactoryBuilder.build(configuration);
SqlSessionFactoryBuilder.build
,构建的是DefaultSqlSessionFactory
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}