欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis自定義插件Interceptor問題

 更新時間:2022年11月18日 09:12:59   作者:什么時候怕過冷  
這篇文章主要介紹了Mybatis自定義插件Interceptor問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Mybatis自定義插件-Interceptor

MyBatis允許你在映射語句執(zhí)行過程中的某一點進行攔截調用。

默認情況下,MyBatis 允許使用插件來攔截的方法調用包括:

Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)

這些類中方法的細節(jié)可以通過查看每個方法的簽名來發(fā)現(xiàn),或者直接查看 MyBatis 發(fā)行包中的源代碼。 如果你想做的不僅僅是監(jiān)控方法的調用,那么你最好相當了解要重寫的方法的行為。 因為在試圖修改或重寫已有方法的行為時,很可能會破壞 MyBatis 的核心模塊。 這些都是更底層的類和方法,所以使用插件的時候要特別當心。

通過 MyBatis 提供的強大機制,使用插件是非常簡單的,只需實現(xiàn) Interceptor 接口,并指定想要攔截的方法簽名即.

@Intercepts(value = {@Signature(
? ? ? ? type = Executor.class,
? ? ? ? method = "query",
? ? ? ? args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MyInterceptor implements Interceptor {
? ? // ? ?@Autowired
? ? private Logger log = new LoggerStudoImpl();

? ? @Override
? ? public Object intercept(Invocation invocation) throws Throwable {
? ? ? ? // 該方法寫入自己的邏輯
? ? ? ? Object[] queryArgs = invocation.getArgs();
? ? ? ? MappedStatement ms = (MappedStatement) queryArgs[0];
? ? ? ? BoundSql boundSql = ms.getBoundSql(queryArgs[1]);
? ? ? ? String sql = boundSql.getSql();
? ? ? ? String SQL = new StringBuilder(sql).append(" ").append("and deleted = '0'").toString();
? ? ? ? StaticSqlSource rawSqlSource = new StaticSqlSource(ms.getConfiguration(), SQL, boundSql.getParameterMappings());

? ? ? ? MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), rawSqlSource, ms.getSqlCommandType());
? ? ? ? builder.resource(ms.getResource());
? ? ? ? builder.fetchSize(ms.getFetchSize());
? ? ? ? builder.statementType(ms.getStatementType());
? ? ? ? builder.keyGenerator(ms.getKeyGenerator());
? ? ? ? if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
? ? ? ? ? ? builder.keyProperty(ms.getKeyProperties()[0]);
? ? ? ? }
? ? ? ? builder.timeout(ms.getTimeout());
? ? ? ? builder.parameterMap(ms.getParameterMap());
? ? ? ? builder.resultMaps(ms.getResultMaps());
? ? ? ? builder.resultSetType(ms.getResultSetType());
? ? ? ? builder.cache(ms.getCache());
? ? ? ? builder.flushCacheRequired(ms.isFlushCacheRequired());
? ? ? ? builder.useCache(ms.isUseCache());
? ? ? ? MappedStatement statement = builder.build();
? ? ? ? queryArgs[0] = statement;
? ? ? ? log.error(SQL);
? ? ? ? return invocation.proceed();

? ? }

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

? ? @Override
? ? public void setProperties(Properties properties) {

? ? }
}

然后將自定義插件添加到Mybatis配置文件中,該插件就會生效。

? ?<plugins>
? ? ? ? <plugin interceptor="com.example.shiro.config.MyInterceptor"></plugin>
? ? </plugins>

自定義插件攔截的是Excutor中的query方法,也就是說只要是Mybatis執(zhí)行查詢,那么該插件就會攔截查詢的SQL語句,將查詢的SQL添加上and deleted = ‘0’,這樣每次查詢就無需加上deleted='0’了。

比如一些分頁插件就是攔截的ReultSetHandle進而將查詢的結果進行分頁處理。

Mybatis Interceptor插件開發(fā)總結

Interviewceptor插件開發(fā)的demo先記錄下,mybatis的加載過程和執(zhí)行過程下次再補上

package com.syygl.test.study.mybatis;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.plugin.*;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 *type=
 * Executor          攔截執(zhí)行器的方法
 * ParameterHandler  攔截參數(shù)的處理
 * ResultSetHandler  攔截結果集的處理
 * StatementHandler  攔截Sql語法構建的處理
 *
 * method的值是以上四個接口中的method
 *
 * args的值是method的參數(shù)
 *
 */


@Intercepts(
{
@Signature(type = Executor.class, method = "query", args = {})
}
)

public class InterceptorTest implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //Invocation是type中指定要攔截的對象   ---調用
        //proceed  ---繼續(xù)
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        //是要攔截的對象才會進入處理方案
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}


/**
 * MybatisConfig用來將自定義的Interceptor添加進去
 */

@Configuration
class MybatisConfig {
    @Bean
    ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return new ConfigurationCustomizer() {
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                configuration.addInterceptor(new InterceptorTest());
            }
        };
    }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • SpringBoot?mybatis-plus使用json字段實戰(zhàn)指南

    SpringBoot?mybatis-plus使用json字段實戰(zhàn)指南

    在現(xiàn)代應用開發(fā)中經常會使用JSON格式存儲和傳輸數(shù)據,為了便捷地處理數(shù)據庫中的JSON字段,MyBatis-Plus提供了強大的JSON處理器,這篇文章主要給大家介紹了關于SpringBoot?mybatis-plus使用json字段的相關資料,需要的朋友可以參考下
    2024-01-01
  • JavaWeb學習筆記之Filter和Listener

    JavaWeb學習筆記之Filter和Listener

    這篇文章主要給大家介紹了關于JavaWeb學習筆記之Filter和Listener的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • 詳解Java springboot 整合Shiro框架

    詳解Java springboot 整合Shiro框架

    這篇文章主要為大家介紹了Java springboot 整合Shiro框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Spring-cloud Feign 的深入理解

    Spring-cloud Feign 的深入理解

    這篇文章主要介紹了Spring-cloud Feign 的深入理解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • Java掩碼的幾種使用例舉

    Java掩碼的幾種使用例舉

    今天小編就為大家分享一篇關于Java掩碼的使用,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Spring Task定時任務的實現(xiàn)詳解

    Spring Task定時任務的實現(xiàn)詳解

    這篇文章主要介紹了SpringBoot定時任務功能詳細解析,這次的功能開發(fā)過程中也算是對其內涵的進一步了解,以后遇到定時任務的處理也更清晰,更有效率了,對SpringBoot定時任務相關知識感興趣的朋友一起看看吧
    2022-08-08
  • Spring中MVC模塊代碼詳解

    Spring中MVC模塊代碼詳解

    這篇文章主要介紹了Spring中MVC模塊代碼詳解,涉及Controller的簡單介紹,具有一定借鑒價值,需要的朋友可以參考下。
    2017-11-11
  • 一篇文章帶你入門Java字面量和常量

    一篇文章帶你入門Java字面量和常量

    這篇文章主要介紹了探究Java的常量,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-08-08
  • 利用Redis實現(xiàn)延時處理的方法實例

    利用Redis實現(xiàn)延時處理的方法實例

    這篇文章主要給大家介紹了關于利用Redis實現(xiàn)延時處理的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03
  • springboot themaleaf 第一次進頁面不加載css的問題

    springboot themaleaf 第一次進頁面不加載css的問題

    這篇文章主要介紹了springboot themaleaf 第一次進頁面不加載css的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論