Location>code7788 >text

Describing the mybatis startup process in the context of interceptors

Popularity:724 ℃/2024-07-31 15:42:04

synopsis

There are generally two entry points to start mybatis, in combination with the spring framework by the spring integration package under the SqlSessionFactoryBean to start the

If spring is not integrated, there is XMLConfigBuilder to start the

Both of these startup entries initialize the Configuration object, which is the object form of the mybatis configuration file, and our implementation of the mybatis interceptor is loaded into the configuration at this time

priming process

First, the SqlSessionFactoryBean calls the post method after class loading is complete, executing buildSqlSessionFactory(); the method initializes the configuration class configuration, and after initialization of the configuration class is complete, the SqlSessionFactoryBuilder class is called with the builder method

Second, () method in the new DefaultSqlSessionFactory object, using the configuration class as an input parameter

Third, the SqlSession object;

In the MyBatis framework, theSqlSessionobject is the core database session manager that provides methods for performing persistent operations and managing the lifecycle of persistent operations. Specifically, theSqlSessionThe role of the object includes the following:

  1. Database session management:SqlSessionResponsible for managing the connection to the database, it is the main entry point for database operations. In the application program, the connection to the database is made through theSqlSessionObjects can execute SQL statements, commit transactions, close connections, and more.

  2. SQL Execute operation:SqlSessionProvides methods for executing SQL statements to perform database operations such as query (select), update (update), insert (insert), and delete (delete).

  3. Transaction Management: In MyBatis.SqlSessionThe life cycle of a transaction can be controlled. It is possible to control the life cycle of a transaction through theSqlSessionOpen transactions, commit transactions, or rollback transactions to ensure consistency and integrity of data operations.

  4. Mapper Interface Bindings: MyBatis provides the Mapper interface bindings via theSqlSessionprovides an implementation class (proxy class) for the Mapper interface.The Mapper interface defines the methods for database operations, and theSqlSessionBy loading the Mapper interface and the XML mapping file, the Mapper interface is bound to actual SQL statements so that the methods defined in the Mapper interface can be executed to operate on the database.

  5. Resource management:SqlSessionThe database connection will be acquired at creation time and released at closure time to ensure that resources are managed and released appropriately to avoid resource leakage.

  6. You can think of a sqlsession as an upgrade to a connection.

Fourth, the creation of sqlsession object, mybatis provides from datasource and connection two ways to create sqlsession method; the difference between these two ways is whether you need to manually manage the database connection and transaction; in the acquisition of the transaction factory, through the transaction factory and the executor Executor type through the configuration class method to create the executor object;

  • Create an executor object; the executor object encapsulates methods such as query and update, and transactional operations and caching are also managed in this object;
  • About interceptors, mybaits' interceptors can intercept the methods of executor, statementhandler, pameterhandler, and resultsethandler; during the creation process of executor, our customized interceptors will enhance the executor; specifically, the The chain of interceptors we define will execute the plugin method on the executor object in turn, execute the wrap method of the Plugin class in the plugin method, and parse the configuration of the interceptor in the wrap method to generate a proxy object for the executor (jdk dynamic proxy). In the execution of the method defined in the executor, according to whether the method is intercepted to decide by the proxy object or executor object execution

Fifth, the generation of statementhandler; statementhandler object in the execution of the executor object is created when the method, statementhandler to complete the parameter binding and other work, and the execution of the sql statement