3.Mybatis Frame principle
3.1 Functional Architecture Design
Functional Architecture explained:
We divide MyBatis's functional architecture into three tiers:
(1) API Interface Layer: provided to externally used interface APIs, where developers manipulate databases through these local APIs. Once the interface layer receives the call request, it invokes the data processing layer to complete the specific data processing.
(2) Data processing layer: responsible for specific SQL lookups, SQL parsing, SQL execution, and execution result mapping processing. Its primary purpose is to complete a database operation at the request of the call.
(3) Base support layer: responsible for the most basic functional support, including connection management, transaction management, configuration loading and caching processing, these are common things that are extracted from them as the most basic components. Provides the most basic support for the data processing layer of the upper layer.
3.2 Frame Architecture Design
Frame Architecture explained:
(1) Load configuration: configuration from two places, one is the configuration file, one is the Java code annotations, the SQL configuration information is loaded into a Mappedstatement object (including the incoming parameter mapping configuration, executed SQL statement, result mapping configuration), stored in memory.
(2) SQL parsing: when the API interface layer receives a call request, it receives the ID of the incoming SQL and the incoming object (which can be a map, JavaBean, or base data type), and MyBatis finds the corresponding mappedstatement based on the SQL ID. Then the mappedstatement is parsed according to the incoming parameter object, and the SQL statements and parameters are finally executed.
(3) SQL execution: the final SQL and parameters are taken to the database for execution, resulting in the operation of the database.
(4) Result Mapping: The results of the operational database are transformed into a mapped configuration, which can be converted to HashMap, JavaBean, or basic data types and return the final result.
3.3 More popular Framework architecture diagram
1. Mybatis configuration file, including Mybatis Global profile and Mybatis mapping file , where the global configuration file is configured with data source, transaction information, and the mapping file is configured with SQL execution related information.
2, MyBatis by reading the configuration file information (global configuration file and mapping file), constructs the sqlsessionfactory, namely the session factory.
3, through the sqlsessionfactory, you can create sqlsession that is the session. MyBatis is a database that operates through sqlsession.
4, sqlsession itself can not directly manipulate the database, it is through the underlying Executor actuator interface to operate the database. The executor interface has two implementation classes, one for ordinary actuators and one for cache actuators (default).
5. The SQL information to be processed by the executor actuator is encapsulated into an underlying object mappedstatement . The object includes: SQL statement, input parameter mapping information, output result set mapping information. The types of mappings where input parameters and output results include Java simple types, HashMap collection objects, and Pojo object types.
Through the above we can have a certain understanding of the architecture of MyBatis, below we use an example to see how the MyBatis is configured and run.
MyBatis Study (ii)