Mybatis Plugin拦截器开发过程详解

网友投稿 409 2023-06-23

Mybatis Plugin-开发过程详解

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上面

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 employcEdSNee 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

resultType="mybatis.bean.Employee">

select * from employee where id=#{id}

这里我们拦截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]

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JVM对象创建和内存分配原理解析
下一篇:Spring Boot2.X国际化文件编写配置
相关文章

 发表评论

暂时没有评论,来抢沙发吧~