首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拦截器执行命令

拦截器执行命令
EN

Stack Overflow用户
提问于 2018-06-15 03:13:30
回答 1查看 2.4K关注 0票数 1

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

代码语言:javascript
复制
<plugins>
    <plugin interceptor="cn.common.interceptor.DbInterceptor"/>
    <plugin interceptor="cn.common.interceptor.MybatisInterceptor"/>
</plugins>

执行两个拦截器。

代码语言:javascript
复制
@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在我执行update方法后第一次执行。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-15 05:42:15

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

变出

代码语言:javascript
复制
<plugins>
    <plugin interceptor="cn.common.interceptor.DbInterceptor"/>
    <plugin interceptor="cn.common.interceptor.MybatisInterceptor"/>
</plugins>

代码语言:javascript
复制
<plugins>
    <plugin interceptor="cn.common.interceptor.MybatisInterceptor"/>
    <plugin interceptor="cn.common.interceptor.DbInterceptor"/>
</plugins>

更详细的信息可以在CSDN上找到

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50868631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档