Do you really know about the operation database of MyBatis framework

In order to enable readers to understand the MyBatis program more clearly, before officially explaining the MyBatis entry case, let's first understand the working principle of the MyBatis program, as shown in Figure 1.

How MyBatis works

Figure 1 Flow chart of MyBatis framework execution

As can be seen from Figure 1, the MyBatis framework has generally gone through 8 steps when operating the database. The following is a detailed explanation of each step of the process in Figure 6-4, as follows.

(1) Read the MyBatis configuration file mybatis-config.xml. As the global configuration file of MyBatis, mybatis-config.xml configures information such as the operating environment of MyBatis, and the main content is to obtain database connections.

(2) Load the mapping file Mapper.xml. The Mapper.xml file is the SQL mapping file. The SQL statement for operating the database is configured in the file and needs to be loaded in mybatis-config.xml to execute. mybatis-config.xml can load multiple configuration files, and each configuration file corresponds to a table in the database.

(3) Construct a conversation factory. The session factory SqlSessionFactory is constructed through configuration information such as MyBatis environment.

(4) Create a SqlSession object. The SqlSession object is created by the session factory, which contains all the methods for executing SQL.

(5) The bottom layer of MyBatis defines an Executor interface to operate the database. It dynamically generates the SQL statements that need to be executed according to the parameters passed by the SqlSession, and is responsible for the maintenance of the query cache.

(6) In the execution method of the Executor interface, a parameter of the MappedStatement type is included, which is an encapsulation of the mapping information and is used to store the id and parameters of the SQL statement to be mapped. A SQL in the Mapper.xml file corresponds to a MappedStatement object, and the id of the SQL is the id of the MappedStatement.

(7) Input parameter mapping. When executing the method, the MappedStatement object defines the input parameters for the user to execute the SQL statement (it can be defined as Map, List type, basic type and POJO type), and the Executor executor will use the MappedStatement object to enter the Java before executing the SQL Objects are mapped to SQL statements. The mapping process of input parameters here is similar to the process of setting parameters to the preparedStatement object in JDBC programming.

(8) Output result mapping. After the SQL statement is executed in the database, the MappedStatement object will define the results of the SQL execution output (can be defined as Map and List types, basic types, and POJO types). The Executor executor will use the MappedStatement object to execute the SQL statement. The output results are mapped to Java objects. This process of mapping output results to Java objects is similar to the process of analyzing results in JDBC programming.

Through the explanation of the execution process of the MyBatis framework above, I believe that readers have a preliminary understanding of the MyBatis framework. For beginners, the content explained above may not be fully understood, and readers are not required to fully understand at this stage. The execution process of the MyBatis framework is explained here to facilitate the learning of the subsequent program. After learning the MyBatis framework, readers will naturally understand the content explained above.

Guess you like

Origin blog.csdn.net/cz_00001/article/details/111590765