在 <MyBatis框架中Mapper映射配置的使用及原理解析(一) 配置与使用> 的demo中看到了SessionFactory的创建过程:
SqlSessionFactory sessionFactory = null;
String resource = "mybatisConfig.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
e.printStackTrace();
}
那么我们就从SqlSessionFactoryBuilder开始,看看Mybatis的加载过程。
SqlSessionFactoryBuilder的核心源码:
package org.apache.ibatis.session;
public class SqlSessionFactoryBuilder {
//通过Reader读取Mybatis配置
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try { XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); return build(parser.parse()); //parse()方法得到Configuration } catch (Exception e) { throw ExceptionFactory.wrapException("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { reader.close(); } catch (IOException e) { // Intentionally ignore. Prefer previous error. } } }
//通过InputStream读取Mybatis配置 public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) { try { XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties); return build(parser.parse()); //parse()方法得到Configuration } catch (Exception e) { throw ExceptionFactory.wrapException("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { inputStream.close(); } catch (IOException e) { // Intentionally ignore. Prefer previous error. } } }
//以上2个方法最终调用的是build(Configuration config) public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); } }
通过源码,我们可以看到SqlSessionFactoryBuilder 通过XMLConfigBuilder 去解析我们传入的mybatis的配置文件,构造出Configuration,最终返回new DefaultSqlSessionFactory(config)的SqlSessionFactory实例。
接下来我们看看 XMLConfigBuilder 是怎样解析Mybatis的配置文件的,下面是部分源码:
package org.apache.ibatis.builder.xml; /** * 解析Mybatis配置文件 */ public class XMLConfigBuilder extends BaseBuilder { private boolean parsed; private XPathParser parser; private String environment; public XMLConfigBuilder(Reader reader) { this(reader, null, null); } public XMLConfigBuilder(Reader reader, String environment) { this(reader, environment, null); } public XMLConfigBuilder(Reader reader, String environment, Properties props) { this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props); } public XMLConfigBuilder(InputStream inputStream) { this(inputStream, null, null); } public XMLConfigBuilder(InputStream inputStream, String environment) { this(inputStream, environment, null); } public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) { this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props); } private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { super(new Configuration()); ErrorContext.instance().resource("SQL Mapper Configuration"); this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = parser; } //调用此方法对mybatis配置文件进行解析,返回Configuration对象 public Configuration parse() { if (parsed) { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } parsed = true; //从根节点configuration,开始解析 parseConfiguration(parser.evalNode("/configuration")); return configuration; } //解析configuration节点下的10个子节点。 private void parseConfiguration(XNode root) { try {
//解析子节点properties propertiesElement(root.evalNode("properties")); //issue #117 read properties first
//解析子节点typeAliases 别名 typeAliasesElement(root.evalNode("typeAliases"));
//解析子节点plugins 插件 pluginElement(root.evalNode("plugins"));
//解析子节点objectFactory mybatis为结果创建对象时都会用到objectFactory objectFactoryElement(root.evalNode("objectFactory"));
//解析子节点objectWrapperFactory objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析settings定义一些全局性的配置 settingsElement(root.evalNode("settings"));
//解析environments 可以配置多个运行环境,但是每个SqlSessionFactory 实例只能选择一个运行环境 environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
//解析databaseIdProvider MyBatis能够执行不同的语句取决于你提供的数据库供应商。许多数据库供应商的支持是基于databaseId映射 databaseIdProviderElement(root.evalNode("databaseIdProvider"));
//解析typeHandlers 当MyBatis设置参数到PreparedStatement 或者从ResultSet 结果集中取得值时,就会使用TypeHandler 来处理数据库类型与java 类型之间转换 typeHandlerElement(root.evalNode("typeHandlers"));
//解析mappers 主要的crud操作都是在mappers中定义的 mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } } }
从上面可以看出可以配置10个子节点, 分别为:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。