In order to enable readers to have a clearer understanding of the MyBatis program, before formally explaining the MyBatis introductory case, let’s first understand the working principle of the MyBatis program, as shown in Figure 1.

Figure 1 MyBatis framework execution flow chart

As can be seen from Figure 1, the MyBatis framework generally goes through 8 steps when operating the database. Each step in Figure 6-4 is explained in detail as follows.

(1) Read the MyBatis configuration file MyBatis -config.xml. As the global configuration file of Mybatis, mybatis config. XML configures the operating environment and other information of MyBatis. The main content is to obtain the database connection.

(2) Load the mapping file mapper.xml. The mapper. XML file is an SQL mapping file. The SQL statements used to operate the database are configured in the file and must be loaded in mybatis-config. XML to be executed. Mybatis -config.xml can load multiple configuration files, each corresponding to a table in the database.

(3) Build a session factory. Build session factory SqlSessionFactory by MyBatis environment configuration information.

(4) Create a SqlSession object. The SESSION factory creates the SqlSession object, which contains all the methods that execute the SQL.

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

(6) The Executor interface execution method contains a parameter of type MappedStatement. This parameter encapsulates the mapping information and stores the ID and parameters of the SQL statement to be mapped. An SQL file in mapper. XML corresponds to an MappedStatement object. The ID of the SQL statement is the ID of the MappedStatement.

(7) Input parameter mapping. The MappedStatement object defines the input parameters (Map, List, primitive, and POJO types) used by the user to execute the SQL statement. The MappedStatement object defines the input parameters (Map, List, primitive, and POJO types) for the user to execute the SQL statement. Map the input Java object into the SQL statement. The mapping of input parameters here is similar to the setting of parameters for a preparedStatement object in JDBC programming.

(8) Output result mapping. After an SQL statement has been executed in the database, the MappedStatement object defines the output of the SQL execution. The MappedStatement object defines the output of the SQL execution (Map and List types, primitive types, and POJO types). Map the output to a Java object. This process of mapping the output to a Java object is similar to the result parsing process in JDBC programming.

Through the explanation of the implementation process of MyBatis framework, I believe that readers have a preliminary understanding of 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 implementation process of MyBatis framework is explained here for the convenience of learning the following procedures. After studying the MyBatis framework, the reader will understand the content explained above.

Finally, welcome to pay attention to my public account: Java programmers gathering place, Maidong will share Java related technical articles or industry information every day, welcome to pay attention to and forward the article!