MyBatis Source Analysis--sqlsession interface and executor class

Source: Internet
Author: User
Tags instance method stmt

MyBatis framework, when manipulating data, is inseparable from the role of the Sqlsession interface instance class. It can be said that the Sqlsession interface instance is one of the most frequently used classes in the development process. That is the Defaultsqlsession class. If I remember that there is no mistake, there is no Getmapper method in the early days. Adding and deleting changes to each log has a corresponding method to operate. Although it has improved a lot now, but also retained a lot. We can still see a method similar to that of SelectList. Examples of source code can be found. As follows

    sqlsession session = sqlmapper.opensession (transactionisolationlevel.serializable);     Try {      List<Author> authors = session.  SelectList("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors");      Assertequals (2, Authors.size ());     finally {      session.close ();

Of course, in a sense, it's a little annoying to write. But there is no denying that his function does exist. This is why I prefer to use dynamic agent to do the data on the operation. At least the objectivity of the author thinks it's better.

Regardless of the data on which the operation is inseparable from the Sqlsession interface instance. So it's quite important to look into the Qlsession interface instance. The author chooses the SelectList method of Defaultsqlsession class as the entry point to cut into. The code is as follows.

1   Public<E> list<e>selectlist (String statement, Object parameter, rowbounds rowbounds) {2     Try {3Mappedstatement ms =configuration.getmappedstatement (statement);4       returnExecutor.query (MS, wrapcollection (parameter), rowbounds, Executor.no_result_handler);5}Catch(Exception e) {6       ThrowExceptionfactory.wrapexception ("Error querying database. Cause: "+e, E);7}finally {8 errorcontext.instance (). reset ();9     }Ten}

The source code means that it is obtained from the configuration variable, and the corresponding result is obtained by the query method of the Executor class. Let's think about the previous chapter in the Mappedstatement class is the information used to hold a select node or an update node. This means that the incoming statement parameter indicates which select node is going to be executed. The parameter passed in is naturally the value corresponding to the Select Node ID. So what is the executor class. If the more serious people to view the source code, you will find that basically will use the Executor class instance. In a sigh-the beauty of architecture.

The member executor of the Defaultsqlsession class assigns to him a value in the constructor. So we're going to go back and see when the Defaultsqlsession class was instantiated. I also talked about the executor class at the end of the first chapter. From the source code, we can see that he was obtained through the Newexecutor method of the configuration class. The code is as follows

1  PublicExecutor newexecutor (Transaction Transaction, Executortype executortype) {2Executortype = Executortype = =NULL?Defaultexecutortype:executortype;3Executortype = Executortype = =NULL?ExecutorType.SIMPLE:executorType;4 Executor Executor;5     if(Executortype.batch = =Executortype) {6Executor =New Batchexecutor( This, transaction);7}Else if(Executortype.reuse = =Executortype) {8Executor =New Reuseexecutor( This, transaction);9}Else {TenExecutor =New Simpleexecutor( This, transaction); One     } A     if(cacheenabled) { -Executor =New cachingexecutor(executor); -     } theExecutor =(Executor) Interceptorchain.pluginall (Executor); -     returnexecutor; -}

Finished. I did not think of the executor class. To tell the truth, if I did not go to see the source code really do not know the original executor class there are sub-categories. Look at the source code above the author has just started to think there are four kinds. But it turns out that the last one is a little different from the three instances above. In order to further confirm the author has to look at the official website above the API. The author copied the contents. As follows.

Executortype.simple: This actuator type does not do special things. It creates a new preprocessing statement for the execution of each statement. Executortype.reuse: This actuator type will be reused for preprocessing statements. Executortype.batch: This executor executes all UPDATE statements in bulk, and if SELECT is executed in between them it is necessary to calibrate them to ensure a simple and easy-to-understand behavior.

It seems that the official website explained very clearly. The author is not here to say more. So, by default, which executor subclass does the MyBatis framework call? In fact, this point can be found on the source. As follows

Configuration class:

protected Executortype defaultexecutortype = executortype.simple;

I'm sure we all understand. If you do not specify the type of the corresponding executor class, the MyBatis framework calls the Simpleexecutor class.

For the Executor Class I first learned about this. Because there is a lot to use him, but also only with the function of the back to understand the role of the Executor class. From the SelectList method of the Defaultsqlsession class above, we can find that the query method of the Executor class is finally called.

return executor.query (MS, wrapcollection (parameter), rowbounds, Executor.no_result_handler);

For the variable Ms I will not say much. The Wrapcollection method is worth noting. He means to first determine whether the parameter is a collection type, and if so, to create a new Strictmap type and store it with "collection" as the key. Check again is not the list type. If so, store it as a "list" key. The next is to determine whether the array type, if so, is to create a new Strictmap type and "array" for the key to store. Finally, return back to the Strictmap class instance. Of course, if none of the above, then directly returned to the parameter. If it is not clear why the designer to do this does not matter, the author this time do not know why. So I just remember what he did. Rowbounds type variable rowbounds is generally used for pagination. The default is 0 to 2147483647 values, I believe this is enough for you. The Resulthandler is used to process the returned results, and the returned results are not processed directly here. So I don't have to talk about it.

Baseexecutor class:

   Public throws SQLException {    = ms.getboundsql (parameter);     = Createcachekey (ms, parameter, Rowbounds, boundsql);     return query (MS, parameter, Rowbounds, Resulthandler, Key, Boundsql);}

Subclasses like Simpleexecutor do not inherit the executor class directly. Instead, this layer is passed through the Baseexecutor class to the inheriting executor class. So when the query method is called, the Simpleexecutor class is not called. Instead, the Baseexecutor class is called. Of course you can't find the query method from the Simpleexecutor class. It is clear that after the Baseexecutor class has processed the relevant information, the Doquery method of the executor subclass is called. So we can find the Doquery method of the Simpleexecutor class. The author does not want to elaborate on this process. The author's main goal is to guide everyone to see.

Simpleexecutor class:

1   Public<E> list<e> Doquery (mappedstatement MS, Object parameter, rowbounds rowbounds, Resulthandler Resulthandler, Boundsql boundsql)throwsSQLException {2Statement stmt =NULL;3     Try {4Configuration Configuration =ms.getconfiguration ();5Statementhandler handler =Configuration.newstatementhandler (Wrapper, MS, parameter, Rowbounds, Resulthandler, boundsql);6stmt =preparestatement (Handler, Ms.getstatementlog ());7       returnHandler.<e>query (stmt, resulthandler);8}finally {9 closestatement (stmt);Ten     } One}

From the source point of view, I only know two classes, one is the configuration class and the statement class. The statement class is a knowledge point that belongs to JDBC. The configuration class is part of the config information that belongs to the MyBatis framework. It means to get information about the configuration class through Mappedstatement. Creates a new Statementhandler interface instance through the configuration class. The Statementhandler interface instance is used to process the generated statement class. It is clear that the execution of the SQL statement is in the Statementhandler interface instance.

The author comes to a summary: the way of dynamic Proxy in fact, finally, the use of Sqlsession interface instance method. So the only way to understand the Sqlsession interface instance is to go deeper. The Sqlsession interface instance executes the method in the process of executing the executor class. And the role of the configuration class can be said that the task can be initially used. The current final task is temporarily given to the subclass of the executor class.

All right. In this, the Sqlsession interface and the role of the Executor class have a presumably like space. Of course, only look at the Sqlsession interface and executor class, I feel a bit difficult. At least I don't have the ability to do that. In the author's opinion, the Sqlsession interface and the executor class work in the Doquery method, the end of the round is already finished. Although the author does not say the Sqlsession interface and the role of the Executor class. It's mostly up to you to understand. Next is to understand the role of the Statementhandler interface.

MyBatis Source Analysis--sqlsession interface and executor class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.