[MyBatis] Analysis and operation principle of MyBatis

MyBatis programming steps

1. Create SqlSessionFactory
2. Create SqlSession through SqlSessionFactory
3. Perform database operations through sqlsession
4. Call session.commit() to submit the transaction
5. Call session.close() to close the session

How MyBatis works

1) Read the MyBatis configuration file: mybatis-config.xml is the global configuration file of MyBatis, which configures the operating environment of MyBatis and other information, such as database connection information.

2) Load the mapping file. The mapping file is the SQL mapping file, which is configured with SQL statements for operating the database.

It needs to be loaded in the MyBatis configuration file mybatis-config.xml. The mybatis-config.xml file can load multiple mapping files, and each file corresponds to a table in the database.

3) Construct a session factory: construct a session factory SqlSessionFactory through configuration information such as MyBatis environment.
4) Create session object: The SqlSession object is created by the session factory, which contains all methods for executing SQL statements.

5) Executor: the bottom layer of MyBatis defines an Executor interface to operate the database, it will be based on

The parameters passed by SqlSession dynamically generate the SQL statements that need to be executed, and are responsible for the maintenance of the query cache.

6) MappedStatement object: there is a MappedStatement in the execution method of the Executor interface

Type of parameter, which is an encapsulation of the mapping information and is used to store the id and parameters of the SQL statement to be mapped.

7) Input parameter mapping: The input parameter type can be a collection type such as Map and List, or it can be a basic data type

And POJO type. The input parameter mapping process is similar to the process of setting parameters for the JDBC preparedStatement object.

8) Output result mapping: The output result type can be a collection type such as Map and List, or it can be a basic data type and a POJO type. The output result mapping process is similar to the JDBC parsing process of the result set.

Functional architecture of MyBatis

The functional architecture of Mybatis is divided into three layers:

API interface layer : Provide interface API for external use, developers use these local API to manipulate the database. As soon as the interface layer receives the call request, it calls the data processing layer to complete specific data processing.

Data processing layer : responsible for specific SQL lookup, SQL analysis, SQL execution and execution result mapping processing. Its main purpose is to complete a database operation according to the call request.

Basic support layer : Responsible for the most basic functional support, including connection management, transaction management, configuration loading, and cache processing. These are common things, and they are extracted as the most basic components. Provide the most basic support for the upper data processing layer.

What is the framework design of MyBatis?

MyBatis initialization, will from the mybatis-config.xmlconfiguration file, configured to parse Configurationthe class.

(1) Load configuration: The configuration comes from two places, one is the configuration file, and the other is the annotation of the Java code. The SQL configuration information is loaded into MappedStatementobjects (including the incoming parameter mapping configuration and the executed SQL statement , Result mapping configuration), stored in memory.

(2) SQL analysis: When the API interface layer receives the call request, it will receive the incoming SQL ID and incoming object (it can be Map, JavaBean or basic data type), Mybatis will find the corresponding according to the SQL ID MappedStatement, then the passed parameter object to MappedStatementparse the SQL statement can be parsed and final parameters to be executed.

(3) SQL execution: Take the final SQL and parameters to the database for execution, and get the result of operating the database.

(4) Result mapping: The result of operating the database is converted according to the mapping configuration, which can be converted into HashMap, JavaBean or basic data type, and the final result is returned.

Guess you like

Origin blog.csdn.net/Black_Customer/article/details/107417288