From 9b4433fddf5b401edb0aace8a404ac733b122702 Mon Sep 17 00:00:00 2001 From: 田源 <tianyuan@vci-tech.com> Date: 星期四, 03 四月 2025 14:35:02 +0800 Subject: [PATCH] 添加非密字段显示 --- Source/BladeX-Tool/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 164 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java b/Source/BladeX-Tool/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java new file mode 100644 index 0000000..234ab4c --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-mybatis/src/main/java/org/springblade/core/mp/plugins/SqlLogInterceptor.java @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2018-2028, DreamLu All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com) + */ +package org.springblade.core.mp.plugins; + +import com.alibaba.druid.DbType; +import com.alibaba.druid.filter.FilterChain; +import com.alibaba.druid.filter.FilterEventAdapter; +import com.alibaba.druid.proxy.jdbc.JdbcParameter; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.proxy.jdbc.StatementProxy; +import com.alibaba.druid.sql.SQLUtils; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.mp.props.MybatisPlusProperties; +import org.springblade.core.tool.utils.StringUtil; + +import java.time.temporal.TemporalAccessor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 鎵撳嵃鍙墽琛岀殑 sql 鏃ュ織 + * + * @author L.cm锛孋hill + */ +@Slf4j +public class SqlLogInterceptor extends FilterEventAdapter { + private static final SQLUtils.FormatOption FORMAT_OPTION = new SQLUtils.FormatOption(false, false); + + private static final List<String> SQL_LOG_EXCLUDE = new ArrayList<>(Arrays.asList("ACT_RU_JOB", "ACT_RU_TIMER_JOB")); + + private final MybatisPlusProperties properties; + + public SqlLogInterceptor(MybatisPlusProperties properties) { + this.properties = properties; + if (properties.getSqlLogExclude().size() > 0) { + SQL_LOG_EXCLUDE.addAll(properties.getSqlLogExclude()); + } + } + + @Override + protected void statementExecuteBefore(StatementProxy statement, String sql) { + statement.setLastExecuteStartNano(); + } + + @Override + protected void statementExecuteBatchBefore(StatementProxy statement) { + statement.setLastExecuteStartNano(); + } + + @Override + protected void statementExecuteUpdateBefore(StatementProxy statement, String sql) { + statement.setLastExecuteStartNano(); + } + + @Override + protected void statementExecuteQueryBefore(StatementProxy statement, String sql) { + statement.setLastExecuteStartNano(); + } + + @Override + protected void statementExecuteAfter(StatementProxy statement, String sql, boolean firstResult) { + statement.setLastExecuteTimeNano(); + } + + @Override + protected void statementExecuteBatchAfter(StatementProxy statement, int[] result) { + statement.setLastExecuteTimeNano(); + } + + @Override + protected void statementExecuteQueryAfter(StatementProxy statement, String sql, ResultSetProxy resultSet) { + statement.setLastExecuteTimeNano(); + } + + @Override + protected void statementExecuteUpdateAfter(StatementProxy statement, String sql, int updateCount) { + statement.setLastExecuteTimeNano(); + } + + @Override + @SneakyThrows + public void statement_close(FilterChain chain, StatementProxy statement) { + // 鏄惁寮�鍚棩蹇� + if (!properties.getSqlLog()) { + chain.statement_close(statement); + return; + } + // 鏄惁寮�鍚皟璇� + if (!log.isInfoEnabled()) { + chain.statement_close(statement); + return; + } + // 鎵撳嵃鍙墽琛岀殑 sql + String sql = statement.getBatchSql(); + // sql 涓虹┖鐩存帴杩斿洖 + if (StringUtil.isEmpty(sql)) { + chain.statement_close(statement); + return; + } + // sql 鍖呭惈鎺掗櫎鐨勫叧閿瓧鐩存帴杩斿洖 + if (excludeSql(sql)) { + chain.statement_close(statement); + return; + } + int parametersSize = statement.getParametersSize(); + List<Object> parameters = new ArrayList<>(parametersSize); + for (int i = 0; i < parametersSize; ++i) { + // 杞崲鍙傛暟锛屽鐞� java8 鏃堕棿 + parameters.add(getJdbcParameter(statement.getParameter(i))); + } + String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType(); + String formattedSql = SQLUtils.format(sql, DbType.of(dbType), parameters, FORMAT_OPTION); + printSql(formattedSql, statement); + chain.statement_close(statement); + } + + private static Object getJdbcParameter(JdbcParameter jdbcParam) { + if (jdbcParam == null) { + return null; + } + Object value = jdbcParam.getValue(); + // 澶勭悊 java8 鏃堕棿 + if (value instanceof TemporalAccessor) { + return value.toString(); + } + return value; + } + + private static void printSql(String sql, StatementProxy statement) { + // 鎵撳嵃 sql + String sqlLogger = "\n\n============== Sql Start ==============" + + "\nExecute SQL : {}" + + "\nExecute Time: {}" + + "\n============== Sql End ==============\n"; + log.info(sqlLogger, sql.trim(), StringUtil.format(statement.getLastExecuteTimeNano())); + } + + private static boolean excludeSql(String sql) { + // 鍒ゆ柇鍏抽敭瀛� + for (String exclude : SQL_LOG_EXCLUDE) { + if (sql.contains(exclude)) { + return true; + } + } + return false; + } + +} -- Gitblit v1.9.3