gpt4 book ai didi

java - Mybatis拦截器执行顺序

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:08 29 4
gpt4 key购买 nike

我在mybatis-config.xml中定义了两个拦截器。

<plugins>
<plugin interceptor="cn.common.interceptor.DbInterceptor"/>
<plugin interceptor="cn.common.interceptor.MybatisInterceptor"/>
</plugins>

实现两个拦截器。

@Intercepts(value = {
@Signature (type=Executor.class, method="update", args = { MappedStatement.class, Object.class })})
public class DbInterceptor implements Interceptor {

private Properties properties;

@Override
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("db interceptor invoke");
return invocation.proceed();
}

@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

@Override
public void setProperties(Properties properties) {
this.properties = properties;
}
}


@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class MybatisInterceptor implements Interceptor {

private Properties properties;

@Override
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("mybatis interceptor invoke");
return invocation.proceed();
}

@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

@Override
public void setProperties(Properties properties) {
this.properties = properties;
}

}

MybatisInterceptor 在我执行更新方法后首先执行。

但是我想让DbInterceptor拦截器先执行,我该怎么办?

最佳答案

MyBatis中,后一个插件将首先执行,所以我认为你只需要更改插件的配置顺序:

更改自

<plugins>
<plugin interceptor="cn.common.interceptor.DbInterceptor"/>
<plugin interceptor="cn.common.interceptor.MybatisInterceptor"/>
</plugins>

<plugins>
<plugin interceptor="cn.common.interceptor.MybatisInterceptor"/>
<plugin interceptor="cn.common.interceptor.DbInterceptor"/>
</plugins>

更多详情请参见CSDN

关于java - Mybatis拦截器执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50868631/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com