Mybatis 3.4.6 source code deep analysis call main process

Article Directory

 

1 Overview

MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual setting of parameters and obtaining result sets. It eliminates most of the JDBC repetitive code and parameter settings and result set mapping through XML configuration. As a developer, in order to better learn and understand, it is necessary to in-depth study and understand the source code of the excellent framework and the design ideas of mybatis for better reference. At the same time, you can better understand the essence of design patterns by studying the source code.

2 Environment setup

You can download the mybatis source code from the git source link , and then pour it into idea. The source code above is the source code of version 3.4.6.

3 Introduction to project hierarchy

From the perspective of MyBatis code implementation, I personally believe that MyBatis mainly includes the following core components:

  • SqlSession, as the main top-level API of MyBatis work, represents the session that interacts with the database, and completes the necessary database addition, deletion, modification, and query functions
  • Executor MyBatis executor is the core of MyBatis scheduling, responsible for SQL statement generation and query cache maintenance
  • StatementHandler encapsulates the JDBC Statement operation and is responsible for the operation of the JDBC statement, such as setting parameters and converting the Statement result set into a List set.
  • ParameterHandler is responsible for converting the parameters passed by the user into the parameters required by the JDBC Statement,
  • ResultSetHandler is responsible for converting the ResultSet result set object returned by JDBC into a List type collection;
  • TypeHandler is responsible for the mapping and conversion between java data types and jdbc data types
  • MappedStatement MappedStatement maintains a package of <select|update|delete|insert> node,
  • SqlSource is responsible for dynamically generating SQL statements based on the parameterObject passed by the user, encapsulating the information in the BoundSql object, and returning
  • BoundSql represents dynamically generated SQL statements and corresponding parameter information
  • All configuration information of Configuration MyBatis is maintained in the Configuration object.
  • MapperProxyFactory interface proxy factory class This class is mainly to try the implementation of jdk proxy mapper to call SqlSession. The name of the interface class corresponds to the value of the nameSpace of the configuration file, and the id of the label corresponds to the method name
    . The relationship between them (the picture is stolen) :
    relation chart
    If MapperProxyFactory is also added to the above picture, it should be placed on the top. This is equivalent to automatically implementing the mapper interface and then calling the method in sqlsession

4 Mybatis analysis configuration file timing diagram

Mybatis is to customize SQL, stored procedures and advanced mapping through XML configuration. So first, he must parse the relevant mapper configuration file and put the results of parsing each other in the Configuration class. Personally think that the main ones are placed as follows (class name. method):

  • The SqlSessionFactoryBuilder.build() parameter is the InputStream of the configuration file (this configuration is the configuration of mybatis not the sql file configuration that we often need to write in our development). The function is to call XMLConfigBuilder.parse() to get the Configuration, and then create the DefaultSqlSessionFactory object
  • The XMLConfigBuilder.parse() parameter is the InputStream of the configuration file (this configuration is the configuration of mybatis, not the sql file configuration that we often need to write in our development). The function is to call XPathParser.evalNode() to get the XNode node, and then call XMLConfigBuilder.parseConfiguration to parse the node. The parsing node calls different methods through different nodes to analyze and save the results of side-by-side parsing in the Configuration. The most important thing in the analysis is to parse our sql configuration file, which is parsed in XMLConfigBuilder.mapperElement
  • The parameter of MLConfigBuilder.mapperElement() is the Xnode node. Here, the function is mainly to call the XMLMapperBuilder.parse method. This method focuses on parsing each sql file that we often write in development.
  • The XMLMapperBuilder.parse() parameter is the sql configuration that we often need to write in our development. The function is mainly to parse our sql file. This is divided into the tags we often use for parsing,,,,, and so on. After parsing, call configuration The .addMappedStatement method saves the MappedStatement object into the map of mappedStatements, and the key is the id of the namespace tag. In fact, we generally start from here when we use the SSM framework. Mybaris-spring starts from here. If you are interested, you can take a look at the SqlSessionFactoryBean.buildSqlSessionFactory() method
    in mybaris-spering. The main process here is almost so many, but there are many other analysis and interested parties can go and see
    Analyze the timing diagram

5 Mybatis call sequence diagram

The configuration file has been parsed before, and the following is to call the relevant methods to operate the database. In fact, selectList and selectOne are actually queried by calling the selectList method (only selectOne determines whether the number of items returned by the query list is one, otherwise a TooManyResultsException is thrown) , And whether it is integrating mybatis-spring or directly viewing the 3mybatis source code, it is operated through the methods in sqlSession. This is equivalent to a refinement of  1.2.
Mybatis call sequence diagram-sqlSession call
Here is an explanation of this picture that I personally think is more important:

  • CachingExecutor ------ This is the mybatis secondary cache execution class, which is enabled by default. Although it is enabled by default, it still needs to add a real storage engine to each nameSpace configuration file, otherwise it will go directly Adjust the executor to operate the database, and it is based on nameSpace, so many people on the Internet also say that it is not recommended to use the second-level cache (that is, because it is based on nameSpace, for example, there is a userMapper which is all user operations, and other places There is no problem if there is no user operation, otherwise there will be problems.)
  • SimpleExecutor ------ This is an executor class. There are many kinds of this, including ReuseExecutor and BatchExecutor. Different executors can be selected through configuration. The default and commonly used method is SimpleExecutor, which inherits from BaseExecutor. The delegate. query() and queryFromDatabase methods are both methods of the parent class, and doQuery() is the method to call different executors to operate the database. There is also a general method here is the prepareStatement() method, this method is to go back to the database connection, and pre-execute the SQL statement, and set the query parameters into it
  • PreparedStatementHandler ------ This class actually sets the parameters, and obtains the data returned by the database, and passes the parameters to the DefaultResultSetHandler to encapsulate the data returned by the database into the data we set.

Guess you like

Origin blog.csdn.net/csdn_lulinwei/article/details/108657872