MyBatis series (vi)---objectfactory, plugins, Mappers Introduction and configuration

Source: Internet
Author: User

In the article "Simple MyBatis Series (five)---typehandler Introduction and configuration (MyBatis source code)" Simply looked at Typehandler, this time will end for MyBatis configuration file learning, This involves the configuration of several nodes that are not mentioned: Objectfactory, Databaseidprovider, Plugins, mappers.

So, let's take a brief look at how these configurations work:

1. What is Objectfactory? Do I need to configure it?

MyBatis each time a new instance of the resulting object is created, it is done using an object factory (objectfactory) instance. The default object factory needs to do is instantiate the target class, either through the default constructor, or by way of a parameter constructor when the parameter mapping exists. By default, we do not need to configure MyBatis to invoke the default implementation of Objectfactory. Unless we want to customize the implementation of the objectfactory, then we need to manually configure.

So how do you customize the implementation of Objectfactory? How to configure it?

The custom objectfactory only needs to inherit defaultobjectfactory (the implementation class of the Objectfactory interface) and override its methods. Specifically, the department does not say much, the latter is explained in detail.

Write the objectfactory, only need to do the following configuration:

<Configuration>    ......    <objectfactorytype= "Org.mybatis.example.ExampleObjectFactory">        < Propertyname= "Someproperty"value= "+"/>    </objectfactory>    ......  </Configuration

2. What is the role of plugin? Do I need to configure it?

Plugins is an optional configuration. MyBatis in the plugin is actually a interceptor, it can intercept executor, Parameterhandler, Resultsethandler, Statementhandler part of the method, To deal with our own logic. Executor is the real execution of SQL statements, Parameterhandler is to deal with our incoming parameters, but also remember that Typehandler mentioned earlier, mybatis the default to help us achieve a lot of typehandler, When we do not display the configuration Typehandler, MyBatis will automatically select the appropriate typehandler execution according to the parameter type, in fact, Parameterhandler is selected. Resultsethandler is the process of returning results.

How to customize plugin? How to configure?

To customize a plugin, you need to implement the Interceptor interface, here is not detailed, the latter part of the actual combat will be explained in detail. Once defined, the configuration is as follows:

<Configuration>    ......    <Plugins>      <pluginInterceptor= "Org.mybatis.example.ExamplePlugin">        < Propertyname= "Someproperty"value= "+"/>      </plugin>    </Plugins>    ......  </Configuration>

3, mappers, this leads to mybatis one of the core, mappers role? Do I need to configure it?

Under the Mappers node, configuring our Mapper mapping file, the so-called mapper mapping file, is a bridge for MyBatis to build data tables and javabean mappings. In our actual development, usually a mapper file corresponds to a DAO interface, and this mapper can be seen as a DAO implementation. Therefore, the mappers must be configured.

So how do you configure it?

<Configuration>    ......    <mappers>      <!--The first way: Specify by resource -    <MapperResource= "Com/dy/dao/userdao.xml"/>         <!--The second way, the interface is specified through class, and the interface is mapped to the corresponding XML file. However, the interface must be guaranteed to have the same name as the Mapper file (case-insensitive), and I have an interface that is Userdao, which means that the mapper file is Userdao.xml <mapper class= "Com.dy.dao.UserDao"/> -            <!--The Third Way, the direct designation package, the automatic scan, and the method two the same <package name= "Com.dy.dao"/> -      <!--Fourth Way: Specify the Mapper file location by URL <mapper url= "file://..."/> -  </mappers>    ......  </Configuration>

This article is only a brief introduction, more advanced use and its implementation principle, will be in the actual combat part of the following detailed explanation.

The parse source of these nodes is similar to those of the nodes mentioned earlier, so it is no longer said. I will fold the source code, need to open a look.

/*** objectfactory Node parsing*/Private voidObjectfactoryelement (XNode context)throwsException {if(Context! =NULL) {      //read the value of the Type property, then instantiate Objectfactory and set the configuration//to this, simply talk about the configuration of this object, in fact, it is mainly stored in the MyBatisString type = Context.getstringattribute ("type"); //reads the value of the Propertie, can be configured as needed, mybatis the default implementation of Objectfactory does not use the propertiesProperties Properties =context.getchildrenasproperties (); Objectfactory Factory=(objectfactory) resolveclass (type). newinstance ();      Factory.setproperties (properties);    Configuration.setobjectfactory (Factory); } }     /*** Plugins node parsing*/  Private voidPluginelement (xnode parent)throwsException {if(Parent! =NULL) {       for(XNode Child:parent.getChildren ()) {String Interceptor= Child.getstringattribute ("Interceptor"); Properties Properties=child.getchildrenasproperties (); //This shows that when we define a interceptor, we need to implement interceptor, here is not specific, will be explained in detail laterInterceptor Interceptorinstance =(Interceptor) Resolveclass (Interceptor). Newinstance ();        Interceptorinstance.setproperties (properties);      Configuration.addinterceptor (interceptorinstance); }    }  }    /*** Mappers Node parsing * This is one of the core of MyBatis, here is a brief introduction, in the next article will be analyzed*/  Private voidMapperelement (xnode parent)throwsException {if(Parent! =NULL) {       for(XNode Child:parent.getChildren ()) {if("Package". Equals (Child.getname ())) {          //If the child node of the Mappers node is the package, then scan theString mapperpackage = Child.getstringattribute ("name");        Configuration.addmappers (Mapperpackage); } Else{String Resource= Child.getstringattribute ("Resource"); String URL= Child.getstringattribute ("url"); String Mapperclass= Child.getstringattribute ("Class"); //resource, URL, class three select one                    if(Resource! =NULL&& URL = =NULL&& Mapperclass = =NULL) {errorcontext.instance (). resource (Resource); InputStream InputStream=Resources.getresourceasstream (Resource); //Mapper mapping files are parsed by XmlmapperbuilderXmlmapperbuilder Mapperparser =NewXmlmapperbuilder (inputstream, configuration, Resource, configuration.getsqlfragments ());          Mapperparser.parse (); } Else if(Resource = =NULL&& URL! =NULL&& Mapperclass = =NULL) {errorcontext.instance (). resource (URL); InputStream InputStream=resources.geturlasstream (URL); Xmlmapperbuilder Mapperparser=NewXmlmapperbuilder (inputstream, configuration, URL, configuration.getsqlfragments ());          Mapperparser.parse (); } Else if(Resource = =NULL&& URL = =NULL&& Mapperclass! =NULL) {Class<?> Mapperinterface =Resources.classforname (Mapperclass);          Configuration.addmapper (Mapperinterface); } Else {            Throw NewBuilderexception ("A mapper element may be only specify a URL, resource or class, and not more than one."); }        }      }    }  }
View Code

This is simply the end of this, from the next article, will begin to contact the core of MyBatis, but first of all, it is to say Mapper file configuration and use, and through the source further into the core.

 

MyBatis series (vi)---objectfactory, plugins, Mappers Introduction and configuration

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.