0%

mybatis拦截器

mybatis拦截器

mybatis拦截器也可以叫做插件,mybatis允许用户自定义拦截器对SQL语句执行过程中的某一个点进行拦截。默认mybatis允许拦截Mybatis中的四大核心对象:Executor中的方法、ParameterHandler的方法、ResultSetHandler的方法以及StatementHandler中的方法

  • Executor中的update()、query()、flushStatements()、commit()、rollback()、getTransaction()、close()、isClose()方法,拦截执行器的方法

  • ParameterHandler中的getParameterObject()、setParameters()方法,拦截参数的处理

  • ResultSetHandler中的handlerResultSets()、handlerOutputParameters()方法,拦截结果集的处理

  • StatementHandler中的prepare()、parameterize()、batch()、update()、query()方法,拦截sql语句的构建

通过动态代理,为需要拦截的接口生成代理对象以实现接口方法拦截功能,当执行所拦截的方法时,就会进入拦截方法

Interceptor接口

mybatis使用拦截器需要实现Interceptor接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface Interceptor {
// 覆盖所拦截对象的原有方法,执行拦截逻辑的方法
Object intercept(Invocation var1) throws Throwable;
// target为被拦截对象,作用是给被拦截对象生成一个代理对象
default Object plugin(Object target) {
// Plugin实现了InvocationHandler接口,wrap方法就是执行Proxy.newProxyInstance(type.getClassLoader(),interfaces,new Plugin(target, interceptor, signatureMap))
//使用JDK的动态代理,给target对象创建一个delegate代理对象,以此来实现方法拦截和增强功能,它会回调intercept()方法,使用当前拦截器来包装目标对象
// 调用一次wrap方法产生一个代理对象,如果有第二个插件,会将第一个带对象传递给wrap方法,生成第二个代理对象,再调用Invocation.proceed会从最后一个代理对象的invoke方法运行到第一个代理对象的invoke方法,直至真实方法
return Plugin.wrap(target, this);
}
// 根据配置初始化Interceptor对象,只在插件初始化的时候调用一次
default void setProperties(Properties properties) {
}
}

@Intercepts注解

用户自定义的拦截器除了实现Interceptor接口,还需要使用@Intercepts和@Signature注解进行标识

@Intercepts注解中指定了一个@Signature注解列表

1
2
3
4
5
6
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Intercepts {
Signature[] value();
}

每个@Signature注解中标识了该插件需要拦截的方法信息

1
2
3
4
5
6
7
8
9
10
11
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Signature {
//拦截的对象类型
Class<?> type();
//拦截的方法
String method();
//被拦截方法的参数列表
Class<?>[] args();
}

使用示例

1
2
3
@Intercepts(
@Signature(type = StatementHandler.class,method = "prepare",args = {})
)

如何进行拦截的

就是在创建对象实例时生成代理类,采用责任链模式,由interceptorChain来进行生成代理对象并返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Configuration 类中的方法

// 创建ParameterHandler
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}

// 创建ResultSetHandler
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
return resultSetHandler;
}

// 创建StatementHandler
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}

// 创建Executor
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}

工具类MetaObject

在编写插件的时候经常会使用到Mybatis提供的一个工具类MetaObject,可以有效地读取或修改一些重要对象的属性

1
2
3
4
// 获取对象属性值,支持OGNL
public Object getValue(String name)
// 修改对象属性值,支持OGNL
public void setValue(String name, Object value)

欢迎关注我的其它发布渠道