# MyBatis 인터셉터를 사용하여 Mybatis SQL 콘솔의 완전한 출력을 얻으십시오.

MyBatis 인터셉터를 사용하여 Mybatis SQL 콘솔의 완전한 출력을 얻으십시오.

Mybatis 인터셉터

소개

  • 이름에서 알 수 있듯이 인터셉터는 특정 작업을 수행하기 위해 특정 요청 또는 인터페이스를 인터셉트해야합니다. 예를 들어 웹 요청 인터페이스를 인터셉트하여 사용자가 로그인했는지 확인할 수 있는 HandlerInterceptor 인터페이스를 구현할 수 있습니다.

  • MyBatis를 사용하면 매핑 된 문을 실행하는 동안 특정 지점에서 호출을 가로 챌 수 있습니다. 기본적으로 MyBatis는 플러그인을 사용하여 다음을 포함한 메소드 호출을 가로 챌 수 있습니다.

  1. 실행기를 가로채는 방법 : 실행기 (업데이트, 쿼리, flushStatements, 커밋, 롤백, getTransaction, close, isClosed)
  2. 인터셉트 매개 변수 처리 : ParameterHandler (getParameterObject, setParameters)
  3. 결과 세트의 처리를 인터셉트하십시오. ResultSetHandler (handleResultSets, handleOutputParameters)
  4. SQL 구문 생성 차단 : StatementHandler (준비, 매개 변수화, 배치, 업데이트, 쿼리)

소스 코드 미리보기

인터셉터 :

Mybatis에는이 인터페이스의 기본 구현이 없으므로 사용할 때 필요에 따라 구현할 수 있습니다. 페이징 쿼리 도구 pagehelper에는 페이징 쿼리의 실현을 지원하는 인터셉터 인터페이스 구현이 있습니다.

public interface Interceptor {
    
    
    Object intercept(Invocation var1) throws Throwable;

    default Object plugin(Object target) {
    
    
        return Plugin.wrap(target, this);
    }

    default void setProperties(Properties properties) {
    
    
    }
}
공식 웹 사이트 구성 인터셉터 인스턴스
  • ExamplePlugin : 실행기를 실행하는 모든 업데이트 메서드는이 인터셉터에 의해 차단됩니다.
@Intercepts({
    
    @Signature(
  type= Executor.class,
  method = "update",
  args = {
    
    MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
    
    
  public Object intercept(Invocation invocation) throws Throwable {
    
    
    return invocation.proceed();
  }
  public Object plugin(Object target) {
    
    
    return Plugin.wrap(target, this);
  }
  public void setProperties(Properties properties) {
    
    
  }
}
  • xml 배치
<plugins>
    <plugin interceptor="org.format.mybatis.cache.interceptor.ExamplePlugin"></plugin>
</plugins>
  • 객체 인터셉트 (Invocation invocation)는 인터 셉션 로직이 구현 된 곳으로 내부적으로는 다음 인터셉터를 호출하여 타겟 메소드를 인터셉트하는 invocation.proceed ()를 통해 명시 적으로 책임 체인을 전진시켜야합니다.

  • Object plugin (Object target)은 현재 인터셉터를 사용하여 대상 대상에 대한 프록시를 생성하는 것으로, 실제로는 Plugin.wrap (target, this)를 통해 수행되며 대상 대상과 인터셉터를 래퍼 함수에 전달합니다.

  • setProperties (Properties properties)는 인터셉터의 Properties 노드에서 구성되는 추가 매개 변수를 설정하는 데 사용됩니다.

    어노테이션은 인터 셉션 전 결정에 사용되는 지정된 인터 셉션 방법 (즉, 객체가 인터셉트되는 방법)의 시그니처 [유형, 방법, 인수]를 설명합니다.

Plugin.wrap 메서드
public staticObject wrap(Object target, Interceptor interceptor) {
    
    
    //从拦截器的注解中获取拦截的类名和方法信息
    Map<Class<?>, Set<Method>> signatureMap =getSignatureMap(interceptor);
    Class<?> type = target.getClass();
    //解析被拦截对象的所有接口(注意是接口)
    Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
    if(interfaces.length > 0) {
    
    
        //生成代理对象, Plugin对象为该代理对象的InvocationHandler  
        returnProxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target,interceptor,signatureMap));
    }
    returntarget;
} 

콘솔 SQL 완전한 출력 실현

  • SqlLoogerConfig
/**
 * @Description:Sql 完整输出sql
 * @Author:LiDong
 * @Create:2020/12/26
 * @Version:1.0.0
 */
@Configuration
public class SqlLoogerConfig {
    
    

    @Bean
    public SqlInterceptor sqlInterceptor() {
    
    
        return new SqlInterceptor();
    }
}
  • SqlInterceptor
package com.li.core.config.mybatis;

import com.github.pagehelper.util.StringUtil;
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.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @Description:mybatis sql拦截器 控制打印出完整sql
 * @Author:LiDong
 * @Create:2020/12/26
 * @Version:1.0.0
 */
@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}),
        @Signature(type = Executor.class, method = "update", args = {
    
    MappedStatement.class, Object.class})}
)
public class SqlInterceptor implements Interceptor {
    
    

    private static final Logger logger = LoggerFactory.getLogger(SqlInterceptor.class);

    private static final ThreadLocal<SimpleDateFormat> DATETIME_FORMATTER = new ThreadLocal<SimpleDateFormat>() {
    
    
        @Override
        protected SimpleDateFormat initialValue() {
    
    
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
    
    
        Object result = null;
        //捕获掉异常,不要影响业务
        try {
    
    
            MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
            Object parameter = null;
            if (invocation.getArgs().length > 1) {
    
    
                parameter = invocation.getArgs()[1];
            }
            String sqlId = mappedStatement.getId();
            BoundSql boundSql = mappedStatement.getBoundSql(parameter);
            Configuration configuration = mappedStatement.getConfiguration();

            long startTime = System.currentTimeMillis();

            try {
    
    
                result = invocation.proceed();
            } finally {
    
    
                long endTime = System.currentTimeMillis();
                long sqlCostTime = endTime - startTime;
                String sql = this.getSql(configuration, boundSql);
                this.formatSqlLog(sqlId, sql, sqlCostTime, result);
            }
            return result;

        } catch (Exception e) {
    
    
            return result;
        }
    }

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

    @Override
    public void setProperties(Properties properties) {
    
    
    }

    /**
     * 获取完整的sql语句
     *
     * @param configuration
     * @param boundSql
     * @return
     */
    private String getSql(Configuration configuration, BoundSql boundSql) {
    
    
        // 输入sql字符串空判断
        String sql = boundSql.getSql();
        if (StringUtil.isEmpty(sql)) {
    
    
            return "";
        }
        return formatSql(sql, configuration, boundSql);
    }

    /**
     * 将占位符替换成参数值
     *
     * @param sql
     * @param configuration
     * @param boundSql
     * @return
     */
    private String formatSql(String sql, Configuration configuration, BoundSql boundSql) {
    
    
        //美化sql
        sql = beautifySql(sql);
        //填充占位符, 目前基本不用mybatis存储过程调用,故此处不做考虑
        Object parameterObject = boundSql.getParameterObject();
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        List<String> parameters = new ArrayList<>();
        if (parameterMappings != null) {
    
    
            MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
            for (int i = 0; i < parameterMappings.size(); i++) {
    
    
                ParameterMapping parameterMapping = parameterMappings.get(i);
                if (parameterMapping.getMode() != ParameterMode.OUT) {
    
    
                    //  参数值
                    Object value;
                    String propertyName = parameterMapping.getProperty();
                    //  获取参数名称
                    if (boundSql.hasAdditionalParameter(propertyName)) {
    
    
                        // 获取参数值
                        value = boundSql.getAdditionalParameter(propertyName);
                    } else if (parameterObject == null) {
    
    
                        value = null;
                    } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
    
    
                        // 如果是单个值则直接赋值
                        value = parameterObject;
                    } else {
    
    
                        value = metaObject == null ? null : metaObject.getValue(propertyName);
                    }

                    if (value instanceof Number) {
    
    
                        parameters.add(String.valueOf(value));
                    } else {
    
    
                        StringBuilder builder = new StringBuilder();
                        builder.append("'");
                        if (value instanceof Date) {
    
    
                            builder.append(DATETIME_FORMATTER.get().format((Date) value));
                        } else if (value instanceof String) {
    
    
                            builder.append(value);
                        }
                        builder.append("'");
                        parameters.add(builder.toString());
                    }
                }
            }
        }

        for (String value : parameters) {
    
    
            sql = sql.replaceFirst("\\?", value);
        }
        return sql;
    }


    /**
     * 格式化sql日志
     *
     * @param sqlId
     * @param sql
     * @param costTime
     * @return
     */
    private void formatSqlLog(String sqlId, String sql, long costTime, Object obj) {
    
    
        String sqlLog = "=====> " + sql;
        StringBuffer result = new StringBuffer();
        if (obj instanceof List) {
    
    
            List list = (List) obj;
            int count = list.size();
            result.append("=====> Total:" + count);
        } else if (obj instanceof Integer) {
    
    
            result.append("=====> Total:" + obj);
        }
        result.append(" SpendTime:" + costTime + " ms");
        logger.info("\n------------------------------------------------------------------------------------------------------------------\n"
                + sqlLog + "\n" + result +
                "\n------------------------------------------------------------------------------------------------------------------");
    }


    public static String beautifySql(String sql) {
    
    
        sql = sql.replaceAll("[\\s\n ]+", " ");
        return sql;
    }
}

효과 달성

여기에 사진 설명 삽입

추천

출처blog.csdn.net/qq_37248504/article/details/111767707