Mybatis Plugin * 开发过程详解
作者:---WeiGeH 时间:2021-07-15 22:30:18
这篇文章主要介绍了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]
来源:https://www.cnblogs.com/dgwblog/p/9591499.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
小议Java中final关键字使用时的注意点
C#(.net)水印图片的生成完整实例
Java抽象类与接口区别详解
![](https://img.aspxhome.com/file/2023/5/76265_0s.png)
Android中微信小程序支付倒计时功能
![](https://img.aspxhome.com/file/2023/9/91659_0s.gif)
Java反射获取class对象方式解析
JAVA中Comparable接口和自定义比较器示例讲解
android多媒体类VideoView使用方法详解
![](https://img.aspxhome.com/file/2023/6/89516_0s.jpg)
servlet上传文件实现代码详解(四)
理解Java当中的回调机制(翻译)
![](https://img.aspxhome.com/file/2023/0/89610_0s.png)
秒懂Java枚举类型(enum)
java中LinkedList使用迭代器优化移除批量元素原理
![](https://img.aspxhome.com/file/2023/0/66180_0s.png)
SpringBoot优雅地实现全局异常处理的方法详解
c#网络唤醒功能实现
![](https://img.aspxhome.com/file/2023/3/84443_0s.jpg)
C# 实现QQ式截图功能实例代码
![](https://img.aspxhome.com/file/2023/2/89782_0s.png)
Android 单双击实现的方法步骤
使用SpringDataJpa创建中间表
Mybatis全局配置及映射关系的实现
![](https://img.aspxhome.com/file/2023/7/72307_0s.png)
mybatis框架xml下trim中的prefix与suffix等标签的用法
Spring实例化bean的四种方式详解
![](https://img.aspxhome.com/file/2023/0/89560_0s.png)
如何在Redis中实现分页排序查询过程解析
![](https://img.aspxhome.com/file/2023/0/92660_0s.jpg)