In the previous article, we introduced Mybatis at an introductory level. Today we will take a look at Mybatis from a global perspective.

Main contents of this paper:

Mybatis workflow

The Mybatis workflow can be roughly divided into four steps:

Here are the four steps:

Load the configuration and initialize it

Trigger condition: Load configuration files, such as the global configuration file and xxxmapper. XML configuration file.

The configuration comes from two places, one is a configuration file, the other is a Java code annotation. The SQL configuration information is loaded into one MappedStatement object (including the incoming parameter mapping configuration, the executed SQL statement, and the result mapping configuration) and stored in memory.

Receive call request

Trigger condition: call the API provided by Mybatis

Incoming parameter: the ID of the SQL and the incoming parameter object

Processing: The request is passed to the lower request processing layer for processing.

Processing operation request trigger condition: THE API interface layer passes the request

Incoming parameter: the ID of the SQL and the incoming parameter object

(A) Search for the MappedStatement object based on the SQL ID.

(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.

Return processing result

Returns the final processing result.

General process of Mybatis:

Implementation principle of Mybatis

Mybatis underlying or use native JDBC for database operation, only through SqlSessionFactory, SqlSession Executor, StatementHandler, ParameterHandler, ResultHandler and TypeHandler processor encapsulate these processes.

Executors: Executor (Update, Query, flushStatements, COMMIT, rollback, getTransaction, close, isClosed) ParameterHandler (getParameterObject, setParameters) SQL query handler: StatementHandler (prepare, parameterize, Batch, Update, Query)

ParameterHandler and ResultHandler are used for parameter precompilation and result processing respectively for StatementHandler. Both ParameterHandler and ResultHandler use TypeHandler to map. The diagram below:

MyBatis overall architecture

The functional architecture of Mybatis is divided into three layers:

Detailed content of each layer:

API interface layer

First of all, the interface layer is the layer we deal with the most, the core object is SqlSession, it is the upper application and Mybatis to deal with the bridge, also some people call it the gate, SqlSession defined a lot of database operation methods, interface layer when receiving the call request, The e corresponding module of the core processing layer is called to complete the specific database operation.

Data processing layer

This layer is where the actions related to database operations are done.

The core processing layer does four things:

  • Parses and maps the parameters passed in the interface to JDBC types;
  • Parsing SQL statements in XML files, including inserting parameters and generating dynamic SQL;
  • Execute SQL statement;
  • Process the result set and map it to Java objects.

Plug-ins also belong to the core layer, depending on how they work and which objects they intercept.

Foundation 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.

Main member of MyBatis

Configuration

All the Configuration information of MyBatis is saved in the Configuration object, and most of the Configuration in the Configuration file is stored in this class.

SqlSession

As the main top-level API of MyBatis work, it represents the session when interacting 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.

ParameterHandler

Is responsible for converting user-passed parameters to the corresponding data type of the JDBC Statement.

ResultSetHandler

Converts the ResultSet object returned by JDBC into a collection of type List.

TypeHandler

Responsible for mapping and conversion between Java data types and JDBC data types (dataflow column types, if you like).

MappedStatement

MappedStatement maintain a < select | update | delete | insert > node encapsulation.

SqlSource

Is responsible for dynamically generating SQL statements based on user-passed ParameterObjects, encapsulating the information into BoundSql objects, and returning them

BoundSql

Represents dynamically generated SQL statements and parameter information.

Mybatis hierarchy