這篇“mybatis攔截器及不生效如何解決”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“mybatis攔截器及不生效如何解決”文章吧。
在一些需求下,使用攔截器會大大簡化工作量也更加靈活:
在項目中,要更新數據表的審計字段,比如 create_time, creator, update_time, updator, 這些字段,如果每一個表對應的mapper 都去寫一次,或每一個方法都去更新一下,這個工作量非常大并且不太友好,并且不夠優雅。
記錄一些日志,比如執行sql時侯,要打印每一個sql執行了多久,那就要記錄sql執行前的時間戳,執行后的時間戳,得到其執行時間,再打印。
等等場景
在這些場景下,使用攔截器肯定會更加靈活且方法。
定義一個攔截器
把這個攔截器交給spring容器管理
如果項目里面使用了 com.github.pagehelper.PageInterceptor 攔截器可能會無效,則需要再定義一個 MybatisInterceptorAutoConfiguration
根據以上三點,進行詳細說明
簡單示意一下怎樣寫。。。具體業務肯定不止這樣子的
一個攔截器,主要是實現 Interceptor 這個接口,實現這個接口下的三個方法。
然后在這個實現類加上 @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) { } }
為什么要有這么一個類呢,主要是因為如果你的模塊里面引用了 com.github.pagehelper.PageInterceptor,你自定義的攔截器會無效,是因為mybatis的攔截器這就是一個責任鏈,但是如果執行了 PageInterceptor,這個Interceptor比較特別,它自己執行完,就不往下傳遞鏈條了,即這個鏈會在它這里斷開。所以加了其它的interceptor, 它們必須在 PageInterceptor 之前執行。
具體可見代碼:
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 // 這一行很重要,因為interceptor 鏈的執行與添加是反序的,所以在 PageHelperAutoConfiguration 之后添加,才能先執行。 @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攔截器及不生效如何解決”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。