mybatis攔截器及不生效的解決方法
背景:
在一些需求下,使用攔截器會大大簡化工作量也更加靈活:
- 在項(xiàng)目中,要更新數(shù)據(jù)表的審計字段,比如 create_time, creator, update_time, updator, 這些字段,如果每一個表對應(yīng)的mapper 都去寫一次,或每一個方法都去更新一下,這個工作量非常大并且不太友好,并且不夠優(yōu)雅。
- 記錄一些日志,比如執(zhí)行sql時侯,要打印每一個sql執(zhí)行了多久,那就要記錄sql執(zhí)行前的時間戳,執(zhí)行后的時間戳,得到其執(zhí)行時間,再打印。
- 等等場景
在這些場景下,使用攔截器肯定會更加靈活且方法。
mybatis攔截器怎樣做
- 定義一個攔截器
- 把這個攔截器交給spring容器管理
- 如果項(xiàng)目里面使用了 com.github.pagehelper.PageInterceptor 攔截器可能會無效,則需要再定義一個 MybatisInterceptorAutoConfiguration
根據(jù)以上三點(diǎn),進(jìn)行詳細(xì)說明
定義一個攔截器
簡單示意一下怎樣寫。。。具體業(yè)務(wù)肯定不止這樣子的
一個攔截器,主要是實(shí)現(xiàn) Interceptor 這個接口,實(shí)現(xiàn)這個接口下的三個方法。
然后在這個實(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) {
}
}定義一個 MybatisInterceptorAutoConfiguration
為什么要有這么一個類呢,主要是因?yàn)槿绻愕哪K里面引用了 com.github.pagehelper.PageInterceptor,你自定義的攔截器會無效,是因?yàn)閙ybatis的攔截器這就是一個責(zé)任鏈,但是如果執(zhí)行了 PageInterceptor,這個Interceptor比較特別,它自己執(zhí)行完,就不往下傳遞鏈條了,即這個鏈會在它這里斷開。所以加了其它的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);
}
}
}
附錄幾個參考的博文:
Mybatis攔截器
PageHelper導(dǎo)致自定義Mybatis攔截器不生效
Mybatis攔截器失效
到此這篇關(guān)于mybatis攔截器及不生效的解決方法的文章就介紹到這了,更多相關(guān)mybatis攔截器及不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)License認(rèn)證(只校驗(yàn)有效期)的詳細(xì)過程
License也就是版權(quán)許可證書,一般用于收費(fèi)軟件給付費(fèi)用戶提供的訪問許可證明,這篇文章主要介紹了SpringBoot實(shí)現(xiàn)License認(rèn)證(只校驗(yàn)有效期),需要的朋友可以參考下2024-04-04
Spring IOC原理補(bǔ)充說明(循環(huán)依賴、Bean作用域等)
這篇文章主要介紹了Spring IOC原理補(bǔ)充說明(循環(huán)依賴、Bean作用域等),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring中的@AutoWired與@Resource及@Qualifier注解詳解
這篇文章主要介紹了Spring中的@AutoWired與@Resource及@Qualifier注解詳解,spring不但支持自己定義的@Autowired注解,所以Autowired與Spring是強(qiáng)相關(guān)性,只能在spring框架中使用,而后幾個注解則不然,需要的朋友可以參考下2023-11-11
java集合_淺談Iterable和Iterator的區(qū)別
下面小編就為大家?guī)硪黄猨ava集合_淺談Iterable和Iterator的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
IDEA啟動Tomcat報Unrecognized option: --add-opens=java
這篇文章主要為大家介紹了解決IDEA啟動Tomcat報Unrecognized option: --add-opens=java.base/java.lang=ALL-UNNAMED的方法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08

