瀏覽代碼

mybatis profiler插件

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 11 月之前
父節點
當前提交
315b3b9f8b

+ 9 - 0
data-mybatis-plus/src/main/java/com/qmth/boot/mybatis/config/MybatisPlusAutoConfiguration.java

@@ -7,8 +7,11 @@ import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import com.qmth.boot.core.constant.CoreConstant;
 import com.qmth.boot.core.logger.context.LoggerContextService;
 import com.qmth.boot.mybatis.logger.MybatisPlusLogger;
+import com.qmth.boot.mybatis.plugin.MybatisProfilerPlugin;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
@@ -55,4 +58,10 @@ public class MybatisPlusAutoConfiguration {
             return DbType.OTHER;
         }
     }
+
+    @Bean
+    @ConditionalOnProperty(value = CoreConstant.CONFIG_PREFIX + ".mybatis.profiler.enable", havingValue = "true")
+    public MybatisProfilerPlugin mybatisProfilerPlugin(@NotNull MybatisProperties mybatisProperties) {
+        return new MybatisProfilerPlugin(mybatisProperties.getProfiler());
+    }
 }

+ 11 - 0
data-mybatis-plus/src/main/java/com/qmth/boot/mybatis/config/MybatisProperties.java

@@ -17,6 +17,9 @@ public class MybatisProperties {
 
     private boolean schemaValidate = true;
 
+    @NotNull
+    private ProfilerProperties profiler = new ProfilerProperties();
+
     @NotNull
     private String logLevel = LoggerConstant.DEFAULT_LEVEL;
 
@@ -43,4 +46,12 @@ public class MybatisProperties {
     public void setSchemaValidate(boolean schemaValidate) {
         this.schemaValidate = schemaValidate;
     }
+
+    public ProfilerProperties getProfiler() {
+        return profiler;
+    }
+
+    public void setProfiler(ProfilerProperties profiler) {
+        this.profiler = profiler;
+    }
 }

+ 33 - 0
data-mybatis-plus/src/main/java/com/qmth/boot/mybatis/config/ProfilerProperties.java

@@ -0,0 +1,33 @@
+package com.qmth.boot.mybatis.config;
+
+import org.hibernate.validator.constraints.time.DurationMin;
+import org.springframework.validation.annotation.Validated;
+
+import javax.validation.constraints.NotNull;
+import java.time.Duration;
+
+@Validated
+public class ProfilerProperties {
+
+    private boolean enable = false;
+
+    @NotNull
+    @DurationMin(millis = 0)
+    private Duration threshold = Duration.ofMillis(0);
+
+    public boolean isEnable() {
+        return enable;
+    }
+
+    public void setEnable(boolean enable) {
+        this.enable = enable;
+    }
+
+    public Duration getThreshold() {
+        return threshold;
+    }
+
+    public void setThreshold(Duration threshold) {
+        this.threshold = threshold;
+    }
+}

+ 58 - 0
data-mybatis-plus/src/main/java/com/qmth/boot/mybatis/plugin/MybatisProfilerPlugin.java

@@ -0,0 +1,58 @@
+package com.qmth.boot.mybatis.plugin;
+
+import com.qmth.boot.mybatis.config.ProfilerProperties;
+import org.apache.ibatis.cache.CacheKey;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.executor.statement.StatementHandler;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.*;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Properties;
+
+@Intercepts(value = {
+        @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
+        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
+                RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class }),
+        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
+                RowBounds.class, ResultHandler.class }) })
+public class MybatisProfilerPlugin implements Interceptor {
+
+    private static final Logger log = LoggerFactory.getLogger(MybatisProfilerPlugin.class);
+
+    private ProfilerProperties profilerProperties;
+
+    public MybatisProfilerPlugin(ProfilerProperties profilerProperties) {
+        this.profilerProperties = profilerProperties;
+        log.info("Mybatis profiler plugin inited.");
+    }
+
+    @Override
+    public Object intercept(Invocation invocation) throws Throwable {
+        long start = System.currentTimeMillis();
+        try {
+            return invocation.proceed();
+        } finally {
+            long cost = System.currentTimeMillis() - start;
+            if (profilerProperties.isEnable() && cost > profilerProperties.getThreshold().toMillis()) {
+                StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
+                String sql = statementHandler.getBoundSql().getSql();
+                log.info("Sql time cost: " + cost + "ms\n" + sql);
+            }
+        }
+    }
+
+    @Override
+    public Object plugin(Object target) {
+        return Plugin.wrap(target, this);
+    }
+
+    @Override
+    public void setProperties(Properties properties) {
+
+    }
+}