环境准备
- mybatis 3.1.0
SqlSessionFactory
- sqlSessionFactory是Mybatis的核心类之一,最重要的功能就是创建SqlSession.SqlSessionFactory应该是单例的,如果采用多例则它对数据库连接的消耗是很大的,所以每一个数据库只对应一个SqlSessionFactory,避免过多的Connection被消耗
- SqlSessionFactoryBuilder构建SqlSessionFactory
- 通过XMLConfigBuilder解析xml文件读取配置参数,并存入Configuration类中(mybatis几乎所有配置都存放在这里)
- 使用Configuration对象去创建SqlSessionFactory(默认实现是DefaultSqlSessionFactory)
- SqlSessionFactoryBuilder作用就是构建器,一旦我们创建了SqlSessionFactory,生命周期就结束了,此时应该回收。所以它的生命周期只存在于方法的局部
SqlSessionFactoryBuilder
- 源码分析
public class SqlSessionFactoryBuilder {
public SqlSessionFactory build(Reader reader) {
return build(reader, null, null);
}
public SqlSessionFactory build(Reader reader, String environment) {
return build(reader, environment, null);
}
public SqlSessionFactory build(Reader reader, Properties properties) {
return build(reader, null, properties);
}
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException e) {
}
}
}
public SqlSessionFactory build(InputStream inputStream) {
return build(inputStream, null, null);
}
public SqlSessionFactory build(InputStream inputStream, String environment) {
return build(inputStream, environment, null);
}
public SqlSessionFactory build(InputStream inputStream, Properties properties) {
return build(inputStream, null, properties);
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
}
}
}
/**
这里是写死的,创建的是DefaultSqlSessionFactory
***/
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
}
XMLConfigBuilder
- 源码分析
/***
`configuration`为根节点
在configuration节点之下,我们可以配置10个子节点, 分别为:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。
***/
public Configuration parse() {
if (parsed) {
throw new BuilderException("Each MapperConfigParser can only be used once.");
}
parsed = true;
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
private void parseConfiguration(XNode root) {
try {
propertiesElement(root.evalNode("properties"));
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
settingsElement(root.evalNode("settings"));
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
Configuration
- 作用
- 读入配置文件,基础配置文件比如Configuration.xml和bean的映射mapper文件
- 初始化基础配置,比如别名,插件,映射器,ObjectFactory等