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/PrintSqlHelp.java | 123 +++++++++++++++++++++++++++++++++++++++++
1 files changed, 123 insertions(+), 0 deletions(-)
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;
+ }
+}
--
Gitblit v1.8.0