Sqlsession is one of the most important mybatis Guangdong Lucky 28 Source Sales (www.1159880099.com) QQ1159880099 of the construction, you can simply think MyBatis a series of configuration purposes is to generate similar The JDBC-generated Connection object Sqlsession object, in order to open "communication" with the database, through the sqlsession can implement additions and deletions (of course, it is now more recommended to use the Mapper interface form), then it is how to implement the implementation, This is what this blog post describes, which will involve a simple source explanation.
Understand the operation principle of sqlsession is to learn MyBatis plug-ins, because MyBatis plug-in will be in the sqlsession run the "insert" operation, if not well understood, MyBatis plug-ins may overwrite the corresponding source caused serious problems. In view of this, this blog post as far as possible detailed introduction sqlsession operation Principle!
Recommendation: In my previous post, "MyBatis cache (1)--------system cache and Simple configuration Introduction" introduced to the sqlsession of the production process, you can understand before reading this blog post may be better understood!
Note: This blog post is also my recent real understanding of MyBatis to start writing, there may be some places are not too accurate, if there are errors please point out, another creation is not easy, hope reproduced, thank you!
Parameter data: "Basic principles and practice of MyBatis" (I only have the electronic version of the PDF, the need for friends can contact me)
1, sqlsession Simple Introduction
(1) sqlsession Simple principle Introduction
Sqlsession provides the Select/insert/update/delete method for using these methods with the Sqlsession interface in older versions, but the Mapper interface method is recommended in the new version of MyBatis.
Mapper is actually a dynamic proxy object, into the Mappermethod Execute method can simply find sqlsession Delete, update, query, select method, from the bottom realization: through the dynamic agent technology, let the interface run up, followed by command mode, Finally, the Sqlsession interface method (Getmapper () method is used to wait until mapper) to execute the SQL query (that is, the Mapper interface method is implemented at the bottom or by the Sqlsession interface method).
Note: Although the above is only a simple description, but the actual source code is relatively complex, the following will be combined with the source code for a simple introduction!
(2) sqlsession four important objects
1) Execute: Dispatch execution Statementhandler, Parmmeterhandler, resulthandler execute corresponding SQL statement;
2) Statementhandler: Use the database statement (Preparestatement) to perform operations, that is, the bottom layer is encapsulated preparestatement;
3) Parammeterhandler: Processing SQL parameters;
4) Resulthandler: Result set resultset package processing is returned.
2. Sqlsession Four objects
(1) Execute actuator:
Actuators play a critical role, and it is something that actually executes Java's interaction with the database and participates in the entire SQL query execution process.
1) There are three main actuators: Simple actuator (not configured is the default actuator), reuse is a reuse preprocessing statement, Batch batch update, batch dedicated processor
Copy Code
Package org.apache.ibatis.session;
/**
- @author Clinton Begin
*/
public enum Executortype {
Simple, Reuse, BATCH
}
Copy Code
2) Actuator Action: Executor will first call Statementhandler's prepare () method to precompile the SQL statement, set some basic operating parameters, and then call Parameterize () The Paremeterhandler method enables the setup of parameters, completes precompilation, and simply sums up the SQL statements first, then sets the parameters
First of all, take simpleexecutor as an example, look at the source code we have the following important points of knowledge:
First: Executor Select the appropriate executor generation by using the Newexecutor () method in the Configuration object
Copy Code
Public Executor Newexecutor (Transaction Transaction, Executortype executortype) {
Executortype = Executortype = = null? Defaultexecutortype:executortype;
Executortype = Executortype = = null? ExecutorType.SIMPLE:executorType;
Executor Executor;
if (Executortype.batch = = Executortype) {
Executor = new Batchexecutor (this, transaction);
} else if (Executortype.reuse = = Executortype) {
Executor = new Reuseexecutor (this, transaction);
} else {
Executor = new Simpleexecutor (this, transaction);
}
if (cacheenabled) {
Executor = new Cachingexecutor (executor);
}
Executor = (executor) interceptorchain.pluginall (executor);
return executor;
}
Copy Code
(Note: The last Interceptorchain.pluginall () executes the layer dynamic agent, finally can modify the plug-in code before calling the real executor, This is why learning to MyBatis plug-ins must know the sqlsession of the running process)
Second: The Statementhandler in the actuator is built according to the configuration
Copy Code
Public simpleexecutor (Configuration configuration, Transaction Transaction) {
Super (configuration, transaction);
}
@Override
public int DoUpdate (mappedstatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = Ms.getconfiguration ();
Statementhandler handler = Configuration.newstatementhandler (this, MS, parameter, rowbounds.default, NULL, NULL);
stmt = Preparestatement (Handler, Ms.getstatementlog ());
Return Handler.update (stmt);
} finally {
Closestatement (stmt);
}
}
Copy Code
Third: Executor executes the Statementhandler prepare () method to precompile----> Fill in the Connection object----> Then call the Parameterize () method to set the parameters----> Finish precompilation
Copy Code
Private Statement preparestatement (Statementhandler handler, Log Statementlog) throws SQLException {
Statement stmt;
Connection Connection = getconnection (Statementlog);
stmt = Handler.prepare (connection, transaction.gettimeout ());
Handler.parameterize (stmt);
return stmt;
}
Copy Code
Summarize the above drawing a simple mind map as follows:
(2) Statementhanlder database session
The Parameterize method in the Preparestatement method of executor leads to Statementhandler
The generation of Statementhandler is generated by the Newstatementhandler () method in the configuration method, but Statementhandler is Routingstatementhandler
Copy Code
Public Statementhandler Newstatementhandler (Executor Executor, mappedstatement mappedstatement, Object Parameterobject,
Rowbounds rowbounds, Resulthandler Resulthandler, Boundsql boundsql) {
Statementhandler Statementhandler = new Routingstatementhandler (executor, mappedstatement, Parameterobject, RowBounds , Resulthandler, Boundsql);
Statementhandler = (Statementhandler) interceptorchain.pluginall (Statementhandler);
return statementhandler;
}
Copy Code
MyBatis's sqlsession run Guangdong Lucky 28 Source Sale principle