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 | 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