溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis如何使用攔截器interceptor對(duì)sql打印,使執(zhí)行的sql在日志中可見

發(fā)布時(shí)間:2021-06-25 13:45:44 來源:億速云 閱讀:581 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“mybatis如何使用攔截器interceptor對(duì)sql打印,使執(zhí)行的sql在日志中可見”,在日常操作中,相信很多人在mybatis如何使用攔截器interceptor對(duì)sql打印,使執(zhí)行的sql在日志中可見問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”mybatis如何使用攔截器interceptor對(duì)sql打印,使執(zhí)行的sql在日志中可見”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

import lombok.extern.slf4j.Slf4j;
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.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
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 java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

/**
 * @author kxd
 * @date 2019/10/25 15:55
 * description:
 */

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

    /**
     * 是否顯示語句的執(zhí)行時(shí)間
     */
    public static final String PROPERTIES_KEY_ENABLE_EXECUTOR_TIME = "enableExecutorTIme";
    public static final String NO = "NO";
    public static final String YES = "YES";
    private String enableExecutorTime;


    /**
     * 執(zhí)行邏輯
     *
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        if (enableExecutorTime.equals(YES)) {
            MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
            //這里是為了找到上層是哪個(gè)方法觸發(fā)了這個(gè)攔截器

            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 sqlStartTime = System.currentTimeMillis();
            Object next = invocation.proceed();
            long sqlEndTime = System.currentTimeMillis();
            String sql = getSql(configuration, boundSql, sqlId);
            String sqlTimeLog = sqlId.concat(">>runs :").concat(String.valueOf(sqlEndTime - sqlStartTime)).concat("ms");
            log.info(">>>>>>>>>>>>>runs method:{}", sqlTimeLog);
            log.info(">>>>>>>>>>>>>content:{}", sql);
            return next;
        }
        return invocation.proceed();
    }

    private String getSql(Configuration configuration, BoundSql boundSql, String sqlId) {
        return sqlId + ">>execute sql:" + assembleSql(configuration, boundSql);
    }

    /**
     * 組裝sql信息
     *
     * @param configuration
     * @param boundSql
     * @return
     */
    private String assembleSql(Configuration configuration, BoundSql boundSql) {
        Object sqlParameter = boundSql.getParameterObject();
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        String sql = boundSql.getSql().replaceAll("[\\s+]", "").replaceAll("from", "\n\tFROM\n\t").replaceAll("select", "\n\tSELECT\t\n");
        if (parameterMappings.size() > 0 && sqlParameter != null) {
            TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            if (typeHandlerRegistry.hasTypeHandler(sqlParameter.getClass())) {
                sql = sql.replaceFirst("\\?", getParameterValue(sqlParameter));
            } else {
                MetaObject metaObject = configuration.newMetaObject(sqlParameter);
                for (ParameterMapping parameterMapping : parameterMappings) {
                    String propertyName = parameterMapping.getProperty();
                    if (metaObject.hasGetter(propertyName)) {
                        Object obj = metaObject.getValue(propertyName);
                        sql = sql.replaceFirst("\\?", getParameterValue(obj));
                    } else if (boundSql.hasAdditionalParameter(propertyName)) {
                        Object obj = boundSql.getAdditionalParameter(propertyName);
                        sql = sql.replaceFirst("\\?", getParameterValue(obj));
                    }
                }
            }

        }
        return sql;
    }

    /**
     * 獲取參數(shù)對(duì)應(yīng)string值
     *
     * @param obj
     * @return
     */
    private String getParameterValue(Object obj) {
        String value = "";
        if (obj instanceof String) {
            value = "'".concat(obj.toString()).concat("'");
        } else if (obj instanceof Date) {
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
            value = "'".concat(dateFormat.format(new Date())) + "'";
        } else {
            if (obj != null) {
                value = obj.toString();
            } else {
                value = "";
            }
        }
        return value != null ? makeStringAllRegExp(value) : value;
    }

    /**
     * 轉(zhuǎn)義正則特殊字符串
     *
     * @param str
     * @return
     */
    private String makeStringAllRegExp(String str) {
        if (str != null && !str.equals("")) {
            return str.replace("\\", "\\\\").replace("*", "\\*")
                    .replace("+", "\\+").replace("|", "\\|")
                    .replace("{", "\\{").replace("}", "\\}")
                    .replace("(", "\\(").replace(")", "\\)")
                    .replace("^", "\\^").replace("$", "\\$")
                    .replace("[", "\\[").replace("]", "\\]")
                    .replace("?", "\\?").replace(",", "\\,")
                    .replace(".", "\\.").replace("&", "\\&");
        }
        return str;
    }

    /**
     * 返回代理對(duì)象
     *
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    /**
     * 設(shè)置屬性信息
     *
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
        if (properties != null) {
            String executorTimeValue = properties.getProperty(PROPERTIES_KEY_ENABLE_EXECUTOR_TIME);
            if (executorTimeValue != null) {
                enableExecutorTime = executorTimeValue;
            }
        }
    }
}

到此,關(guān)于“mybatis如何使用攔截器interceptor對(duì)sql打印,使執(zhí)行的sql在日志中可見”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI