USER-20240325AU\Administrator 1 год назад
Родитель
Сommit
4028e0e691

+ 14 - 2
zd-modules/zd-security/src/main/java/com/zd/security/config/DataScopeInterceptor.java

@@ -34,7 +34,8 @@ public class DataScopeInterceptor implements InnerInterceptor {
     @Override
     @Override
     public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
     public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
                             ResultHandler resultHandler, BoundSql boundSql) {
                             ResultHandler resultHandler, BoundSql boundSql) {
-
+        long startTime = System.currentTimeMillis();
+        log.info("beforeQuery  starting.........,startTime :{}",startTime);
         PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);
         PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);
         String originalSql = boundSql.getSql();
         String originalSql = boundSql.getSql();
 
 
@@ -112,6 +113,9 @@ public class DataScopeInterceptor implements InnerInterceptor {
             session.removeAttribute(key);
             session.removeAttribute(key);
         }
         }
         mpBs.sql(originalSql);
         mpBs.sql(originalSql);
+        long endTime = System.currentTimeMillis();
+        long executionTime = (endTime - startTime) / 1000;
+        log.info("beforeQuery end,endTime:{},takes about {} seconds",endTime,executionTime);
     }
     }
 
 
     private DataPermission getDataPermission(MappedStatement ms) {
     private DataPermission getDataPermission(MappedStatement ms) {
@@ -160,6 +164,8 @@ public class DataScopeInterceptor implements InnerInterceptor {
      * @return
      * @return
      */
      */
     private String sqlHandle(String originalSql, DataPermission dataScope, LoginModel loginUser, boolean isMulti, List<Long> userIds) {
     private String sqlHandle(String originalSql, DataPermission dataScope, LoginModel loginUser, boolean isMulti, List<Long> userIds) {
+        long startTime = System.currentTimeMillis();
+        log.info("sqlHandle starting.........,startTime:{}",startTime);
         String fieldSql = dataScope.field();
         String fieldSql = dataScope.field();
         if(!"".equals(dataScope.tableAlias()) && !"*".equals(dataScope.tableAlias())){
         if(!"".equals(dataScope.tableAlias()) && !"*".equals(dataScope.tableAlias())){
             fieldSql = dataScope.tableAlias()+"."+fieldSql;
             fieldSql = dataScope.tableAlias()+"."+fieldSql;
@@ -181,7 +187,13 @@ public class DataScopeInterceptor implements InnerInterceptor {
         } else {
         } else {
             buffer.append(fieldSql+" = "+loginUser.getUserId());
             buffer.append(fieldSql+" = "+loginUser.getUserId());
         }
         }
-        return makeSqlQuery(dataScope.tableAlias(), originalSql, buffer.toString());
+
+        long endTime = System.currentTimeMillis();
+        long executionTime = (endTime - startTime) / 1000;
+
+        String  result = makeSqlQuery(dataScope.tableAlias(), originalSql, buffer.toString());
+        log.info("sqlHandle  end,endTime:{},takes about {} seconds",endTime,executionTime);
+        return result;
     }
     }
 
 
     /**
     /**

+ 2 - 0
zd-modules/zd-security/src/main/java/com/zd/security/config/MybatisPlusConfig.java

@@ -3,6 +3,7 @@ package com.zd.security.config;
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Configuration;
 
 
@@ -14,6 +15,7 @@ import org.springframework.context.annotation.Configuration;
  * @since:
  * @since:
  */
  */
 @Configuration
 @Configuration
+@MapperScan("com.zd.security.mapper")
 public class MybatisPlusConfig {
 public class MybatisPlusConfig {
 
 
     @Bean
     @Bean

+ 26 - 0
zd-modules/zd-security/src/main/java/com/zd/security/config/SqlCostAspect.java

@@ -0,0 +1,26 @@
+package com.zd.security.config;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.*;
+import org.springframework.stereotype.Component;
+
+@Aspect
+@Component
+@Slf4j(topic = "sql aop - ")
+public class SqlCostAspect {
+    @Pointcut("execution(* com.zd.security.mapper.*.*(..))")
+    public void mapperPointcut() {}
+
+    @Around("mapperPointcut()")
+    public Object aroundMapper(ProceedingJoinPoint joinPoint) throws Throwable {
+        long startTime = System.currentTimeMillis();
+        Object result = joinPoint.proceed();
+        long endTime = System.currentTimeMillis();
+        long costTime = (endTime - startTime) / 1000;
+
+        String methodName = joinPoint.getSignature().toShortString();
+        System.out.println("SQL 执行耗时:" + costTime + " s, Method: " + methodName);
+        log.info("The sql execution took {} seconds,Method:{}",costTime,methodName);
+        return result;
+    }
+}

+ 30 - 0
zd-modules/zd-security/src/main/java/com/zd/security/config/SqlCostInterceptor.java

@@ -0,0 +1,30 @@
+package com.zd.security.config;
+
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.*;
+
+import org.apache.ibatis.executor.Executor;
+
+@Intercepts({
+        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
+        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class})
+})
+public class SqlCostInterceptor implements Interceptor {
+    @Override
+    public Object intercept(Invocation invocation) throws Throwable {
+        long startTime = System.currentTimeMillis();
+        Object result = invocation.proceed();
+        long endTime = System.currentTimeMillis();
+        long costTime = endTime - startTime;
+
+        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
+        String sqlId = mappedStatement.getId();
+        System.out.println("SQL 执行耗时:" + costTime + "ms, SQL ID: " + sqlId);
+        return result;
+    }
+
+    @Override
+    public Object plugin(Object target) {
+        return Plugin.wrap(target, this);
+    }
+}