mybatis plug-in mechanism

A, mybatis introduction of plug-ins

About mybatis plug-in, I think we have used, like on the most commonly used reverse engineering, generation model, dao, xml files, as well as plug-page table structure based on that principle of these plug-ins is kind of how it For example pagination plug-in, it can change how we write the xml file sql statement, we will take everyone together to find a plug-in mechanism mybatis. (Since I am also constantly learning, the text will inevitably there are errors or deficiencies at, but also look correct, paper-based mybatis3.3.0 version), we will focus on these areas

1, an inlet plug, i.e., how the widget is injected into the code inside mybatis

2, plug-in which classes or which can intercept method

3, for example simple principles table plugin

Second, plug the entrance

Before the earlier understanding, we come for some custom code mybatis plugin

. 1  Import org.apache.ibatis.executor.statement.StatementHandler;
 2  Import org.apache.ibatis.plugin *. ;
 . 3  
. 4  Import the java.sql.Connection;
 . 5  Import the java.util.Properties;
 . 6  
. 7  @Intercepts (
 . 8          value = {
 . 9                  @Signature (
 10                          type = StatementHandler. class ,
 . 11                          method = "PREPARE" ,
 12 is                          args = {Connection. class } // PREPARE different versions of the method parameters are not the same, there is a higher version parameter Integer
13                 )
14         }
15 )
16 public class PluginDemo implements Interceptor {
17     @Override
18     public Object intercept(Invocation invocation) throws Throwable {
19         return invocation.proceed();
20     }
21 
22     @Override
23     public Object plugin(Object target) {
24         return Plugin.wrap(target,this);
25     }
26 
27     @Override
28     public void setProperties(Properties properties) {
29 
30     }

 

To customize mybatis plug-in, have to implement Interceptor interface, which has three abstract methods

1, intercept, is the core of this method mybatis, to implement custom logic, basically this transformation method, wherein the invocation parameter method to get the original information and the corresponding parameter reflecting

2, plugin, its role is to generate an intercept each other, that is, the proxy object, such that the proxy object will pass intercept method, usually using the tools provided Plugin mybatis to obtain the proxy object, if has its own unique needs You can customize

3, setProperties, this method is used to set some of the attributes of plug-ins

Which @intercepts annotation class is used to indicate which interception, which methods.

When we use container from the spring mybatis, we usually write code

 

 

 In mybatisConfig.xml file, we have to configure the database connection information, plugins, aliases, mapper file mapping address, etc. (Tips, which are arranged in a certain order, will throw an exception if you do not order configured, the parsing mybatis detailed configuration information can mbatis the dtd file configuration) since the configuration in the configuration file, it must also parsed out, as shown below, in parsing the XmlConfigBuild

 

After parsing, the widget information is stored in a configuation which InterceptorChain set, the operating cycle is mybatis configuation in a single embodiment, which is responsible for storing all the configuration information

 

 

 

 

 Eventually, the plug-in information mybatis complete the configuation injected into the inside.

Two, mybaits plug-in which classes or methods which can intercept

In normal development process, we basically by first obtaining sqlSession, if not using a custom configuration, the default implementation is sqlsession defaultSqlSession, in a way it's kind of implied merged plug,

Which will get to the executor, this class is obtained by configuation,

 

 The final method is the integration of plug-figure red box code. It will generate a proxy class for the original executor class. So that when you perform some method executor class, such as Query, update method, Mr. will to the corresponding proxy object, myabtis uses a dynamic proxy jdk after the agent, you perform Query executor class, will be automatically transferred when the update method you received inside a custom plug-intercept method can also be understood to cover the original method.

Through this, we can probably guess that, mybatis plug to block the class, in large part because there is generated in the configuation class, do not take everyone roundabout, look at screenshots

Interception statementHandler interface implementation class, it is achieved by routing routingStatementHandler, finally directed to class prepareStatementHandler

Interception parameterHandler. This interface is used to package the parameters, the parameter is set to the final inside sql

 

Intercept ResultSetHandler interface, which is mainly used to process the results returned by running sql

 

 

 Summary, mybatis can intercept the class divided into four categories

A, executor, executor class can be said that the whole process of implementation of sql, such as assembly parameters, sql transformation, the results of treatment, more extensive, but not much practical use

Two, StatementHandler, this process is performed in sql, sql can be performed to obtain, it can be used to transform sql, such as paging, sub-table, the most common type of interception

Three, paremeterHandler, to intercept the sql parameters, custom parameters can be assembled rules

Four, resultHandler, this used to process the result

Third, the simple plug-in version of the sub-table

 1 import org.apache.ibatis.executor.statement.StatementHandler;
 2 import org.apache.ibatis.mapping.BoundSql;
 3 import org.apache.ibatis.plugin.*;
 4 import org.apache.ibatis.reflection.MetaObject;
 5 import org.apache.ibatis.reflection.SystemMetaObject;
 6 
 7 import java.sql.Connection;
 8 import java.util.Properties;
 9 
10 @Intercepts(
11         value = {
12                 @Signature(
13                         type = StatementHandler.class,
14                         method = "prepare",
15                         args = {Connection.class}
16                 )
17         }
18 )
19 public class PluginDemo implements Interceptor {
20     @Override
21     public Object intercept(Invocation invocation) throws Throwable {
22 
23         /**
24          * 获取被拦截的目前类,在这里是拦截了statementHandler,所有目前类也就是它
25          * 通过这个类我们可以拿到待执行的sql语句,通常使用mataObject工具类来获取
26          * 关于这个工具类,大家可自行了解,个人认为这个工具类很强大
27           */
28         StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
29         MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
30         /**
31          * 先解释下为什么写成delegate.boundSql就可以拿到boundSql类
32          * 从前面也可以得知,statementHandler的默认实现是routingStatementHandler。
33          * 这个类有一个属性statementHandler,属性名就叫delegate,而这个属性的默认实现又是preparedStatementHandler
34          * 后面这个类又有属性boundSql,所以,最终形成的写法就是delegate.boundSql。
35          * 所以这也体现了MetaObject工具类的强大,可以通过实例传参,就可以根据属性名获取对应属性值
36          */
37         BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
38 
39         // 待执行的sql,在这里也就是预编译后的sql,即参数位都是?号
40         String sql = boundSql.getSql();
41         /**
42          * 既然拿到了预编译后的sql,那就可以按照你自己的想法为所欲为,如分页,按年分表等等
43          * 分表的话,个人推荐druid的sql解析器,我认为还是不错的,大家可以自行了解
44          * 最后改造完sql,别忘了把它设置回去
45          * metaObject.setValue("delegate.boundSql.sql",sql);
46          *  invocation.proceed,即原始方法的执行
47          *  注意点就是,因为mybatis插件采用的是代理模式,所以如果存在多个插件,会形成多个代理
48          *  你如果要拿到最原始的对象,还得进一步进行分解
49          *  如:while(metaObject.getValue(""h) != null){
50          *      Object obj = metaObject.getValue("h");
51          *       ....
52          *  }
53          */
54         return invocation.proceed();
55     }
56 
57     @Override
58     public Object plugin(Object target) {
59         return Plugin.wrap(target,this);
60     }
61 
62     @Override
63     public void setProperties(Properties properties) {
64 
65     }
66 }

 

 -----------------------------------------------------------------------------------------------------------------------------分界线-------------------------------------------------------------------------------------

以上就是全部内容

Guess you like

Origin www.cnblogs.com/qm-article/p/11785350.html