In the previous article, "Mybatis series in simple terms (1)---Getting started with Mybatis", I wrote a Demo to briefly reflect the process of Mybatis. This time, we will briefly introduce the configuration file of Mybatis:
In the last example, we used SqlSessionFactoryBuilder to create SqlSessionFactory. Then, we will start with SqlSessionFactoryBuilder. Let's first take a look at how the source code is implemented:
SqlSessionFactoryBuilder source code snippet:
1 public class SqlSessionFactoryBuilder { 2 3 //Reader读取mybatis配置文件,传入构造方法 4 //除了Reader外,其实还有对应的inputStream作为参数的构造方法, 5 //这也体现了mybatis配置的灵活性 6 public SqlSessionFactory build(Reader reader) { 7 return build(reader, null, null); 8 } 9 10 public SqlSessionFactory build(Reader reader, String environment) { 11 return build(reader, environment, null); 12 } 13 14 //mybatis配置文件 + properties, 此时mybatis配置文件中可以不配置properties,也能使用${}形式 15 public SqlSessionFactory build(Reader reader, Properties properties) { 16 return build(reader, null, properties); 17 } 18 19 //通过XMLConfigBuilder解析mybatis配置,然后创建SqlSessionFactory对象 20 public SqlSessionFactory build(Reader reader, String environment, Properties properties) { 21 try { 22 XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); 23 //下面看看这个方法的源码 24 return build(parser.parse()); 25 } catch (Exception e) { 26 throw ExceptionFactory.wrapException("Error building SqlSession.", e); 27 } finally { 28 ErrorContext.instance().reset(); 29 try { 30 reader.close(); 31 } catch (IOException e) { 32 // Intentionally ignore. Prefer previous error. 33 } 34 } 35 } 36 37 public SqlSessionFactory build(Configuration config) { 38 return new DefaultSqlSessionFactory(config); 39 } 40 41 }
Through the source code, we can see that SqlSessionFactoryBuilder uses XMLConfigBuilder to parse the mybatis configuration file we passed in. Let’s continue to see Look at XMLConfigBuilder part of the source code:
1 /** 2 * mybatis 配置文件解析 3 */ 4 public class XMLConfigBuilder extends BaseBuilder { 5 public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) { 6 this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props); 7 } 8 9 private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { 10 super(new Configuration()); 11 ErrorContext.instance().resource("SQL Mapper Configuration"); 12 this.configuration.setVariables(props); 13 this.parsed = false; 14 this.environment = environment; 15 this.parser = parser; 16 } 17 18 //外部调用此方法对mybatis配置文件进行解析 19 public Configuration parse() { 20 if (parsed) { 21 throw new BuilderException("Each XMLConfigBuilder can only be used once."); 22 } 23 parsed = true; 24 //从根节点configuration 25 parseConfiguration(parser.evalNode("/configuration")); 26 return configuration; 27 } 28 29 //此方法就是解析configuration节点下的子节点 30 //由此也可看出,我们在configuration下面能配置的节点为以下10个节点 31 private void parseConfiguration(XNode root) { 32 try { 33 propertiesElement(root.evalNode("properties")); //issue #117 read properties first 34 typeAliasesElement(root.evalNode("typeAliases")); 35 pluginElement(root.evalNode("plugins")); 36 objectFactoryElement(root.evalNode("objectFactory")); 37 objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); 38 settingsElement(root.evalNode("settings")); 39 environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631 40 databaseIdProviderElement(root.evalNode("databaseIdProvider")); 41 typeHandlerElement(root.evalNode("typeHandlers")); 42 mapperElement(root.evalNode("mappers")); 43 } catch (Exception e) { 44 throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); 45 } 46 } 47 }
Through the above source code, we can see that in the configuration file of mybatis:
1. configuration The node is the root node.
2. Under the configuration node, we can configure 10 sub-nodes, namely: properties, typeAliases, plugins, objectFactory, objectWrapperFactory, settings, environments, databaseIdProvider, typeHandlers, mappers.
This article will only introduce these contents first. The following articles will analyze and parse the source code of the more important nodes among the 10 nodes in order to see when parsing these nodes. What exactly was done.
The above is the in-depth introduction to the Mybatis series (2)---Configuration Introduction (mybatis source code). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!