This is the fourth day of my participation in Gwen Challenge
1. Architecture design
We divide the functional architecture of Mybatis into three layers:
(1) API interface layer: the API provided for external use, developers through these local APIS to manipulate the database. 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 MyBati S;
B. Use the Mapper agent
(2) Data processing layer: responsible for specific SQL search, SQL parsing, SQL execution and execution result mapping processing, etc. Its main purpose is to complete a database operation based on the request being invoked.
(3) Basic support layer: Responsible for the most basic function support, including connection management, transaction management, configuration loading and cache processing, these are common things, they are extracted as the most basic components. It provides the most basic support for the upper data processing layer
Two: the main components and their relationship
build | 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 the number of 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 wrapper around the < SELECT /update/delete/Insert > node |
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 |
Three: the overall process
(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
(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.