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> ... <objectfactory type= "Org.mybatis.example.ExampleObjectFactory" > <property name= "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> <plugin interceptor= "Org.mybatis.example.ExamplePlugin" > <property name= " 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: by resource- <mapper resource= "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, this way you must ensure that the interface is the same name as the Mapper file (case-insensitive), and I have an interface Userdao here. That means that the mapper file is userdao.xml <mapper class= "Com.dy.dao.UserDao"/>-- <!--The third way, directly specifying the package, automatically scanning , with method two <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 Resolution */private void Objectfactoryelement (XNode context) throws Exception {if (context! = null) { Read the value of the Type property, then instantiate the Objectfactory, and set the configuration//to this, simply talk about the configuration of this object, in fact, it is mainly stored in the MyBatis String type = Context.getstringattribute ("type"); Read the value of the Propertie, can be configured as needed, mybatis the default implementation of Objectfactory does not use properties properties Properties = Context.getchildrenasprop Erties (); Objectfactory factory = (objectfactory) resolveclass (type). newinstance (); Factory.setproperties (properties); Configuration.setobjectfactory (Factory); }}/** * Plugins node Resolution */private void Pluginelement (xnode parent) throws Exception {if (parent! = NULL) { For (XNode Child:parent.getChildren ()) {String interceptor = Child.getstringattribute ("Interceptor"); Properties Properties = Child.getchildrenasproperties (); Thus, we define a interceptor, we need to achieve interceptor, here is not specific, will be explained in detail in the future interceptor INTERCEptorinstance = (Interceptor) resolveclass (Interceptor). Newinstance (); Interceptorinstance.setproperties (properties); Configuration.addinterceptor (interceptorinstance); }}}/** * mappers node resolution * This is one of the core of MyBatis, here is a brief introduction, which will be analyzed in the next article */private void Mapperelement (xnode parent) Throws Exception {if (parent! = null) {for (XNode Child:parent.getChildren ()) {if ("package". Equals (c Hild.getname ())) {//If the child node of the Mappers node is a package, then scan the file under the document and inject the configuration String 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 an if (resource! = null && URL = = NULL && Mapperclass = null) {Errorcontext.instAnce (). resource (Resource); InputStream InputStream = resources.getresourceasstream (Resource); The mapper mapping file is parsed through Xmlmapperbuilder xmlmapperbuilder mapperparser = new Xmlmapperbuilder (InputStream, Configurat Ion, Resource, configuration.getsqlfragments ()); Mapperparser.parse (); } else if (resource = = NULL && URL! = NULL && Mapperclass = = null) {errorcontext.instance (). Re Source (URL); InputStream inputstream = resources.geturlasstream (URL); Xmlmapperbuilder mapperparser = new Xmlmapperbuilder (inputstream, configuration, URL, configuration.getsqlfragments () ); Mapperparser.parse (); } else if (resource = = NULL && URL = = NULL && Mapperclass! = null) {class<?> Mapperinter face = Resources.classforname (Mapperclass); Configuration.addmapper (Mapperinterface); } else {throw new Builderexception ("A mapper element maY only specify a URL, resource or class, and not more than one. ");}}}}
/** * objectfactory node Resolution */private void Objectfactoryelement (XNode context) throws Exception {if (context! = null) { Read the value of the Type property, then instantiate the Objectfactory, and set the configuration//to this, simply talk about the configuration of this object, in fact, it is mainly stored in the MyBatis String type = Context.getstringattribute ("type"); Read the value of the Propertie, can be configured as needed, mybatis the default implementation of Objectfactory does not use properties properties Properties = Context.getchildrenasprop Erties (); Objectfactory factory = (objectfactory) resolveclass (type). newinstance (); Factory.setproperties (properties); Configuration.setobjectfactory (Factory); }}/** * Plugins node Resolution */private void Pluginelement (xnode parent) throws Exception {if (parent! = NULL) { For (XNode Child:parent.getChildren ()) {String interceptor = Child.getstringattribute ("Interceptor"); Properties Properties = Child.getchildrenasproperties (); Thus, we define a interceptor, we need to achieve interceptor, here is not specific, will be explained in detail in the future interceptor INTERCEptorinstance = (Interceptor) resolveclass (Interceptor). Newinstance (); Interceptorinstance.setproperties (properties); Configuration.addinterceptor (interceptorinstance); }}}/** * mappers node resolution * This is one of the core of MyBatis, here is a brief introduction, which will be analyzed in the next article */private void Mapperelement (xnode parent) Throws Exception {if (parent! = null) {for (XNode Child:parent.getChildren ()) {if ("package". Equals (c Hild.getname ())) {//If the child node of the Mappers node is a package, then scan the file under the document and inject the configuration String 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 an if (resource! = null && URL = = NULL && Mapperclass = null) {Errorcontext.instAnce (). resource (Resource); InputStream InputStream = resources.getresourceasstream (Resource); The mapper mapping file is parsed through Xmlmapperbuilder xmlmapperbuilder mapperparser = new Xmlmapperbuilder (InputStream, Configurat Ion, Resource, configuration.getsqlfragments ()); Mapperparser.parse (); } else if (resource = = NULL && URL! = NULL && Mapperclass = = null) {errorcontext.instance (). Re Source (URL); InputStream inputstream = resources.geturlasstream (URL); Xmlmapperbuilder mapperparser = new Xmlmapperbuilder (inputstream, configuration, URL, configuration.getsqlfragments () ); Mapperparser.parse (); } else if (resource = = NULL && URL = = NULL && Mapperclass! = null) {class<?> Mapperinter face = Resources.classforname (Mapperclass); Configuration.addmapper (Mapperinterface); } else {throw new Builderexception ("A mapper element maY only specify a URL, resource or class, and not more than one. ");}}}}
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 [GO]