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, theSqlSession
object is the core database session manager that provides methods for performing persistent operations and managing the lifecycle of persistent operations. Specifically, theSqlSession
The role of the object includes the following:
-
Database session management:
SqlSession
Responsible 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 theSqlSession
Objects can execute SQL statements, commit transactions, close connections, and more. -
SQL Execute operation:
SqlSession
Provides methods for executing SQL statements to perform database operations such as query (select), update (update), insert (insert), and delete (delete). -
Transaction Management: In MyBatis.
SqlSession
The life cycle of a transaction can be controlled. It is possible to control the life cycle of a transaction through theSqlSession
Open transactions, commit transactions, or rollback transactions to ensure consistency and integrity of data operations. -
Mapper Interface Bindings: MyBatis provides the Mapper interface bindings via the
SqlSession
provides an implementation class (proxy class) for the Mapper interface.The Mapper interface defines the methods for database operations, and theSqlSession
By 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. -
Resource management:
SqlSession
The 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. - 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