Mybatis principle analysis (1) parsing the global xml configuration file to generate SqlSessionFactory process

In Mybatis alone, we all implement the steps described in the above figure, so let's see how SqlSessionFactory is created through the source code.

Source code in-depth

After entering, first call the overload method of a builde inside, pass in the stream of our global configuration file, click in

Inside is to create an XMLConfigBuilder object, we can know by name that the object should be a class that mainly parses our global configuration file, then let’s look at its parse method

 The main logic inside is the parseConfiguration method, and the last returned is a Configuration object, we can know that this Configuration object actually stores the information of the global configuration file we parsed.

 You can see that in the parseConfiguration method, the content of each node under the passed configuration node is parsed, such as properties node, plugins node, environment node, etc., and then parsed into the content and stored in our Configuration object . Here we focus on Look at parsing mappers nodes.

 The main need here is to parse the content under the mappers node. At this point, we can see a sentence configuration.addMappers (mapperPackage), let’s take a closer look at what is done in this method

Found that there are multiple layers of encapsulation, we enter the method in the following figure:

Then execute step by step, execute to Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();

Traverse this set collection containing all interface class paths, and go deep into addMapper(mapperClass)

 

In-depth loadXmlResource method, we can find

Then I found that an XMLMapperBuilder was created below to infer that the following parse method is to parse xml, and then execute to the xmlParse.parse method 

  Dive into this method

Continue to dive into the configurationElement(parser.evalNode("/mapper")) method

 For these nodes, we are more concerned about the select, insert, update, and delete nodes, so go into the last sentence of the buildStatementFromContext method

Go deeper into the overloading of this method

 You can see that there is an XMLStatementBuilder. The parseStatementNode method of this class may be an important method for parsing xml files.

Let's take a look at what this method does with these parsed attribute values, and continue to go deeper

So now we can know how the interface xml file is parsed and where the parsed result is stored. The answer is that each select|update|insert|delete tag in an interface xml file corresponds to a MappedStatement object . This conclusion is very important, and it is very helpful for subsequent crud operations and plugins.

The above is the execution process of the addMapper method of MapperRegistry. An important process is to encapsulate each addition, deletion, modification, and check label into a MappedStatement object.

Let's go back to the MapperRegistry class. In this class, there is a map collection named knownMappers. The key value is our interface type (such as com.zyh.mybatis.dao.DeptMapper), and the value is MapperProxyFactory, as shown below :

Let's take a look at the MapperProxyFactory class again:

So what is the role of this MapperProxyFactory? Let's leave it alone here. In fact, it is related to our dynamic creation of interface classes. Now we only know how the values ​​in it come from. 

Back to where we parsed the mappers tag, we did a lot of things in parsing the tag, including obtaining our interface classes under the package path according to the package path of the package, and storing each interface class as a key in the MapperRegistry One of the variables in the Map object named knownMappers, and the value corresponding to each key of the Map is a MapperProxyFactory object, which assigns the interface class object we passed in to the mapperInterface object when it is initialized.

Finally, the global configuration file is parsed, our Configuration object is returned, and the Configuration object is passed to the overload method build, and a DefaultSqlSessionFactory object that implements the SqlSessionFactroy interface is returned.

Finally, in the entire process of initializing the SqlSessionFactory, including the initialization of the Configuration object, and when the Configuration object is initialized, there are two important properties: MappedStatement and MapperRegistry. After the initialization of the Configuration object is complete, each MappedStatement corresponds to each addition, deletion, modification, and check tag. The Configuration object has a Map<String,MappedStatement> property, which stores the MappedStatement object. Note that a MappedStatement with addition, deletion, modification, and check tags will store two In this Map, once is the label's namespace+id as the key, and once is the direct id as the key. Such as:

 

Guess you like

Origin blog.csdn.net/weixin_37689658/article/details/99086353