From 90518d09cc278d3304ccd5fc122269d89854b656 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期五, 13 十二月 2024 10:18:33 +0800
Subject: [PATCH] 增加SQL日志输出mybatis拦截器,并在application-global.xml中增加是否输出sql配置项
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java | 61 ++++++++++++++++++++++++++++++
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/MyBatisConfig.java | 6 +++
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml | 5 ++
3 files changed, 71 insertions(+), 1 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/MyBatisConfig.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/MyBatisConfig.java
index fed35d1..60eef8c 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/MyBatisConfig.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/MyBatisConfig.java
@@ -4,6 +4,7 @@
import jakarta.annotation.PostConstruct;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -15,6 +16,8 @@
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
+ @Value("${mybatis-plus.configuration.print-sql}")
+ private boolean printSql;
/**
* 涓や釜鎷︽埅鍣紝鑷姩鐢熸垚ID锛屽紓甯告椂杈撳嚭SQL
*/
@@ -23,6 +26,9 @@
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(new AutoGenerateIdInterceptor());
sqlSessionFactory.getConfiguration().addInterceptor(new PrintExceptionSqlInterceptor());
+ if (printSql) {
+ sqlSessionFactory.getConfiguration().addInterceptor(new PrintSqlInterceptor());
+ }
}
}
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
new file mode 100644
index 0000000..ad7db0b
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java
@@ -0,0 +1,61 @@
+package com.dy.common.mybatis;
+
+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.Signature;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/12/13 9:53
+ * @Description
+ */
+/**
+ * 鎷︽埅鎵ц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 PrintSqlInterceptor implements Interceptor {
+ @Override
+ public Object intercept(Invocation invocation) throws Throwable {
+ Object[] args = invocation.getArgs();
+
+ 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);
+ }
+
+ return invocation.proceed();
+ }
+}
\ No newline at end of file
diff --git a/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml b/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
index 8cd0186..a0ce5e1 100644
--- a/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
+++ b/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -65,7 +65,8 @@
#type-aliases-package: com.dy.pipIrrGlobal.daoBa
configuration:
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #鏃惰�岃緭鍑烘棩蹇楋紝鏃惰�屼笉杈撳嚭鏃ュ織
- log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl
+ #log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl 涓嶅啀搴旂敤锛屾敼涓篜rintSqlInterceptor鎷︽埅鍣ㄨ緭鍑簊ql鏃ュ織锛屼笅闈rint-sql椤归厤缃�
+ print-sql: true # 杈撳嚭sql鏃ュ織寮�鍏筹紝鎺у埗PrintSqlInterceptor鎷︽埅鍣ㄦ槸鍚﹀姞杞�
#寮�鍚┘宄皍Name鑷姩鏄犲皠鍒皍_name
#2023-10-24缁忓疄楠岋紝涓嬮潰閰嶇疆true鎴杅alse锛岄兘鑳戒粠u_name鏄犲皠鍒皍Name
#map-underscore-to-camel-case: true
@@ -79,6 +80,8 @@
global:
dev: false #鏄惁寮�鍙戦樁娈碉紝true鎴杅alse
dsName: ym #寮�鍙戦樁娈碉紝璁剧疆涓存椂鐨勬暟鎹簱鍚嶇О
+ sql:
+ print: true #鏄惁鎵撳嵃SQL璇彞锛宼rue鎴杅alse
nginx:
webPort: 54321
mw:
--
Gitblit v1.8.0