From 8c58db9a4b3ec49eebfdaf642b0298a482cd987a Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期五, 13 十二月 2024 10:41:45 +0800 Subject: [PATCH] 完善SQL日志输出实现 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlHelp.java | 123 ++++++++++++++++++++++++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java | 16 ++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java | 160 +++++-------------------------- 3 files changed, 163 insertions(+), 136 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java index 96dc6f3..b3e1e8e 100644 --- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java @@ -1,27 +1,20 @@ package com.dy.common.mybatis; import java.lang.reflect.InvocationTargetException; -import java.text.DateFormat; -import java.util.Date; -import java.util.List; -import java.util.Locale; import java.util.Properties; import lombok.extern.slf4j.Slf4j; 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; /** * 鎷︽埅鎵цSQL鍙戠敓寮傚父鐨勫満鏅紝骞跺皢鎵ц閿欒锛屾墽琛孲QL鍜屽弬鏁版墦鍗板嚭鏉� @@ -53,33 +46,36 @@ @Override public Object intercept(Invocation invocation) throws Throwable { // 鑾峰彇鎵ц鏂规硶鐨凪appedStatement鍙傛暟 - 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(); - Object response; - try { - response = invocation.proceed(); - } catch (Exception e) { - // 杈撳嚭SQL寮傚父淇℃伅 - log.error("SQL ErrorException:", e); - log.info("SQL Parameters: {}", boundSql.getParameterObject()); - log.info("Execute SqlId: {}", sqlId); - log.info("Completely Execute SQL: {}", getFullSql(configuration, boundSql)); - // 鏍规嵁婧愬紓甯哥被鍨嬭繘琛岃繑鍥� - if (e instanceof InvocationTargetException) { - throw new InvocationTargetException(e); - } else if (e instanceof IllegalAccessException) { - throw new IllegalAccessException(e.getMessage()); - } else { - throw new RuntimeException(e); + Object[] args = invocation.getArgs(); + if(args != null && args.length > 1){ + MappedStatement mappedStatement = (MappedStatement) args[0]; + Object parameter = args[1]; + + String sqlId = mappedStatement.getId(); + BoundSql boundSql = mappedStatement.getBoundSql(parameter); + Configuration configuration = mappedStatement.getConfiguration(); + Object response; + try { + response = invocation.proceed(); + } catch (Exception e) { + // 杈撳嚭SQL寮傚父淇℃伅 + log.error("SQL ErrorException:", e); + log.info("SQL Parameters: {}", boundSql.getParameterObject()); + log.info("SQL Id: {}", sqlId); + log.info("SQL: {}", PrintSqlHelp.getFullSql(configuration, boundSql)); + // 鏍规嵁婧愬紓甯哥被鍨嬭繘琛岃繑鍥� + if (e instanceof InvocationTargetException) { + throw new InvocationTargetException(e); + } else if (e instanceof IllegalAccessException) { + throw new IllegalAccessException(e.getMessage()); + } else { + throw new RuntimeException(e); + } } + return response; + }else{ + return invocation.proceed(); } - return response; } /** @@ -106,105 +102,5 @@ public void setProperties(Properties properties) { } - - /** - * 杞箟姝e垯鐗规畩瀛楃 锛�$()*+.[]?\^{} - * \\闇�瑕佺涓�涓浛鎹紝鍚﹀垯replace鏂规硶鏇挎崲鏃朵細鏈夐�昏緫bug - */ - private static String makeQueryStringAllRegExp(String str) { - if (str != null && !"".equals(str)) { - return str.replace("\\", "\\\\") - .replace("*", "\\*") - .replace("+", "\\+") - .replace("|", "\\|") - .replace("{", "\\{") - .replace("}", "\\}") - .replace("(", "\\(") - .replace(")", "\\)") - .replace("^", "\\^") - .replace("$", "\\$") - .replace("[", "\\[") - .replace("]", "\\]") - .replace("?", "\\?") - .replace(",", "\\,") - .replace(".", "\\.") - .replace("&", "\\&"); - } - return str; - } - - /** - * 鑾峰彇鍙傛暟瀵瑰簲鐨剆tring鍊� - * - * @param obj 鍙傛暟瀵瑰簲鐨勫�� - * @return string - */ - private static String getParameterValue(Object obj) { - String value; - if (obj instanceof String) { - value = "'" + obj + "'"; - } else if (obj instanceof Date) { - DateFormat formatter = - DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); - value = "'" + formatter.format(obj) + "'"; - } else { - if (obj != null) { - value = obj.toString(); - } else { - value = ""; - } - } - // 瀵圭壒娈婂瓧绗﹁繘琛岃浆涔夛紝鏂逛究涔嬪悗澶勭悊鏇挎崲 - return value != null ? makeQueryStringAllRegExp(value) : ""; - } - - /** - * 鑾峰彇瀹屾暣鐨勬墽琛孲QL - */ - public static String getFullSql(Configuration configuration, BoundSql boundSql) { - try { - return parseAndExtractFullSql(configuration, boundSql); - } catch (Exception e) { - // 濡傛灉瑙f瀽澶辫触杩斿洖鍘熷SQL - return boundSql.getSql(); - } - } - - /** - * 缁勮瀹屾暣鐨剆ql璇彞骞舵妸瀵瑰簲鐨勫弬鏁伴兘浠e叆鍒皊ql璇彞閲岄潰 - * - * @param configuration Configuration - * @param boundSql BoundSql - * @return sql瀹屾暣璇彞 - */ - private static String parseAndExtractFullSql(Configuration configuration, BoundSql boundSql) { - // 鑾峰彇mapper閲岄潰鏂规硶涓婄殑鍙傛暟 - Object sqlParameter = boundSql.getParameterObject(); - // sql璇彞閲岄潰闇�瑕佺殑鍙傛暟 - List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); - // sql鍘熷璇彞(?杩樻病鏈夋浛鎹㈡垚鎴戜滑鍏蜂綋鐨勫弬鏁�) - String sql = boundSql.getSql().replaceAll("[\\s]+", " "); - if (!parameterMappings.isEmpty() && sqlParameter != null) { - // sql璇彞閲岄潰鐨�?鏇挎崲鎴愮湡瀹炵殑鍙傛暟 - 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; - } } diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlHelp.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlHelp.java new file mode 100644 index 0000000..b25921d --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlHelp.java @@ -0,0 +1,123 @@ +package com.dy.common.mybatis; + +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.ParameterMapping; +import org.apache.ibatis.reflection.MetaObject; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.TypeHandlerRegistry; + +import java.text.DateFormat; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +/** + * @Author: liurunyu + * @Date: 2024/12/13 10:39 + * @Description + */ +public class PrintSqlHelp { + + /** + * 鑾峰彇瀹屾暣鐨勬墽琛孲QL + */ + public static String getFullSql(Configuration configuration, BoundSql boundSql) { + try { + return parseAndExtractFullSql(configuration, boundSql); + } catch (Exception e) { + // 濡傛灉瑙f瀽澶辫触杩斿洖鍘熷SQL + return boundSql.getSql(); + } + } + + /** + * 缁勮瀹屾暣鐨剆ql璇彞骞舵妸瀵瑰簲鐨勫弬鏁伴兘浠e叆鍒皊ql璇彞閲岄潰 + * + * @param configuration Configuration + * @param boundSql BoundSql + * @return sql瀹屾暣璇彞 + */ + private static String parseAndExtractFullSql(Configuration configuration, BoundSql boundSql) { + // 鑾峰彇mapper閲岄潰鏂规硶涓婄殑鍙傛暟 + Object sqlParameter = boundSql.getParameterObject(); + // sql璇彞閲岄潰闇�瑕佺殑鍙傛暟 + List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); + // sql鍘熷璇彞(?杩樻病鏈夋浛鎹㈡垚鎴戜滑鍏蜂綋鐨勫弬鏁�) + String sql = boundSql.getSql().replaceAll("[\\s]+", " "); + if (!parameterMappings.isEmpty() && sqlParameter != null) { + // sql璇彞閲岄潰鐨�?鏇挎崲鎴愮湡瀹炵殑鍙傛暟 + 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; + } + + + /** + * 鑾峰彇鍙傛暟瀵瑰簲鐨剆tring鍊� + * + * @param obj 鍙傛暟瀵瑰簲鐨勫�� + * @return string + */ + private static String getParameterValue(Object obj) { + String value; + if (obj instanceof String) { + value = "'" + obj + "'"; + } else if (obj instanceof Date) { + DateFormat formatter = + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); + value = "'" + formatter.format(obj) + "'"; + } else { + if (obj != null) { + value = obj.toString(); + } else { + value = ""; + } + } + // 瀵圭壒娈婂瓧绗﹁繘琛岃浆涔夛紝鏂逛究涔嬪悗澶勭悊鏇挎崲 + return value != null ? makeQueryStringAllRegExp(value) : ""; + } + + + + /** + * 杞箟姝e垯鐗规畩瀛楃 锛�$()*+.[]?\^{} + * \\闇�瑕佺涓�涓浛鎹紝鍚﹀垯replace鏂规硶鏇挎崲鏃朵細鏈夐�昏緫bug + */ + private static String makeQueryStringAllRegExp(String str) { + if (str != null && !"".equals(str)) { + return str.replace("\\", "\\\\") + .replace("*", "\\*") + .replace("+", "\\+") + .replace("|", "\\|") + .replace("{", "\\{") + .replace("}", "\\}") + .replace("(", "\\(") + .replace(")", "\\)") + .replace("^", "\\^") + .replace("$", "\\$") + .replace("[", "\\[") + .replace("]", "\\]") + .replace("?", "\\?") + .replace(",", "\\,") + .replace(".", "\\.") + .replace("&", "\\&"); + } + return str; + } +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java index ad7db0b..a3775cd 100644 --- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java @@ -8,6 +8,7 @@ import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; @@ -49,13 +50,20 @@ if (args != null && args.length > 1) { MappedStatement mappedStatement = (MappedStatement) args[0]; Object parameter = args[1]; - BoundSql boundSql = mappedStatement.getBoundSql(parameter); - String sql = boundSql.getSql(); - log.info("\n\nSQL锛歕n" + sql + "\n"); - //System.out.println("SQL璇彞锛�" + sql); + //BoundSql boundSql = mappedStatement.getBoundSql(parameter); + //String sql = boundSql.getSql(); + //log.info("\n\nSQL锛歕n" + sql + "\n"); + + String sqlId = mappedStatement.getId(); + BoundSql boundSql = mappedStatement.getBoundSql(parameter); + Configuration configuration = mappedStatement.getConfiguration(); + log.info("SQL Parameters: {}", boundSql.getParameterObject()); + log.info("SQL Id: {}", sqlId); + log.info("SQL: {}", PrintSqlHelp.getFullSql(configuration, boundSql)); } return invocation.proceed(); } + } \ No newline at end of file -- Gitblit v1.8.0