From 70b2c7a1f5b54cf9157d8fce4d6a9b0f2fadaebe Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期二, 15 四月 2025 17:13:28 +0800
Subject: [PATCH] 获取终止操作结果

---
 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java |  106 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 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
new file mode 100644
index 0000000..9ffada1
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintExceptionSqlInterceptor.java
@@ -0,0 +1,106 @@
+package com.dy.common.mybatis;
+
+import java.lang.reflect.InvocationTargetException;
+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.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.session.Configuration;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+
+/**
+ * 鎷︽埅鎵цSQL鍙戠敓寮傚父鐨勫満鏅紝骞跺皢鎵ц閿欒锛屾墽琛孲QL鍜屽弬鏁版墦鍗板嚭鏉�
+ * 鎷︽埅Executor閲岄潰鐨剄uery鍜寀pdate鏂规硶
+ */
+@Intercepts({
+        @Signature(
+            method = "query",
+            type = Executor.class,
+            args = {
+                MappedStatement.class,
+                Object.class,
+                RowBounds.class,
+                ResultHandler.class
+            }
+        ),
+        @Signature(
+            type = Executor.class,
+            method = "update",
+            args = {
+                MappedStatement.class,
+                Object.class
+            }
+        )
+})
+@Slf4j
+public class PrintExceptionSqlInterceptor implements Interceptor {
+
+    @Override
+    public Object intercept(Invocation invocation) throws Throwable {
+        // 鑾峰彇鎵ц鏂规硶鐨凪appedStatement鍙傛暟
+        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();
+        }
+    }
+
+    /**
+     * 閫氳繃璇ユ柟娉曞喅瀹氳杩斿洖鐨勫璞℃槸鐩爣瀵硅薄杩樻槸瀵瑰簲鐨勪唬鐞�
+     * 涓嶈鎯崇殑澶鏉傦紝涓�鑸氨涓ょ鎯呭喌锛�
+     * <p>
+     * 1. return target;  鐩存帴杩斿洖鐩爣瀵硅薄锛岀浉褰撲簬褰撳墠Interceptor娌¤捣浣滅敤锛屼笉浼氳皟鐢ㄤ笂闈㈢殑intercept()鏂规硶
+     * 2. return Plugin.wrap(target, this);  杩斿洖浠g悊瀵硅薄锛屼細璋冪敤涓婇潰鐨刬ntercept()鏂规硶
+     *
+     * @param target 鐩爣瀵硅薄
+     * @return 鐩爣瀵硅薄鎴栬�呬唬鐞嗗璞�
+     */
+    @Override
+    public Object plugin(Object target) {
+        return Plugin.wrap(target, this);
+    }
+
+    /**
+     * 鐢ㄤ簬鑾峰彇鍦–onfiguration鍒濆鍖栧綋鍓嶇殑Interceptor鏃舵椂鍊欒缃殑涓�浜涘弬鏁�
+     *
+     * @param properties Properties鍙傛暟
+     */
+    @Override
+    public void setProperties(Properties properties) {
+    }
+
+
+}

--
Gitblit v1.8.0