亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MyBatis自定義SQL攔截器示例詳解

 更新時間:2021年10月24日 12:01:16   作者:老鄭_  
Mybatis支持對Executor、StatementHandler、PameterHandler和ResultSetHandler 接口進(jìn)行攔截,也就是說會對這4種對象進(jìn)行代理,下面這篇文章主要給大家介紹了關(guān)于MyBatis自定義SQL攔截器的相關(guān)資料,需要的朋友可以參考下

前言

本文主要是講通過 MyBaits 的 Interceptor 的拓展點(diǎn)進(jìn)行對 MyBatis 執(zhí)行 SQL 之前做一個邏輯攔截實(shí)現(xiàn)自定義邏輯的插入執(zhí)行。

適合場景:1. 比如限制數(shù)據(jù)庫查詢最大訪問條數(shù);2. 限制登錄用戶只能訪問當(dāng)前機(jī)構(gòu)數(shù)據(jù)。

定義是否開啟注解

定義是否開啟注解, 主要做的一件事情就是是否添加 SQL 攔截器。

// 全局開啟
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MyBatisSqlInterceptorConfiguration.class)
public @interface EnableSqlInterceptor {

}

// 自定義注解
@Target({ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface DataScope {

}

注冊SQL 攔截器

注冊一個 SQL 攔截器,會對符合條件的 SQL 查詢操作進(jìn)行攔截。

public class MyBatisSqlInterceptorConfiguration implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
        sqlSessionFactory.getConfiguration().addInterceptor(new MyBatisInterceptor());
    }
}


處理邏輯

在處理邏輯中,我主要是做一個簡單的 limit 1 案例,如果是自己需要做其他的邏輯需要修改

@Slf4j
@Intercepts(
        {
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
        })
public class MyBatisInterceptor implements Interceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyBatisInterceptor.class);

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // TODO Auto-generated method stub
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        ResultHandler resultHandler = (ResultHandler) args[3];
        Executor executor = (Executor) invocation.getTarget();
        CacheKey cacheKey;
        BoundSql boundSql;
        //由于邏輯關(guān)系,只會進(jìn)入一次
        if (args.length == 4) {
            //4 個參數(shù)時
            boundSql = ms.getBoundSql(parameter);
            cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
        } else {
            //6 個參數(shù)時
            cacheKey = (CacheKey) args[4];
            boundSql = (BoundSql) args[5];
        }
        DataScope dataScope = getDataScope(ms);
        if (Objects.nonNull(dataScope)) {
            String origSql = boundSql.getSql();
            log.info("origSql : {}", origSql);
            // 組裝新的 sql
            // todo you weaving business
            String newSql = origSql + " limit 1";

            // 重新new一個查詢語句對象
            BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), newSql,
                    boundSql.getParameterMappings(), boundSql.getParameterObject());

            // 把新的查詢放到statement里
            MappedStatement newMs = newMappedStatement(ms, new BoundSqlSource(newBoundSql));
            for (ParameterMapping mapping : boundSql.getParameterMappings()) {
                String prop = mapping.getProperty();
                if (boundSql.hasAdditionalParameter(prop)) {
                    newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
                }
            }

            args[0] = newMs;
            if (args.length == 6) {
                args[5] = newMs.getBoundSql(parameter);
            }
        }
        LOGGER.info("mybatis intercept sql:{},Mapper方法是:{}", boundSql.getSql(), ms.getId());


        return invocation.proceed();
    }

    private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
        MappedStatement.Builder builder = new
                MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, 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());
        return builder.build();
    }


    private DataScope getDataScope(MappedStatement mappedStatement) {
        String id = mappedStatement.getId();
        // 獲取 Class Method
        String clazzName = id.substring(0, id.lastIndexOf('.'));
        String mapperMethod = id.substring(id.lastIndexOf('.') + 1);

        Class<?> clazz;
        try {
            clazz = Class.forName(clazzName);
        } catch (ClassNotFoundException e) {
            return null;
        }
        Method[] methods = clazz.getMethods();

        DataScope dataScope = null;
        for (Method method : methods) {
            if (method.getName().equals(mapperMethod)) {
                dataScope = method.getAnnotation(DataScope.class);
                break;
            }
        }
        return dataScope;
    }

    @Override
    public Object plugin(Object target) {
        // TODO Auto-generated method stub
        LOGGER.info("MysqlInterCeptor plugin>>>>>>>{}", target);
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // TODO Auto-generated method stub
        String dialect = properties.getProperty("dialect");
        LOGGER.info("mybatis intercept dialect:>>>>>>>{}", dialect);
    }

    /**
     * 定義一個內(nèi)部輔助類,作用是包裝 SQL
     */
    class BoundSqlSource implements SqlSource {
        private BoundSql boundSql;

        public BoundSqlSource(BoundSql boundSql) {
            this.boundSql = boundSql;
        }

        public BoundSql getBoundSql(Object parameterObject) {
            return boundSql;
        }

    }

}

如何使用

我們在 XXXMapper 中對應(yīng)的數(shù)據(jù)操作方法只要加入 @DataScope 注解即可。

@Mapper
public interface OrderMapper {

   @Select("select 1 ")
   @DataScope
   Intger selectOne();

}

參考資料

mybatis.org/mybatis-3/z

總結(jié)

到此這篇關(guān)于MyBatis自定義SQL攔截器的文章就介紹到這了,更多相關(guān)MyBatis自定義SQL攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于java文本復(fù)制的7種方式總結(jié)

    基于java文本復(fù)制的7種方式總結(jié)

    下面小編就為大家分享一篇基于java文本復(fù)制的7種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • springboot通過spel結(jié)合aop實(shí)現(xiàn)動態(tài)傳參的案例

    springboot通過spel結(jié)合aop實(shí)現(xiàn)動態(tài)傳參的案例

    SpEl 是Spring框架中的一個利器,Spring通過SpEl能在運(yùn)行時構(gòu)建復(fù)雜表達(dá)式、存取對象屬性、對象方法調(diào)用等,今天通過本文給大家介紹springboot?spel結(jié)合aop實(shí)現(xiàn)動態(tài)傳參,需要的朋友可以參考下
    2022-07-07
  • jeefast和Mybatis實(shí)現(xiàn)三級聯(lián)動的示例代碼

    jeefast和Mybatis實(shí)現(xiàn)三級聯(lián)動的示例代碼

    這篇文章主要介紹了jeefast和Mybatis實(shí)現(xiàn)三級聯(lián)動的示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Java中控制流程語句的深入講解

    Java中控制流程語句的深入講解

    流程控制語句顧名思義就是控制程序走向的語句,其中包括條件語句,分支語句和循環(huán)語句,這篇文章主要給大家介紹了關(guān)于Java中控制流程語句的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • SpringBoot整合Javamail實(shí)現(xiàn)郵件發(fā)送的詳細(xì)過程

    SpringBoot整合Javamail實(shí)現(xiàn)郵件發(fā)送的詳細(xì)過程

    日常開發(fā)過程中,我們經(jīng)常需要使用到郵件發(fā)送任務(wù),比方說驗(yàn)證碼的發(fā)送、日常信息的通知等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Javamail實(shí)現(xiàn)郵件發(fā)送的詳細(xì)過程,需要的朋友可以參考下
    2022-10-10
  • Java實(shí)現(xiàn)將CSV轉(zhuǎn)為Excel的示例代碼

    Java實(shí)現(xiàn)將CSV轉(zhuǎn)為Excel的示例代碼

    CSV(Comma?Separated?Values)文件是一種純文本文件,包含用逗號分隔的數(shù)據(jù),常用于將數(shù)據(jù)從一個應(yīng)用程序?qū)牖驅(qū)С龅搅硪粋€應(yīng)用程序。本文將利用Java實(shí)現(xiàn)CSV轉(zhuǎn)為Excel,感興趣的可以了解一下
    2022-03-03
  • java讀取resource目錄下文件的方法示例

    java讀取resource目錄下文件的方法示例

    這篇文章主要介紹了利用java讀取resource目錄下文件的方法,文中給出了詳細(xì)的示例代碼,相信對大家具有一定的參考借鑒,需要的朋友們下面來一起看看吧。
    2017-02-02
  • 使用java獲取md5值的兩種方法

    使用java獲取md5值的兩種方法

    本篇文章是對使用java獲取md5值的兩種方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • SpringBoot激活profiles的幾種方式

    SpringBoot激活profiles的幾種方式

    多環(huán)境是最常見的配置隔離方式之一,可以根據(jù)不同的運(yùn)行環(huán)境提供不同的配置信息來應(yīng)對不同的業(yè)務(wù)場景,這篇文章主要介紹了SpringBoot激活profiles的幾種方式,需要的朋友可以參考下
    2019-10-10
  • Spring?Boot中自動執(zhí)行sql腳本的方法實(shí)例

    Spring?Boot中自動執(zhí)行sql腳本的方法實(shí)例

    在SpringBoot的架構(gòu)中,DataSourceInitializer類可以在項(xiàng)目啟動后初始化數(shù)據(jù),我們可以通過自動執(zhí)行自定義sql腳本初始化數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中自動執(zhí)行sql腳本的相關(guān)資料,需要的朋友可以參考下
    2022-01-01

最新評論