There is a plugin configuration in the configuration of MyBatis, which can be interpreted as "plug-in" according to its name, which can be understood as "interceptor" in essence. The term "interceptor" is not unfamiliar, and there are "interceptors" in many frameworks. What's the use of this plugin? What's the use of interceptors alive? You can think of how the interceptor is implemented. Plugin uses one of the most important features in Java-dynamic proxies. So this plugin can be understood as, when invoking a method, I "intercept" its method to do something I want it to do. It can intercept the following methods:
In the official document there is such a sentence:If You attempt to modify or override the behaviour of a given method, you ' re likely to break the core of MyBatis. Use the custom plugin interceptor with caution, as it may modify the mybatis core stuff. Implementing a custom plugin we need to implement the Interceptor interface. This type of annotation is not @intercepts.
1 PackageDay_8_mybatis.util;2 3 ImportJava.util.Iterator;4 ImportJava.util.Map;5 Importjava.util.Properties;6 7 ImportOrg.apache.ibatis.plugin.Interceptor;8 Importorg.apache.ibatis.plugin.Intercepts;9 Importorg.apache.ibatis.plugin.Invocation;Ten ImportOrg.apache.ibatis.plugin.Plugin; One Importorg.apache.ibatis.plugin.Signature; A - /** - * @authorTurbo the * - * October 25, 2016 - */ - @Intercepts ({ + @Signature ( -Type = Map.class, +method = "Get", Aargs = {Object.class} at )}) - Public classExamplepluginImplementsInterceptor { - - /*This method is used to implement the interception logic - * @see org.apache.ibatis.plugin.interceptor#intercept (org.apache.ibatis.plugin.Invocation) - */ in @Override - PublicObject Intercept (invocation invocation)throwsThrowable { to + return"Exampleplugin"; - } the * /*use the current interceptor to implement a proxy for the target object (the dynamic proxy for the internal implementation of Java) $ * @see org.apache.ibatis.plugin.interceptor#plugin (java.lang.Object)Panax Notoginseng */ - @Override the PublicObject Plugin (object target) { + returnPlugin.wrap (Target, This); A } the + /*This method, like the setproperties in the custom object factory described in the previous section, initializes the configuration file by configuring the property to pass parameters to this method and invoke it. - * @see org.apache.ibatis.plugin.interceptor#setproperties (java.util.Properties) $ */ $ @Override - Public voidSetProperties (Properties properties) { -Iterator Iterator =Properties.keyset (). iterator (); the while(Iterator.hasnext ()) { -String KeyValue =string.valueof (Iterator.next ());Wuyi System.out.println (Properties.getproperty (KeyValue)); the } - } Wu -}
Don't forget to register your custom plugin in the Mybatis-config.xml configuration file. (There are some legacy codes in the configuration below that are configured in the previous two sections and can be selectively ignored.) )
1<?xml version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE Configuration3Public "-//mybatis.org//dtd Config 3.0//en"4"Http://mybatis.org/dtd/mybatis-3-config.dtd" >5 6<configuration>7<!--Note that the order of configuration for each property in the config should be: properties,settings,typealiases,typehandlers,objectfactory, Objectwrapperfactory,reflectorfactory,plugins,environments,databaseidprovider,mappers)--8<properties>9<property name= "Driver" value= "Com.mysql.jdbc.Driver"/>Ten<property name= "url" value= "Jdbc:mysql://localhost:3306/test"/> One<property name= "username" value= "root"/> A<property name= "password" value= "0000"/> -</properties> -<!-- the<typeHandlers> -<typehandler handler= "Day_8_mybatis.util.ExampleTypeHandler" javatype= "Java.util.Date" jdbctype= "VARCHAR"/> -</typeHandlers> -<objectfactory type= "Day_8_mybatis.util.ExampleObjectFactory" > +<property name= "Someproperty" value= "/>" -</objectFactory> +- A<plugins> at<plugin interceptor= "Day_8_mybatis.util.ExamplePlugin" > -<property name= "Someproperty" value= "/>" -</plugin> -</plugins> -<environmentsdefault= "Development" > -<environment id= "Development" > in<transactionmanager type= "JDBC"/> -<datasource type= "Pooled" > to<property name= "Driver" value= "${driver}"/> +<property name= "url" value= "${url}"/> -<property name= "username" value= "${username}"/> the<property name= "Password" value= "${password}"/> *</dataSource> $</environment>Panax Notoginseng</environments> -<mappers> the<mapper resource= "Day_8_mybatis/mapper/usermapper.xml"/> +<mapper resource= "Day_8_mybatis/mapper/notemapper.xml"/> A</mappers> the +</configuration> - $
Client Test Code:
1 PackageDay_8_mybatis;2 3 Importjava.io.IOException;4 ImportJava.util.HashMap;5 ImportJava.util.Map;6 7 Importorg.apache.ibatis.session.SqlSession;8 9 ImportDay_8_mybatis.util.ExamplePlugin;Ten Importday_8_mybatis.util.SessionFactory; One A /** - * Client - * @authorTurbo the * - * October 25, 2016 - */ - Public classMain { + - /** + * @paramargs A * @throwsIOException at */ - Public Static voidMain (string[] args)throwsException { -String resource = "Day_8_mybatis/mybatis-config.xml";//get mybatis configuration file path -Sqlsession sqlsession = sessionfactory.getsqlsession (Resource);//constructs sqlsession from the Sessionfactory tool class (This tool class constructs itself as a sessionfactory in the util package) - -Map map =NewHashMap (); inMap = (map)Newexampleplugin (). plugin (map); -System.out.println (Map.get ("")); to + } - the}
At this point, we have a simple understanding of the MyBatis in the plugin. It is interesting to see the plugin method that we call in line 29th of the client test code. Called the static method of the plugin class wrap (Object target, Interceptor intercpetor), tracing the method will find that this method is the dynamic agent of Java.
1 Public StaticObject Wrap (object target, Interceptor Interceptor)2 {3Map Signaturemap =Getsignaturemap (Interceptor);4Class type =Target.getclass ();5Class interfaces[] =getallinterfaces (type, signaturemap);6 if(Interfaces.length > 0)7 returnProxy.newproxyinstance (Type.getclassloader (), interfaces,NewPlugin (target, Interceptor, Signaturemap)); Returns the proxy class instance8 Else9 returnTarget;Ten}
Dynamic proxies are important, and reflection is important. It is important to understand the dynamic proxies and reflections repeatedly, which helps us to understand many of the framework source code. This article simply understands, does not do too much in-depth.
MyBatis's simple understanding of plugin