[Mybatis source code analysis] the process of mybatis scanning xml parsing

The environment uses springboot 


1. Starting from the MybatisAutoConfiguration auto-assembly class, you can see that a SqlSessionFactoryBean has been created, after a series of filling attribute operations, including the attribute filling of the configuration configuration class

n Finally, execute the getObject method to obtain a bean of type SqlSessionFactory

The property filling of the configuration configuration class: it is the following configuration we have in the configuration file, which will be bound to the MybatisProperties class by springboot

Then the configuration properties of the MybatisProperties class are put into the configuration properties of SqlSessionFactoryBean


2. Enter the getObject method, you can see that when the sqlSessionFactory has not been initialized, first call the afterPropertiesSet method to initialize .

3. Then enter the buildSqlSessionFactory method, and build the SqlSessionFactory here

It can be seen that the configuration property is configured in the first step, so it will go directly to if.

We can also not configure mybatis.configuration.xxx=xxx configuration, and use the xml file configuration method, only need to set mybatis.config-location=c:/mybatis-config.xml. That will use the XMLConfigBuilder class to parse your config xml.

Note: The priority of configuration is greater than that of config xml.

The implementation of the baseBuilder abstract class is as follows:

  • XMLConfigBuilder is used to parse config xml
  • XMLMapperBuilder is used to parse Mapper xml
  • XMLStatementBuilder is used to parse the select|insert|update|delete tag in Mapper xml
  • XMLScriptBuilder is used to parse the if|foreach|trim|where|choose|when|otherwise|bind and other tags in the select|insert|update|delete tag
  • SqlSourceBuilder is used to parse sql, such as replacing #{id}
  • MapperBuilderAssistant is used to assist in the construction of Configuration. For example: Configure Mapper (statment, cache, etc.) information to Configuration

4. After parsing the configuration, the next step is to parse the Mapper, using the XMLMapperBuilder class

The XPathParser class is actually an encapsulation of jdk's Document, XPath and other classes

5. Enter the parse method of XMLMapperBuilder, you can see from the method name, first configure the /mapper element, then bind the namespace, and then parse the ResultMap, CacheRefs, Statements and other elements

6. The configurationElement method used XPathParser to parse the /mapper element into XNode in the previous step, and continue to parse the element downward, such as: cache-ref, cache, resultMap, select|insert|update|delete, etc.

7. Enter the buildStatementFromContext method and start building XMLStatementBuilder to parse the select|insert|update|delete tag

8. The analysis is completed and finally the configuration is added to the configurtion through MapperBuilderAssistant

9. Go back to step 7 sqlSessionFactoryBuilder.build method to build DefaultSqlSessionFactory object through Configuration

10. Go back to the first step and put the returned SqlSessionFactory object into the spring container


The above is the whole parsing process of springboot integrated mybaits xml

The parsing of other xml elements is similar, they are all parsed into XNode, and finally the configuration is added to the configurtion through MapperBuilderAssistant


The underlying process reference for the injection and use of mybatis dao proxy objects: https://blog.csdn.net/sumengnan/article/details/113953507

Guess you like

Origin blog.csdn.net/sumengnan/article/details/114003391