This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021
Mybatis architecture
1.1 Architecture design drawing of Mybatis
As can be seen from the figure above, Mybatis is generally divided into three layers
-
Api interface layer
-
The interface layer provides interface apis for external use by developers to manipulate the database locally. As soon as the interface layer receives the call request, it calls the data processing layer to complete the specific data processing.
-
MyBatis interacts with the database in two ways:
A. Use the API provided by traditional MyBatis;
B. Use the Mapper agent
-
-
Data processing layer
- Responsible for specific SQL lookup, SQL parsing, SQL execution and execution result mapping processing, etc. Its main purpose is to complete a database operation based on the request of the call.
-
Frame support
- Be responsible for the most basic functionality support, including connection management, transaction management, configuration loading, and cache handling, all of which are common, and extract them as the most basic components. It provides the most basic support for the upper data processing layer
1.2 Main components and their relationships with each other
artifacts | describe |
---|---|
SqlSession | As the main top-level API of MyBatis, it represents the session of interaction with the database, and completes the necessary function of adding, deleting, changing and searching the database |
Executor | MyBatis executer, the core of MyBatis scheduling, is responsible for the generation of SQL statements and the maintenance of query cache |
StatementHandler | Encapsulates JDBC Statement operations and performs operations on JDBC Statements, such as setting parameters and converting the Statement result set to a List. |
ParameterHandler | Is responsible for converting user-passed parameters into parameters required for JDBC Statements, |
ResultSetHandler | Converts the ResultSet object returned by JDBC into a collection of type List. |
TypeHandler | Responsible for mapping and conversion between Java and JDBC data types |
MappedStatement | MappedStatement maintains a < SELECT |
SqlSource | Is responsible for dynamically generating SQL statements based on user-passed ParameterObjects, marshalling the information into BoundSql objects, and returning them |
BoundSql | Represents dynamically generated SQL statements and parameter information |
Mybatis diagram hierarchy
1.3 General process of Mybatis
(1) Load the configuration and initialize the trigger conditions: Load the configuration file
The configuration comes from two places: configuration files (main configuration file conf.xml,mapper file *.xml) and annotations in Java code. The main Configuration file is parsed and encapsulated in the Configuration file. The SQL Configuration information is loaded into an MappedStatement object and stored in the memory
(2) Receive the call request
Trigger condition: call API provided by Mybatis pass in parameter: ID for SQL and pass in parameter object processing process: pass the request to the lower request processing layer for processing.
(3) Processing operation requests
Trigger condition: the API interface layer passes the request
Incoming parameter: for SQL ID and incoming parameter object handling procedure:
-
(A) Find the MappedStatement object based on the ID of the SQL statement.
-
(B) Parse the MappedStatement object based on the passed parameter object to get the SQL to be executed and execute the passed parameter.
-
(C) Access to the database connection, according to the final SQL statement and the execution of the incoming parameters to the database execution, and get the execution results.
-
(D) According to the result mapping configuration in the MappedStatement object, the obtained execution result is transformed and the final processing result is obtained.
-
(E) Release connection resources.
(4) Return the processing result
Returns the final processing result.