The article, " in layman's language Mybatis series (two) --- Mybatis entry " wrote a simple Demo reflect a bit Mybatis process. This will be a brief Mybatis profile:
The last example, we go to SqlSessionFactoryBuilder create SqlSessionFactory, then we start SqlSessionFactoryBuilder start, let's take a look at the source code is how to achieve:
SqlSessionFactoryBuilder source code fragment:
public class SqlSessionFactoryBuilder { // Reader reads mybatis configuration file, passed in the constructor // In addition to Reader, there is also a corresponding inputStream as a constructor parameter // It also reflects the flexibility of configuration mybatis public SqlSessionFactory Build (Reader Reader) { return Build (Reader, null , null ); } public SqlSessionFactory build(Reader reader, String environment) { return build(reader, environment, null); } // mybatis profile + properties, this case may not mybatis profile configuration properties, can also be used in the form of $ {} public a SqlSessionFactory Build (Reader Reader, the Properties Properties) { return Build (Reader, null , Properties); } // By XMLConfigBuilder resolve mybatis configuration, and then create a SqlSessionFactory public SqlSessionFactory Build (Reader Reader, String Environment, the Properties the Properties) { the try { Parser XMLConfigBuilder = new new XMLConfigBuilder (Reader, Environment, Properties); // The following look at the source of this method return Build (parser.parse ()); } 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. } } } public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); } }
By source code, we can see SqlSessionFactoryBuilder to resolve our incoming mybatis profile by XMLConfigBuilder, to then take a look at the following section XMLConfigBuilder Source:
/** * Mybatis configuration file parsing */ public class XMLConfigBuilder extends BaseBuilder { 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; } // external call this method on mybatis configuration file is parsed public the Configuration the parse () { IF (of Parsed) { the throw new new BuilderException ( "Each XMLConfigBuilder only CAN BE Used Once." ); } parsed = true; //从根节点configuration parseConfiguration(parser.evalNode("/configuration")); return configuration; } // This method is to parse the next child node of the node configuration // This also can be seen, the nodes that we can be configured in the following configuration of the nodes 10 Private void parseConfiguration (XNode the root) { the try { propertiesElement(root.evalNode("properties")); //issue #117 read properties first typeAliasesElement(root.evalNode("typeAliases")); pluginElement(root.evalNode("plugins")); objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); settingsElement(root.evalNode("settings")); environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631 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); } } }
Through the above source code, we can see that in mybatis configuration file:
1. configuration node as a root.
2. Under the configuration node, we can configure the 10 sub-node, respectively: properties, typeAliases, plugins, objectFactory, objectWrapperFactory, settings, environments, databaseIdProvider, typeHandlers, mappers.
This article only describes the contents on the first, the next article will turn 10 this analysis to resolve the source node of the more important a few nodes to see when parsing these nodes, in the end done.
/** * mybatis 配置文件解析 */public class XMLConfigBuilder extends BaseBuilder { 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配置文件进行解析 public Configuration parse() { if (parsed) { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } parsed = true; //从根节点configuration parseConfiguration(parser.evalNode("/configuration")); return configuration; }
// This method is to parse the next child node of the node configuration // This also can be seen, the nodes that we can be configured in the following configuration of the nodes 10 private void parseConfiguration (XNode root) {try {propertiesElement (root.evalNode ( "properties")); // issue # 117 read properties first typeAliasesElement (root.evalNode ( "typeAliases")); pluginElement (root.evalNode ( "plugins")); objectFactoryElement (root.evalNode ( "objectFactory")); objectWrapperFactoryElement (root.evalNode ( "objectWrapperFactory")); settingsElement (root.evalNode ( "settings")); environmentsElement (root.evalNode ( "environments")); // read it after objectFactory and objectWrapperFactory issue # 631 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); } }}