溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis攔截器及不生效如何解決

發(fā)布時(shí)間:2023-04-07 11:45:57 來源:億速云 閱讀:166 作者:iii 欄目:開發(fā)技術(shù)

這篇“mybatis攔截器及不生效如何解決”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“mybatis攔截器及不生效如何解決”文章吧。

背景:

在一些需求下,使用攔截器會(huì)大大簡(jiǎn)化工作量也更加靈活:

  • 在項(xiàng)目中,要更新數(shù)據(jù)表的審計(jì)字段,比如 create_time, creator, update_time, updator, 這些字段,如果每一個(gè)表對(duì)應(yīng)的mapper 都去寫一次,或每一個(gè)方法都去更新一下,這個(gè)工作量非常大并且不太友好,并且不夠優(yōu)雅。

  • 記錄一些日志,比如執(zhí)行sql時(shí)侯,要打印每一個(gè)sql執(zhí)行了多久,那就要記錄sql執(zhí)行前的時(shí)間戳,執(zhí)行后的時(shí)間戳,得到其執(zhí)行時(shí)間,再打印。

  • 等等場(chǎng)景

在這些場(chǎng)景下,使用攔截器肯定會(huì)更加靈活且方法。

mybatis攔截器怎樣做

  • 定義一個(gè)攔截器

  • 把這個(gè)攔截器交給spring容器管理

  • 如果項(xiàng)目里面使用了 com.github.pagehelper.PageInterceptor 攔截器可能會(huì)無效,則需要再定義一個(gè) MybatisInterceptorAutoConfiguration

根據(jù)以上三點(diǎn),進(jìn)行詳細(xì)說明

定義一個(gè)攔截器

簡(jiǎn)單示意一下怎樣寫。。。具體業(yè)務(wù)肯定不止這樣子的

一個(gè)攔截器,主要是實(shí)現(xiàn) Interceptor 這個(gè)接口,實(shí)現(xiàn)這個(gè)接口下的三個(gè)方法。
然后在這個(gè)實(shí)現(xiàn)類加上 @Component 注解,就交給 spring容器管理了,所以1,2是一起的

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
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.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
 
import java.util.Properties;
 
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
@Component
@Intercepts({
        @Signature(
                method = "query",
                type = Executor.class,
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        ),
        @Signature(
                method = "query",
                type = Executor.class,
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
        ),
        @Signature(
                type = Executor.class,
                method = "update",
                args = {MappedStatement.class, Object.class}
        )
})
public class LogInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info("begin >>>>>>>>>");
        Object rest = invocation.proceed();
        log.info("end >>>>>>>>>");
        return rest;
    }
 
    @Override
    public Object plugin(Object o) {
        return Plugin.wrap(o, this);
    }
 
    @Override
    public void setProperties(Properties properties) {
 
    }
}

定義一個(gè) MybatisInterceptorAutoConfiguration

為什么要有這么一個(gè)類呢,主要是因?yàn)槿绻愕哪K里面引用了 com.github.pagehelper.PageInterceptor,你自定義的攔截器會(huì)無效,是因?yàn)閙ybatis的攔截器這就是一個(gè)責(zé)任鏈,但是如果執(zhí)行了 PageInterceptor,這個(gè)Interceptor比較特別,它自己執(zhí)行完,就不往下傳遞鏈條了,即這個(gè)鏈會(huì)在它這里斷開。所以加了其它的interceptor, 它們必須在 PageInterceptor 之前執(zhí)行。

具體可見代碼:

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
 
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
 
 
@Configuration
// 這一行很重要,因?yàn)閕nterceptor 鏈的執(zhí)行與添加是反序的,所以在 PageHelperAutoConfiguration 之后添加,才能先執(zhí)行。
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisInterceptorAutoConfiguration {
 
    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;
 
    @PostConstruct
    public void addMyInterceptor() {
        LogInterceptor e = new LogInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(e);
        }
    }
}

以上就是關(guān)于“mybatis攔截器及不生效如何解決”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI