# Use o interceptor MyBatis para obter a saída completa do console Mybatis sql

Use o interceptor MyBatis para obter a saída completa do console Mybatis sql

Interceptor mybatis

Introdução

  • Como o nome indica, um interceptor deve interceptar uma determinada solicitação ou interface para realizar certas operações. Por exemplo, podemos implementar a interface HandlerInterceptor , que pode interceptar a interface de solicitação da web para verificar se o usuário está logado.

  • MyBatis permite interceptar chamadas em um determinado ponto durante a execução da instrução mapeada. Por padrão, MyBatis permite o uso de plug-ins para interceptar chamadas de método, incluindo:

  1. Método de interceptação do executor: Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  2. Processamento de parâmetros de interceptação: ParameterHandler (getParameterObject, setParameters)
  3. Intercepte o processamento do conjunto de resultados: ResultSetHandler (handleResultSets, handleOutputParameters)
  4. Interceptação da construção da sintaxe Sql: StatementHandler (preparar, parametrizar, lote, atualizar, consultar)

Visualização do código fonte

Interceptor:

Mybatis não tem uma implementação padrão desta interface, você pode implementá-la de acordo com suas necessidades ao usá-la. A ferramenta de consulta de paginação pagehelper possui uma implementação da interface Interceptor para auxiliar na realização da consulta de paginação.

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

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

    default void setProperties(Properties properties) {
    
    
    }
}
Instância do interceptor de configuração do site oficial
  • ExamplePlugin: Todos os métodos de atualização que executam o executor serão interceptados por este interceptor.
@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) {
    
    
  }
}
  • posicionamento xml
<plugins>
    <plugin interceptor="org.format.mybatis.cache.interceptor.ExamplePlugin"></plugin>
</plugins>
  • Interceptação de objeto (invocação de invocação) é o local onde a lógica de interceptação é implementada.Internamente, é necessário avançar explicitamente a cadeia de responsabilidade através de invocation.proceed (), que é chamar o próximo interceptor para interceptar o método alvo.

  • O plugin Object (Object target) é usar o interceptor atual para gerar um proxy para o alvo alvo, o que na verdade é feito através do Plugin.wrap (target, this), passando o alvo alvo e o interceptor this para a função wrapper.

  • setProperties (Propriedades de propriedades) é usado para definir parâmetros adicionais, que são configurados no nó Propriedades do interceptor.

    A anotação descreve a assinatura [tipo, método, args] do método de interceptação especificado (ou seja, qual método de qual objeto é interceptado), que é usado para decisão antes da interceptação.

Método 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;
} 

Realização de saída completa do Console 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;
    }
}

Alcançar o efeito

Insira a descrição da imagem aqui

Acho que você gosta

Origin blog.csdn.net/qq_37248504/article/details/111767707
Recomendado
Clasificación