首页 > 编程语言 > Mybatis Plugin拦截器开发过程详解
2020
09-23

Mybatis Plugin拦截器开发过程详解

这篇文章主要介绍了Mybatis Plugin拦截器开发过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.Plugin

MyBatis 允许使用插件来拦截的方法调用包括:

  • • Executor (update, query, flushStatements, commit, rollback,getTransaction, close, isClosed)
  • • ParameterHandler (getParameterObject, setParameters)
  • • ResultSetHandler (handleResultSets, handleOutputParameters)
  • • StatementHandler (prepare, parameterize, batch, update, query)

注意;可以通过插件拦截到这四个对象,修改参数等操作:

你必须要知道的类:

  • org.apache.ibatis.plugin.Plugin
  • org.apache.ibatis.reflection.SystemMetaObject

2.使用步骤

实现 Interceptor 接口

三个方法执行顺序

  • setProperties()
  • plugin()
  • intercept()
FirstIntercepter=====>setProperties
FirstIntercepter====>pluginorg.apache.ibatis.executor.CachingExecutor@64485a47
FirstIntercepter====>pluginorg.apache.ibatis.scripting.defaults.DefaultParameterHandler@2f0a87b3
FirstIntercepter====>pluginorg.apache.ibatis.executor.resultset.DefaultResultSetHandler@4fcd19b3
FirstIntercepter====>pluginorg.apache.ibatis.executor.statement.RoutingStatementHandler@2fd66ad3
DEBUG 09-05 11:56:24,696 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:159)
FirstIntercepter:===>intercept
DEBUG 09-05 11:56:24,722 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:159)
DEBUG 09-05 11:56:24,739 <== Total: 1 (BaseJdbcLogger.java:159)
Employee [id=1, lastName=tom, gender=1, email=asd@qq.com, depid=null]

给你的拦截器签名:

/**
* 完成插件签名:
* 告诉MyBatis当前插件用来拦截哪个对象的哪个方法
* type:要拦截的四大类型
* method:拦截那个方法
* args:这个方法的入参
* */
@Intercepts({
@Signature(type=StatementHandler.class,
method="parameterize",
args=java.sql.Statement.class
)
})
public class FirstIntercepter implements Interceptor

mybatis-cfg.xml中配置插件

这里注意配置plugins的标签顺序,以免出错,在environments上面

<!-- plugins 插件的配置 实际上是使用:intercepter原理代理的 -->
<plugins>
<plugin interceptor="mybatis.intercepter.FirstIntercepter">
<property name="param1" value="root"/>
<property name="param2" value="root"/>
</plugin>
</plugins>

3.多个插件的执行

多个插件依次生成目标对象的代理对象,层层包裹,先声明的先包裹;形成代理链

可以理解为:初始化执行

执行log

FirstIntercepter=====>setProperties

MySecondIntercepter====>setProperties:{param1=root}
FirstIntercepter====>pluginorg.apache.ibatis.executor.CachingExecutor@64485a47
MySecondIntercepter====>plugin:org.apache.ibatis.executor.CachingExecutor@64485a47
FirstIntercepter====>pluginorg.apache.ibatis.scripting.defaults.DefaultParameterHandler@2f0a87b3
MySecondIntercepter====>plugin:org.apache.ibatis.scripting.defaults.DefaultParameterHandler@2f0a87b3
FirstIntercepter====>pluginorg.apache.ibatis.executor.resultset.DefaultResultSetHandler@4fcd19b3
MySecondIntercepter====>plugin:org.apache.ibatis.executor.resultset.DefaultResultSetHandler@4fcd19b3
FirstIntercepter====>pluginorg.apache.ibatis.executor.statement.RoutingStatementHandler@2fd66ad3
MySecondIntercepter====>plugin:org.apache.ibatis.executor.statement.RoutingStatementHandler@2fd66ad3
DEBUG 09-05 12:07:01,928 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:159)
MySecondIntercepter====>intercept:public abstract void org.apache.ibatis.executor.statement.StatementHandler.parameterize(java.sql.Statement) throws java.sql.SQLException
FirstIntercepter:===>intercept
DEBUG 09-05 12:07:01,954 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:159)
DEBUG 09-05 12:07:01,968 <== Total: 1 (BaseJdbcLogger.java:159)
Employee [id=1, lastName=tom, gender=1, email=asd@qq.com, depid=null]

4.实现拦截修改参数

sql

<!-- Employee getSelectEmp(Integer id); -->
<select id="getSelectEmp" parameterType="java.lang.Integer"
resultType="mybatis.bean.Employee">
select * from employee where id=#{id}
</select>

这里我们拦截id:

由于ibatis中参数的声明存在与 StatementHandler中所以注意签名

@Intercepts({
@Signature(type=StatementHandler.class,
method="parameterize",
args=java.sql.Statement.class
)
})

业务逻辑intercept方法中

/**
* 1:业务逻辑处理的方法:
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
//在这里可以进行业务逻辑修改
System.out.println("FirstIntercepter:===>intercept"+invocation.getMethod());
 
MetaObject metaObject = SystemMetaObject.forObject(invocation.getTarget());
//拿到target的元数据 StatementHandler==>ParameterHandler===>
//DefaultParameterHandler==>>parameterObject
Object value = metaObject.getValue("parameterHandler.parameterObject");
System.out.println("sql "+value.toString());
//修改完sql语句要用的参数
metaObject.setValue("parameterHandler.parameterObject", 2);
Object object = invocation.proceed();
return object;
}

打印log,

可以看到原来入参为1,现在经过拦截器修改入参为2

DEBUG 09-05 12:36:23,387 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:159)
MySecondIntercepter====>intercept:public abstract void org.apache.ibatis.executor.statement.StatementHandler.parameterize(java.sql.Statement) throws java.sql.SQLException
FirstIntercepter:===>interceptpublic abstract void org.apache.ibatis.executor.statement.StatementHandler.parameterize(java.sql.Statement) throws java.sql.SQLException
sql 1
DEBUG 09-05 12:36:23,418 ==> Parameters: 2(Integer) (BaseJdbcLogger.java:159)
DEBUG 09-05 12:36:23,432 <== Total: 1 (BaseJdbcLogger.java:159)
Employee [id=2, lastName=cat, gender=0, email=qwe@qq.com, depid=null]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧