MyBatis Source Code Inquiry (-) Mapperproxyfactory&mapperproxy

Source: Internet
Author: User

Mapperproxyfactory,mapperproxy,mappermethod is three very important classes in MyBatis.

Figure out these 3 classes and you'll probably know the mapping of the Mapper interface to SQL,

why an interface, no instance class can be injected or invoked .

Where Mappermethod can refer to: Mappermethod Source Analysis Portal

When calling MyBatis Addmapper, if you follow the source code will eventually follow the Mapperregistry Addmapper in the following statement:

New Mapperproxyfactory<t> (type));

The type is the Mapper interface. Let's take a look at the source code of Mapperproxyfactory.

Mapperproxyfactory
 Public classMapperproxyfactory<t> {  Private FinalClass<t>Mapperinterface; PrivateMap<method, mappermethod> Methodcache =NewConcurrenthashmap<method, mappermethod>();  PublicMapperproxyfactory (class<t>mapperinterface) {     This. Mapperinterface =Mapperinterface; }   PublicClass<t>Getmapperinterface () {returnMapperinterface; }   PublicMap<method, mappermethod>Getmethodcache () {returnMethodcache; } @SuppressWarnings ("Unchecked")  protectedT newinstance (mapperproxy<t>mapperproxy) {    return(T) Proxy.newproxyinstance (Mapperinterface.getclassloader (),Newclass[] {mapperinterface}, mapperproxy); }   PublicT newinstance (sqlsession sqlsession) {FinalMapperproxy<t> Mapperproxy =NewMapperproxy<t>(sqlsession, Mapperinterface, Methodcache); returnnewinstance (Mapperproxy); }}

Mapperproxyfactory look at the name we know it must be a factory class, just to generate mapperproxy. In fact, Mapperproxyfactory is also very simple. First look at the 2 member Mapperinterface is the Mapper interface, Methodcache is the Mapper interface in the method and method of encapsulation Class (Mappermethod) mapping. The main thing Mappermethod deal with is: handling annotations, parameters, and return values of methods in the Mapper interface. If you want to know more details, refer to Mappermethod source Analysis Portal

Then is 2 newinstance, look at the name to know is factory method, one is protected, one is public, so first look at the public method. It is found that there is a parameter sqlsession,sqlsession processing is actually the process of executing SQL once. In fact, the public newinstance is a new mapperproxy, about Mapperproxy can look at the back of the mapperproxy. It then calls the newinstance of protected.

Then we look at protected's newinstance. Protected is simple and straightforward, that is, using the Java Proxy Factory method to generate a proxy class for the Mapper interface. I think the relationship between the MyBatis source code should be the Java Proxy Dynamic Agent mode should be very familiar with. If you're not familiar with it, you can refer to a Java dynamic agent that I wrote about Java dynamic agent.

We specify mapperproxy to implement the Invocationhandler, so calling the method in the Mapper interface is Mapperproxy invoke. Mapperproxy's invoke is to wrap the method into a mappermethod,mappermethod that deals with the relationship between Mapper interface methods and XML mappings. is not in series.

Mapperproxy
 Public classMapperproxy<t>ImplementsInvocationhandler, Serializable {Private Static Final LongSerialversionuid = -6424540398559729838l; Private Finalsqlsession sqlsession; Private FinalClass<t>Mapperinterface; Private FinalMap<method, mappermethod>Methodcache;  PublicMapperproxy (sqlsession sqlsession, class<t> mapperinterface, Map<method, mappermethod>Methodcache) {     This. sqlsession =sqlsession;  This. Mapperinterface =Mapperinterface;  This. Methodcache =Methodcache; }   PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {if(Object.class. Equals (Method.getdeclaringclass ())) {      Try {        returnMethod.invoke ( This, args); } Catch(Throwable t) {Throwexceptionutil.unwrapthrowable (t); }    }    FinalMappermethod Mappermethod =Cachedmappermethod (method); returnMappermethod.execute (sqlsession, args); }  PrivateMappermethod Cachedmappermethod (method) {Mappermethod Mappermethod=Methodcache.get (method); if(Mappermethod = =NULL) {Mappermethod=NewMappermethod (Mapperinterface, Method, Sqlsession.getconfiguration ());    Methodcache.put (method, Mappermethod); }    returnMappermethod; }}

We see that Mapperproxy implements the Invocationhandler interface, without careful thought, basically because of the Java dynamic Agent.

Now that the Invocationhandler interface is implemented, of course, you should look at the Invoke method first. First, check that the method itself is called directly if it is a method in object.

If it's not just wrapping the method into Mappermethod, we've already mentioned that Mappermethod is mainly the annotation of the processing method, the parameter, the return value, and the corresponding relationship between the parameters and the parameters in the SQL statement. Another wave of advertising, if like to learn more mappermethod details can refer to the Mappermethod source analysis Portal

Because it is a relatively heavy operation to handle the method as Mappermethod, caching is handled here.

Summary

Summing up, our public mybatis added mapper operation actually added is Mapperproxyfactory, this is Mapperproxy factory class, but mapperproxyfactory production is not mapperproxy , but the proxy agent of mapper. The Invocationhandler used is that the Invoke method of Mapperproxy,mapperproxy is actually wrapping the Mapper interface method in order to Mappermethod, and executes the mappermethod of the Execute method. The logic of Mappermethod processing is some mapping between the Mapper method and SQL in XML. For example, the processing of annotations such as @mapkey, some processing such as Rowbounds special parameters, and some other about the Mapper method and SQL mapping. The logic of executing SQL is actually entrusted to the sqlsession related logic.

MyBatis Source Code Inquiry (-) Mapperproxyfactory&mapperproxy

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.