首页 > 其他 > 详细

Mybaits简单的插件写法

时间:2020-08-21 01:09:05      阅读:105      评论:0      收藏:0      [点我收藏+]

 

Mybatis插件的书写流程:

1.编写Interceptor接口的实现类
2.使用@Intercepts注解完成插件签名(拦截那个类的那个方法)

@Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})})
//那个类(四大对象),的哪个方法及方法形参类型

3.将写好的插件注册到全局配置文件中(mybatis-config.xml)

<plugins>
    <plugin interceptor="全类名" >
        <property name="username" value="root" />
    </plugin>
</plugins>

 

 

支持拦截的方法

  • 执行器Executor(update、query、commit、rollback等方法);
  • 参数处理器ParameterHandler(getParameterObject、setParameters方法);
  • 结果集处理器ResultSetHandler(handleResultSets、handleOutputParameters等方法);
  • SQL语法构建器StatementHandler(prepare、parameterize、batch、update、query等方法);


下面是一个简单的插件:

功能:拦截ParameterHandler的setParameters,目的是在处理查询sql参数时,改变一下原来的where条件参数. 

比如:select * from t_user where id=1, 永久把id=1改为3,就是说不管页面传的id值是多少,一直查询id=3的数据

 

package com.cc8w.plugin;


import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.Statement;
import java.util.Properties;

/**
 * 练习mybatis插件,熟悉其原理
 *
 * 一.mybatis插件实现步骤
 * 1.编写Interceptor接口的实现类
 * 2.使用@Intercepts注解完成插件签名(拦截那个类的那个方法)
 * 3.将写好的插件注册到全局配置文件中(mybatis-config.xml)
 *
 */
@Intercepts({
        @Signature(type = StatementHandler.class, method = "parameterize", args = {java.sql.Statement.class})
})
public class MybaitsPlugin implements Interceptor {


    /**
     * intercept 拦截目标对象的目标方法执行;
     *
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //需求:获取查询参数,更改查询参数为id=3

        Object target = invocation.getTarget();//当前拦截到的目标对象
        //1.拿到StatementHandler==>ParameterHandler==>parameterObject
        MetaObject metaObjetc = SystemMetaObject.forObject(target); //可以拿到目标对象的元数据
        //2.操作MetaObject可以很方便的得到目标对象的所有值
        Object value = metaObjetc.getValue("ParameterHandler.parameterObject");//获取那个属性的值
        System.out.println("sql语句用的参数是:"+value);
        //3.根据需求把这个值改为3,即可
        metaObjetc.setValue("ParameterHandler.parameterObject",3);//设置那个属性的值


        Object proceed = invocation.proceed();//放行语句,执行目标方法
        return proceed;
    }

    /**
     * 包装目标对象:为目标对象创建一个代理对象
     *
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        //借助Plugin类的wrap方法,来使用当前Interceptor包装我们的目标对象
        Object wrap = Plugin.wrap(target,this);
        //返回当前target创建的动态代理
        return wrap;
    }

    /**
     * 注册时:设置一些配置参数
     *
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {

    }
}

 

mybatis-config.xml中配置

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
        <plugin interceptor="com.cc8w.plugin.MybaitsPlugin"></plugin>
    </plugins>

 

Mybaits简单的插件写法

原文:https://www.cnblogs.com/fps2tao/p/13538833.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!