MyBatis custom plug-in development

Plug principle:

1. Each created out of objects not directly return

 interceptorChain.pluginAll(parameterHandler);

2. Get all the Interceptor (Interceptor) (plug-ins need to implement interfaces)

 调用interceptor.plugin(target);返回target包装后的对象

3. The plug-in mechanism, we can use the plug-in for the target object is to create a proxy object, AOP (aspect-oriented)

  • Our plugin can create a proxy object for the four objects
    proxy object can be intercepted four objects each execution.
public Object pluginAll(Object target) {
	for (Interceptor interceptor : interceptors) {
  		target = interceptor.plugin(target);
  	}
   	return target;
}

To customize the interceptor:
1. write blocker

public class MyFirstPlugin implements Interceptor {

   /**
    * 拦截目标对象方法的执行
    * @param invocation
    * @return
    * @throws Throwable
    */
   @Override
   public Object intercept(Invocation invocation) throws Throwable {
       System.out.println("MyFirstPlugin...intercept"+invocation.getMethod());
       //动态的改变一下sql运行的参数,以前1号学生,实际查询5号学生
       Object target = invocation.getTarget();
//        System.out.println("当前拦截到的对象"+invocation.getTarget());

       //拿到StatementHandler==>ParameterHandler==>parameterObject
       //拿到target的元数据
       MetaObject metaObject = SystemMetaObject.forObject(target);
       Object value = metaObject.getValue("parameterHandler.parameterObject");
//        System.out.println("sql语句用的参数是"+value);
       //修改完sql语句要执行的参数
       metaObject.setValue("parameterHandler.parameterObject",5);
       //放行的方法
       Object proceed = invocation.proceed();

       return proceed;
   }

   /**
    * 包装目标对象,将增强的对象和拦截器包装起来并返回
    * @param target
    * @return
    */
   @Override
   public Object plugin(Object target) {
       System.out.println("MyFirstPlugin..plugin:mybatis将要包装的对象"+target);
       //我们可以借助Plugin的wrap方法来使用当前Interceptor包装我们的目标对象
       Object wrap = Plugin.wrap(target, this);
       //返回当前target创建的动态代理
       return wrap;
   }

   /**
    * 将插件注册时的property属性
    * @param properties
    */
   @Override
   public void setProperties(Properties properties) {
//        System.out.println("插件配置的信息");
   }
}

2. Write Signature comment

/**
* 完成插件签名:
*      告诉Mybatis当前插件用来拦截哪个对象的哪个方法
*/
@Intercepts(
      {
          @Signature(type = StatementHandler.class,
          method = "parameterize",args = java.sql.Statement.class)
      })

3. Configuration

<plugins>
  	  <!-- 分页插件-->
      <plugin interceptor="com.github.pagehelper.PageInterceptor">
          <!--&lt;!&ndash; config params as the following &ndash;&gt;-->
          <!--<property name="dialect" value="mysql"/>-->
          <property name="reasonable" value="true"/>
          <!--<property name="dialect" value="mysql"/>-->
      </plugin>
      <plugin interceptor="com.itt.plugins.MyFirstPlugin">
          <property name="username" value="root"/>
          <property name="password" value="123456"/>
      </plugin>
  </plugins>
  • test
Published 47 original articles · won praise 34 · views 8867

Guess you like

Origin blog.csdn.net/weixin_42893085/article/details/105242467