从之前的《Mybatis解析——SqlSessionFactoryBean》可以看出,Configuration在SqlSessionFactory的创建过程中是一个很重要的环节。
在SqlSessionFactoryBean.class可以看到Configuration的创建方式,在未配置configurationProperties的情况下,这里主要的参数是configLocation.getInputStream(),就是mybatis配置文件的路径:即mybatis-config.xml
mybatis-config.xml如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="callSettersOnNulls" value="true"/> <!--<setting name="logImpl" value="STDOUT_LOGGING" />--> <setting name="defaultStatementTimeout" value="40"/> </settings> <typeAliases> <typeAlias alias="PYunYingECPMRecord" type="me.ele.vas.cpt.server.model.PYunYingECPMDto"/> </typeAliases> </configuration>
xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), (String)null, this.configurationProperties); configuration = xmlConfigBuilder.getConfiguration();
XMLConfigBuilder中,通过super()初始化来一个Configuration实例。
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { super(new Configuration()); this.localReflectorFactory = new DefaultReflectorFactory(); ErrorContext.instance().resource("SQL Mapper Configuration"); this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = parser; }
mybatis-config.xml文件解析
xmlConfigBuilder.parse();
private void parseConfiguration(XNode root) { try { this.propertiesElement(root.evalNode("properties")); Properties settings = this.settingsAsProperties(root.evalNode("settings")); this.loadCustomVfs(settings); this.typeAliasesElement(root.evalNode("typeAliases")); this.pluginElement(root.evalNode("plugins")); this.objectFactoryElement(root.evalNode("objectFactory")); this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); this.reflectorFactoryElement(root.evalNode("reflectorFactory")); this.settingsElement(settings); this.environmentsElement(root.evalNode("environments")); this.databaseIdProviderElement(root.evalNode("databaseIdProvider")); this.typeHandlerElement(root.evalNode("typeHandlers")); this.mapperElement(root.evalNode("mappers")); } catch (Exception var3) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3); } }
XNode封装了Node,XPathParser是xml文件的解析工具类