Mybatis Series 2

 

The last article wrote a Demo that simply reflected the process of Mybatis. This time, I will briefly introduce the configuration file of Mybatis:

In the last example, we used SqlSessionFactoryBuilder to create SqlSessionFactory,

So, let's start with SqlSessionFactoryBuilder, let's see how the source code is implemented:

SqlSessionFactoryBuilder source code snippet:

1  public  class SqlSessionFactoryBuilder {
 2   2 
 3   3    // Reader reads the mybatis configuration file and passes in the construction method 
4   4    // In addition to Reader, there is actually a construction method with the corresponding inputStream as a parameter, 
5   5    // This also reflects Flexibility of mybatis configuration 
6   6    public SqlSessionFactory build(Reader reader) {
 7   7      return build(reader, null , null );
 8   8    }
 9   9 
 10 10    public SqlSessionFactory build(Reader reader, String environment) {
11 11      return build(reader, environment, null );
 12 12    }
 13 13   
 14 14    // mybatis configuration file + properties, at this time, properties can not be configured in mybatis configuration file, or you can use ${} form 
15 15    public SqlSessionFactory build (Reader reader, Properties properties) {
 16 16      return build(reader, null , properties);
 17 17    }
 18 18   
 19 19    // Parse the mybatis configuration through XMLConfigBuilder, and then create the SqlSessionFactory object 
20 20    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
21 21     try {
22 22       XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
23 23       //下面看看这个方法的源码
24 24       return build(parser.parse());
25 25     } catch (Exception e) {
26 26       throw ExceptionFactory.wrapException("Error building SqlSession.", e);
27 27     } finally {
28 28       ErrorContext.instance().reset();
29 29       try {
30 30         reader.close();
31 31       } catch (IOException e) {
32 32         // Intentionally ignore. Prefer previous error.
33 33       }
34 34     }
35 35   }
36 36 
37 37   public SqlSessionFactory build(Configuration config) {
38 38     return new DefaultSqlSessionFactory(config);
39 39   }
40 40 
41 41 }

 

Through the source code, we can see that SqlSessionFactoryBuilder parses the configuration file of mybatis we passed in through XMLConfigBuilder. Let's take a look at some of the source code of XMLConfigBuilder:

 

 1  /**
 2  2  * mybatis 配置文件解析
 3  3  */
 4  4 public class XMLConfigBuilder extends BaseBuilder {
 5  5   public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
 6  6     this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
 7  7   }
 8  8 
 9  9   private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
10 10     super(new Configuration());
11 11     ErrorContext.instance().resource("SQL Mapper Configuration");
12 12     this.configuration.setVariables(props);
13 13     this.parsed = false;
14 14     this.environment = environment;
15 15     this.parser = parser;
16 16   }
17 17   
18 18   // Call this method externally to parse the mybatis configuration file 
19 19    public Configuration parse() {
 20 20      if (parsed) {
 21 21        throw  new BuilderException("Each XMLConfigBuilder can only be used once." );
 22 22      }
 23 23 parsed = true ;
 24 24      // configure from root node 
25 25 parseConfiguration(parser.evalNode("/configuration" ));
 26 26      return configuration;
 27 27    }
 2828 
 29 29    // This method is to parse the child nodes under the configuration node 
30 30    // It can also be seen that the nodes we can configure under the configuration are the following 10 nodes 
31 31    private  void parseConfiguration(XNode root) {
 32 32      try {
 33 33 propertiesElement(root.evalNode("properties")); // issue #117 read properties first 
34 34 typeAliasesElement(root.evalNode("typeAliases" ));
 35 35 pluginElement(root.evalNode("plugins" ));
 36 36 objectFactoryElement(root.evalNode("objectFactory" ));
 37 37       objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
38 38       settingsElement(root.evalNode("settings"));
39 39       environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
40 40       databaseIdProviderElement(root.evalNode("databaseIdProvider"));
41 41       typeHandlerElement(root.evalNode("typeHandlers"));
42 42       mapperElement(root.evalNode("mappers"));
43 43     } catch (Exception e) {
44 44       throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
45 45     }
46 46   }
47 47 }

 

Through the above source code, we can see that in the configuration file of mybatis:

  1. The configuration node is the root node.
  2. Under the configuration node, we can configure 10 child nodes, namely: properties, typeAliases, plugins, objectFactory, objectWrapperFactory, settings, environments, databaseIdProvider, typeHandlers, mappers.

This article will only introduce these contents first. The next article will analyze and parse the source code of the more important nodes among the 10 nodes in turn, and see what is being done when parsing these nodes.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342833&siteId=291194637