java 自定义拦截器 MyBatis 插件(拦截器)实战(自定义实现拦截器) - \t"
前言:
前面我们分析了 MyBatis 插件(拦截器)的原理,本篇分享 MyBatis 插件(拦截器)的实战。
Mybatis 相关知识传送门
初识 MyBatis 【MyBatis 核心概念】
MyBatis 源码分析–SqlSessionFactory
MyBatis 源码分析–获取SqlSession
MyBatis 源码分析-- getMapper(获取Mapper)
MyBatis 源码分析-- SQL请求执行流程( Mapper 接口方法的执行的过程)
MyBatis 源码分析-- 插件(拦截器)原理
创建自定义 MyBatis 插件(拦截器)
创建自定义 MyBatis 插件(拦截器)需要实现 org.apache.ibatis.plugin.Interceptor 接口,在类上添加 @Intercepts 注解,前文我们已经说了MyBatis 插件(拦截器)默认可以拦截的类型有四种,分别是 Executor、StatementHandler、ParameterHandler、ResultSetHandler,对自定义 插件(拦截器)必须使用 MyBatis 提供的 @Intercepts 注解来表明我们要拦截的是哪一种获几种接口,如下:
@Intercepts({
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class,method = "update",args = {MappedStatement.class, Object.class})
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
@Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class}),
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
})
@Intercepts 注解
MyBatis 使用 @Intercepts 注解来标记一个类是 MyBatis 插件(拦截器)。
package org.apache.ibatis.plugin;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Intercepts {
Signature[] value();
}
@Signature 注解
@Signature 注解标记插件(拦截器)需要拦截哪一个接口的哪个方法。
type:要拦截的接口类型,也就是前面说的四种类型的接口。method:要拦截的接口的方法,例如 Executor 接口的 update 方法。args:要拦截的接口的方法的参数。
package org.apache.ibatis.plugin;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Signature {
Class type();
String method();
Class[] args();
}
自定义MyBatis 插件(拦截器)代码示例
自定义MyBatis 插件(拦截器) MyBatisSqlInterceptor ,统计 SQL 执行消耗的时间,并打印SQL。
@Slf4j
@Component
@Intercepts({
@Signature(type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class,
method = "update",
args = {MappedStatement.class, Object.class}),
})
public class MyBatisSqlInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
//MappedStatement 中有 SQL 语句参数等信息
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object parameter = null;
if (invocation.getArgs().length > 1) {
parameter = invocation.getArgs()[1];
}
long startTime = System.currentTimeMillis();
try {
//继续执行业务方法
return invocation.proceed();
} finally {
//业务方法执行完了才执行 因此就是 SQL 耗时
//得到 SQL
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
String sql = boundSql.getSql();
sql = sql.replace("\n", "")
.replace("\t", "").replace(" ", " ")
.replace("( ", "(").replace(" )", ")") 拼多多帮助力入口
.replace(" ,", ",").replaceAll(" +", " ");
}
}
@Override
public Object plugin(Object target) {
return Interceptor.super.plugin(target);
}
@Override
public void setProperties(Properties properties) {
Interceptor.super.setProperties(properties);
}
}
添加 MyBatis 插件(拦截器)
添加 MyBatis 插件(拦截器)有两种方式,如下:
使用 mybatis-config.xml 文件配置(本篇不讲解)。自定义 MyBatis 插件(拦截器)类上添加 @Component 注解,交给 Spring 管理。
Spring 管理方式如下:
@Configuration
public class MyInterptorConfig implements SmartInitializingSingleton {
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Override
public void afterSingletonsInstantiated() {
sqlSessionFactory.getConfiguration().addInterceptor(new MyBatisSqlInterceptor());
}
}
演示结果:
从结果中可以看到我们得到了 SQL 执行时间和具体的 SQL 语句,实现起来还是比较简单的,我们可以基于此拓展丰富更多的功能。
如有不正确的地方请各位指出纠正。