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

spring?boot?Mybatis?攔截器實現(xiàn)拼接sql和修改的代碼詳解

 更新時間:2022年05月09日 10:31:52   作者:曾經(jīng)是最好  
這篇文章主要介紹了spring?boot?Mybatis?攔截器實現(xiàn)拼接sql和修改,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

定義一個 SqlIntercepor 類

import com.culturalCenter.placeManage.globalConfig.Interface.InterceptAnnotation;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Statement;
import java.util.Properties;
@Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
        @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
        @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})})
@Component
@Configuration
public class SqlInterceptor implements Interceptor {
    private Logger logger = LoggerFactory.getLogger(SqlInterceptor.class);
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
        //先攔截到RoutingStatementHandler,里面有個StatementHandler類型的delegate變量,其實現(xiàn)類是BaseStatementHandler,然后就到BaseStatementHandler的成員變量mappedStatement
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        //id為執(zhí)行的mapper方法的全路徑名,如com.uv.dao.UserMapper.insertUser
        String id = mappedStatement.getId();
        logger.info("攔截到當(dāng)前請求方法的全路徑名為--->:  " + id);
        //sql語句類型 select、delete、insert、update
        String sqlCommandType = mappedStatement.getSqlCommandType().toString();
        BoundSql boundSql = statementHandler.getBoundSql();
        //獲取到原始sql語句
        String sql = boundSql.getSql();
        String mSql = sql;
        //獲取參數(shù)
        Object parameter = statementHandler.getParameterHandler().getParameterObject();
        logger.info("攔截到當(dāng)前請求SQL為--->: " + sql + "<------------>請求類型為:  " + sqlCommandType);
        logger.info("攔截到當(dāng)前請求參數(shù)為--->: " + parameter);
        //TODO 修改位置
        //注解邏輯判斷  添加注解了才攔截//InterceptAnnotation
        Class<?> classType = Class.forName(mappedStatement.getId().substring(0, mappedStatement.getId().lastIndexOf(".")));
        String mName = mappedStatement.getId().substring(mappedStatement.getId().lastIndexOf(".") + 1, mappedStatement.getId().length());
        for (Method method : classType.getDeclaredMethods()) {
            if (method.isAnnotationPresent(InterceptAnnotation.class) && mName.equals(method.getName())) {
                InterceptAnnotation interceptorAnnotation = method.getAnnotation(InterceptAnnotation.class);
                if (interceptorAnnotation.flag()) {
                    mSql = sql + " limit 2";
                }
            }
        }
        //通過反射修改sql語句
        Field field = boundSql.getClass().getDeclaredField("sql");
        field.setAccessible(true);
        field.set(boundSql, mSql);
        return invocation.proceed();
    }
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, new SqlInterceptor());
    }
    @Override
    public void setProperties(Properties properties) {
//        this.setProperties(properties);
    }

自定義一個注解類實現(xiàn)局部處理SQL修改

InterceptAnnotation 

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 配合SqlInterceptor實現(xiàn)局部攔截
 * */
@Target({ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface InterceptAnnotation {
    boolean flag() default  true;
}

自定義數(shù)據(jù)源工廠類

SqlSessionFactoryConfig

package com.culturalCenter.placeManage.globalConfig;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
 * @author wulincheng
 * @Date 2020年6月23日18:25:22
 *  創(chuàng)建SQL連接工廠類
 * */
@Configuration
public class SqlSessionFactoryConfig {
    @javax.annotation.Resource
    DruidDataSource dataSource;
    /**
     * @Autowired SqlSessionFactory sqlSessionFactory;
     * SqlSession session = sqlSessionFactory.openSession();
     * //創(chuàng)建sqlMapper
     * SqlMapper sqlMapper = new SqlMapper(session);
     */
    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);//更多參數(shù)請自行注入
        bean.setPlugins(new Interceptor[]{new SqlInterceptor()});
        Resource[] resources = new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/*.xml");
        bean.setMapperLocations(resources);
        return bean.getObject();
    }
}

到此這篇關(guān)于spring boot Mybatis 攔截器,實現(xiàn)拼接sql和修改的文章就介紹到這了,更多相關(guān)spring boot Mybatis 攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java操作另一個Java程序使其重啟的簡單實現(xiàn)

    Java操作另一個Java程序使其重啟的簡單實現(xiàn)

    下面小編就為大家?guī)硪黄狫ava操作另一個Java程序使其重啟的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • JAVA中的SPI思想介紹

    JAVA中的SPI思想介紹

    大家好,本篇文章主要講的是JAVA中的SPI思想介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • springBoot詳細(xì)講解使用mybaties案例

    springBoot詳細(xì)講解使用mybaties案例

    MyBatis本是apache的一個開源項目iBatis,2010年這個項目由apache software foundation遷移到了google code,并且改名為MyBatis。2013年11月遷移到Github。iBATIS一詞來源于“internet”和“abatis”的組合,是一個基于Java的持久層框架
    2022-05-05
  • Spring?Boot中使用Spring?Retry重試框架的操作方法

    Spring?Boot中使用Spring?Retry重試框架的操作方法

    這篇文章主要介紹了Spring?Retry?在SpringBoot?中的應(yīng)用,介紹了RetryTemplate配置的時候,需要設(shè)置的重試策略和退避策略,需要的朋友可以參考下
    2022-04-04
  • 利用Java理解sql的語法(實例講解)

    利用Java理解sql的語法(實例講解)

    下面小編就為大家分享一篇利用Java理解sql的語法(實例講解),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • 在SpringBoot框架下實現(xiàn)Excel導(dǎo)入導(dǎo)出的方法詳解

    在SpringBoot框架下實現(xiàn)Excel導(dǎo)入導(dǎo)出的方法詳解

    SpringBoot是由Pivotal團(tuán)隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,今天我們就使用純前對按表格控件帶大家了解,如何在Spring Boot框架下實現(xiàn)Excel服務(wù)端導(dǎo)入導(dǎo)出,需要的朋友可以參考下
    2023-06-06
  • 詳解如何解析pom文件方法示例

    詳解如何解析pom文件方法示例

    這篇文章主要為大家介紹了詳解如何解析pom文件方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Java?Selenide?簡介與用法

    Java?Selenide?簡介與用法

    Selenium?是目前用的最廣泛的Web?UI?自動化測試框架,本文給大家介紹下Java?Selenide使用,感興趣的朋友一起看看吧
    2022-01-01
  • JAVA中反射機(jī)制和模塊化的深入講解

    JAVA中反射機(jī)制和模塊化的深入講解

    很多剛學(xué)Java反射的同學(xué)可能對反射技術(shù)一頭霧水,為什么要學(xué)習(xí)反射,學(xué)習(xí)反射有什么作用,下面這篇文章主要給大家介紹了關(guān)于JAVA中反射機(jī)制和模塊化的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Java,JSP,Servlet獲取當(dāng)前工程路徑(絕對路徑)問題解析

    Java,JSP,Servlet獲取當(dāng)前工程路徑(絕對路徑)問題解析

    這篇文章主要介紹了Java,JSP,Servlet獲取當(dāng)前工程路徑(絕對路徑)問題解析,需要的朋友可以參考下。
    2017-09-09

最新評論

裕民县| 勃利县| 山阴县| 策勒县| 浦城县| 屏东县| 迭部县| 安徽省| 邯郸县| 夏津县| 特克斯县| 凉山| 凉山| 胶州市| 容城县| 绥宁县| 本溪市| 娄底市| 阿克苏市| 高青县| 高唐县| 清丰县| 隆回县| 双峰县| 襄城县| 新沂市| 社旗县| 尚志市| 邵阳市| 长泰县| 密山市| 兴和县| 和田县| 茌平县| 敦化市| 司法| 十堰市| 洪洞县| 咸宁市| 宣化县| 涟水县|