From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java | 160 +++++++++--------------------------------------------
1 files changed, 28 insertions(+), 132 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..9ffada1 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.error("SQL Id: {}", sqlId);
+ log.error("SQL Parameters: {}", boundSql.getParameterObject());
+ log.error("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;
- }
}
--
Gitblit v1.8.0