最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis自定義插件Interceptor問題

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

Mybatis自定義插件-Interceptor

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

默認(rèn)情況下,MyBatis 允許使用插件來攔截的方法調(diào)用包括:

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

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

通過 MyBatis 提供的強(qiáng)大機(jī)制,使用插件是非常簡單的,只需實現(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進(jìn)而將查詢的結(jié)果進(jìn)行分頁處理。

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

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

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  攔截結(jié)果集的處理
 * StatementHandler  攔截Sql語法構(gòu)建的處理
 *
 * 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中指定要攔截的對象   ---調(diào)用
        //proceed  ---繼續(xù)
        return invocation.proceed();
    }

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

    @Override
    public void setProperties(Properties properties) {

    }
}


/**
 * MybatisConfig用來將自定義的Interceptor添加進(jìn)去
 */

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

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

相關(guān)文章

  • 詳談java命令的本質(zhì)邏輯揭秘

    詳談java命令的本質(zhì)邏輯揭秘

    一個簡單的java命令背后究竟做了些什么事情,很多朋友提出幾個問題,下面帶領(lǐng)大家一起學(xué)習(xí)Java命令的本質(zhì)邏輯問題,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • java中for循環(huán)刪除集合陷阱

    java中for循環(huán)刪除集合陷阱

    java中在增強(qiáng)for循環(huán)為什么不能增刪集合呢?一個循環(huán)迭代,跟集合的增刪改沒什么關(guān)系。修改集合不是for去做的。for只管循環(huán)迭代,你在循環(huán)里邊修改集合,改變集合的長度,順序?qū)ρh(huán)都有影響
    2015-05-05
  • 如何使用Spring AOP預(yù)處理Controller的參數(shù)

    如何使用Spring AOP預(yù)處理Controller的參數(shù)

    這篇文章主要介紹了如何使用Spring AOP預(yù)處理Controller的參數(shù)操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java方法簽名為何不包含返回值類型

    Java方法簽名為何不包含返回值類型

    這篇文章主要介紹了Java方法簽名為何不包含返回值類型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body

    Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body

    這篇文章主要介紹了Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 教你怎么用Java數(shù)組和鏈表實現(xiàn)棧

    教你怎么用Java數(shù)組和鏈表實現(xiàn)棧

    本篇文章為大家詳細(xì)介紹了怎么用Java數(shù)組和鏈表實現(xiàn)棧,文中有非常詳細(xì)的代碼示例及注釋,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • mybatis中使用CASE?WHEN關(guān)鍵字報錯及解決

    mybatis中使用CASE?WHEN關(guān)鍵字報錯及解決

    這篇文章主要介紹了mybatis中使用CASE?WHEN關(guān)鍵字報錯及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能

    SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能

    本文介紹了如何使用SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能,同時提供了數(shù)據(jù)庫的設(shè)計和詳細(xì)的后端代碼,前端界面使用的Vue2,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-05-05
  • Java 利用遞歸實現(xiàn)鏈表的歸并排序

    Java 利用遞歸實現(xiàn)鏈表的歸并排序

    這篇文章主要介紹了Java 如何利用遞歸實現(xiàn)鏈表的歸并排序,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09

最新評論

望奎县| 安平县| 双鸭山市| 方正县| 富平县| 永登县| 昆山市| 扶绥县| 甘孜县| 古蔺县| 敦煌市| 孟连| 孟村| 龙门县| 云安县| 东乌珠穆沁旗| 泾阳县| 九寨沟县| 秦皇岛市| 宁阳县| 高邑县| 万源市| 松桃| 长沙市| 丽江市| 克东县| 甘德县| 左云县| 长顺县| 榆林市| 肇庆市| 阳江市| 巴彦县| 墨竹工卡县| 上犹县| 临桂县| 北京市| 天水市| 光山县| 绥江县| 冷水江市|