|
@@ -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) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|