1.简介
MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
这些类中方法的细节可以通过查看每个方法的签名来发现,或者直接查看 MyBatis 的发行包中的源代码。 假设你想做的不仅仅是监控方法的调用,那么你应该很好的了解正在重写的方法的行为。 因为如果在试图修改或重写已有方法的行为的时候,你很可能在破坏 MyBatis 的核心模块。 这些都是更低层的类和方法,所以使用插件的时候要特别当心。
1.1实现
1)实现 Interceptor 接口,通过@Intercepts指定拦截位置
@Intercepts({@Signature( type= Executor.class, method = "update", args = {MappedStatement.class,Object.class})}) public class ExamplePlugin implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { return invocation.proceed(); } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties) { } }
2)配置mybatis-config.xml
<plugins> <plugin interceptor="org.mybatis.example.ExamplePlugin"> <property name="someProperty" value="100"/> </plugin> </plugins>
2.实例
对StatementHandler.query方法进行拦截,记录慢SQL
package com.siyuan.dao.mybatis; import java.sql.Statement; import java.util.Properties; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler; import org.apache.log4j.Logger; @Intercepts(@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class })) public class SqlStatisticInterceptor implements Interceptor { private static final Logger LOGGER = Logger.getLogger(SqlStatisticInterceptor.class); // 慢SQL阀值 private int slowSQLThreshold = 3000; @Override public Object intercept(Invocation invocation) throws Throwable { long start = System.currentTimeMillis(); Object result = invocation.proceed(); long timeSpent = System.currentTimeMillis() - start; StatementHandler handler = (StatementHandler) invocation.getTarget(); String sql = handler.getBoundSql().getSql().trim(); LOGGER.debug("spent [" + timeSpent + "]ms for :\n" + sql); if (timeSpent >= slowSQLThreshold) { LOGGER.warn("slow sql which costs [" + timeSpent + "]ms has been found:\n" + sql); } return result; } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { if (properties.get("slowSQLThreshold") == null) { return; } this.slowSQLThreshold = Integer.parseInt((String) properties.get("slowSQLThreshold")); } }
2) mybatis-config.xml
<plugin interceptor="com.qfang.dao.mybatis.SqlStatisticInterceptor"> <property name="slowSQLThreshold" value="1000"/> </plugin>
3.原理
1)Interceptor.plugin方法将在创建Executor,ParameterHandler,ResultSetHandler,StatementHandler实例时被调用替代mybatis框架默认创建的实例
2)Plugin实现InvocationHandler,wrap方法通过Proxy创建Executor,ParameterHandler,ResultSetHandler,StatementHandler实例的代理类
public static Object wrap(Object target, Interceptor interceptor) { Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor); Class<?> type = target.getClass(); Class<?>[] interfaces = getAllInterfaces(type, signatureMap); if (interfaces.length > 0) { return Proxy.newProxyInstance( type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)); } return target; }
4.参考资料
http://www.mybatis.org/mybatis-3/zh/configuration.html#plugins
相关推荐
idea mybatis插件直接可用。。直接放到 C:\Users\Administrator\.IntelliJIdea2018.1
idea15 mybatis插件
Idea mybatis 插件 ,xml 各种提示 ,dao 层可以直接在 xml 文件生成方法,也可直接跳转到 xml
idea的mybatis插件(MAC通用) 在mac上,找到idea右键,显示包内容--Contents/plugins,拷到里面,重启idea即可。
Mybatis 脱敏插件 最近在研究 Mybatis 的插件,所以考虑能不能在 ORM 中搞一搞脱敏,所以就尝试了一下,这里分享一下思路。借此也分享一下 Mybatis 插件开发的思路。 2.1 Mybatis 插件接口 Mybatis 中使用...
很好用的mybatis插件
myeclipse手动安装mybatis插件安装包,myeclipse安装此插件后可以自动生成mybatis配置文件。
free-idea-mybatis是一个提高mybatis编码的插件。实现了dao代码跳转到mapper,mapper跳转回dao,mapper文件、statement查询自动生成功能。这里提供两个版本free-idea-mybatis-2019.12.18 和free-idea-mybatis-2018....
MyBatis插件,比如放到F盘根目录,改一下要生成的表,cmd运行生成即可
eclipse中mybatis插件mybatipse离线安装包。在线安装总会中断,使用此离线安装包,解压放于eclipse的dropins目录下,重启eclipse即可! 功能: 1:要查找某一个方法 在dao接口中某一个方法中 按住 Ctrl键 鼠标指到...
# MybatisLog sql日志 Free-Mybatis 插件
超级好用的mybatis 插件 ,可以很方便的实现mapper 和配置文件之间的切换,可以提示错误信息,代码生成等
最新mybatis插件破解版
spring的插件,包含spring的IOC,DI, AOP,Mybatis的插件
IntelliJ IDEA 集成Mybatis插件,insert、update、query生成 主键查询生成XXXById
目前IDEA的mybatis插件有两款,其中一款是收费的,这款是免费的,导入IDEA插件即可使用。
近期idea mybatis插件已经升级至3.x,class文件混淆,基本无破解希望,后来,mybatis把老版本的回退下载入口也疯掉了,这里提供了老版本2.9.2的安装包、mybatis秘钥文件。 目前本人的可用,环境:idea 2017.2.5 、 ...
mybatis插件包,mybatis-3.4.6。。。。。。。。。。。。
基于ssm框架下利用mybatis插件pageHelper进行的分页查询源码。
mybatis的插件!适用于idea2017和2018.1.5.喜欢拿走!!