MyBatis Source code Interpretation (4)--sqlsession (UP)

Source: Internet
Author: User
Tags what sql

In the previous blog, I mentioned how MyBatis implements the proxy class Mapperproxy, and throws a question--how to execute a specific SQL statement, and at the end of the article mentions that Mappermethod's execute uses the command pattern to determine what SQL statement And the execution of the specific statement is referred to sqlsession processing. So this blog is about sqlsession.

In the Sqlsession interface contains all the possible SQL statements are not listed here, please refer to Org.apache.ibatis.session.SqlSession source code. Defaultsqlsession is the Sqlsession implementation class, so we focus on the Defaultsqlsession class. In the previous blog, we mentioned that we will choose the Executeformany method of Mapperproxy. The Sqlsession.<e>selectlist method is called in this method. So let's take a look at the SelectList method of the Org.apache.ibatis.session.default.DefaultSqlSession class.

1 //org.apache.ibatis.session.defaults.DefaultSqlSession2 ...3 @Override4    Public<E> list<e>selectlist (String statement) {5     return  This. SelectList (Statement,NULL);6   }7 8 @Override9    Public<E> list<e>selectlist (String statement, Object parameter) {Ten     return  This. SelectList (statement, parameter, rowbounds.default); One   } A  - @Override -    Public<E> list<e>selectlist (String statement, Object parameter, rowbounds rowbounds) { the     Try { -Mappedstatement ms =configuration.getmappedstatement (statement); -       returnExecutor.query (MS, wrapcollection (parameter), rowbounds, Executor.no_result_handler); -}Catch(Exception e) { +       ThrowExceptionfactory.wrapexception ("Error querying database. Cause: "+e, E); -}finally { + errorcontext.instance (). reset (); A     } at   } -......

We see that there are three overloaded methods for SelectList, and the last call is public <E> list<e> selectlist (String statement, Object parameter, Rowbounds rowbounds). In this method, the first argument is of type string and named statement, and the second parameter is named Object parameter, which returns to Mappermethod.

1 //Org.apache.ibatis.binding.MapperMethod2  Private<E>Object Executeformany (sqlsession sqlsession, object[] args) {3List<e>result;4Object param =Method.convertargstosqlcommandparam (args);5     if(Method.hasrowbounds ()) {6Rowbounds rowbounds =method.extractrowbounds (args);7result = Sqlsession.<e>SelectList (Command.getname (), param, rowbounds); Notice what parameters were passed in.8}Else {9result = Sqlsession.<e>SelectList (Command.getname (), param);Ten     } One     //issue #510 Collections & Arrays Support A     if(!Method.getreturntype (). IsAssignableFrom (Result.getclass ())) { -       if(Method.getreturntype (). IsArray ()) { -         returnConverttoarray (result); the}Else { -         returnconverttodeclaredcollection (Sqlsession.getconfiguration (), result); -       } -     } +     returnresult; -}

The 7th line of code can see what parameters were passed in and how the Trace Command.getname () came in.

Private Final SqlCommand command;

It appears to be a variable called SqlCommand, and we can find that this SqlCommand is a static inner class of Mappermethod.

1 //Org.apache.ibatis.binding.MapperMethod2    Public Static classSqlCommand {3 4     Private FinalString name;5     Private Finalsqlcommandtype type;6 7      PublicSqlCommand (Configuration configuration, class<?>Mapperinterface, method) {8String statementname = mapperinterface.getname () + "." +Method.getname (); Get interface + method name9Mappedstatement ms =NULL; Define a mappedstatement, this mapperedstatement will be introduced laterTen       if(Configuration.hasstatement (statementname)) {//Find out if there is a fully qualified name for this method from the configuration object Onems =configuration.getmappedstatement (statementname); There, the Mappedstatement instance is obtained according to the fully qualified name of the method A}Else if(!mapperinterface.equals (Method.getdeclaringclass ())) {//issue #35//If this method is not found in the configuration object, get the fully qualified method name in the parent class up -String parentstatementname = Method.getdeclaringclass (). GetName () + "." +method.getname (); -         if(Configuration.hasstatement (parentstatementname)) {//In the UP parent class to find out if there is this fully qualified method name thems =configuration.getmappedstatement (parentstatementname); There, the Mappedstatement instance is obtained according to the fully qualified name of the method -         } -       } -       if(ms = =NULL) { +         if(Method.getannotation (Flush.class) !=NULL){ -Name =NULL; +Type =Sqlcommandtype.flush; A}Else { at           Throw NewBindingexception ("Invalid bound statement (not found):" +statementname); -         } -}Else { -Name =Ms.getid (); This ms.getid, in fact, is when we configure an SQL statement in the mapper.xml configuration file <select id= "..." ..., which is the fully qualified name of the method name of the interface. -Type =Ms.getsqlcommandtype (); Obviously this is what type of SQL (INSERT, UPDATE, delete, select) is assigned to type -         if(Type = =sqlcommandtype.unknown) { in           Throw NewBindingexception ("Unknown execution method for:" +name); -         } to       } +     } -  the      PublicString GetName () { *       returnname; $     }Panax Notoginseng  -      PublicSqlcommandtype GetType () { the       returntype; +     } A}

The general interpretation of Mappermethod to this, again back to defaultsqlsession, here may be a little bit confusing. Draw a picture to understand this paragraph, hoping to help understand.

Again this time back to the defaultsqlsession.selectlist. SelectList All methods are not posted here, but only one sentence, based on the fully qualified name of the interface method to get mappedstatement.

Mappedstatement ms = Configuration.getmappedstatement (statement);
Return Executor.query (MS, wrapcollection (parameter), rowbounds, Executor.no_result_handler);

After acquiring the mappedstatement, I met one of the four objects under the first sqlsession: Executor actuator. It seems that you have not talked about sqlsession, and then put it in the next section. You can think about what this mappedstatement means, and words too literally can guess a little.

MyBatis Source code Interpretation (4)--sqlsession (UP)

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.