MyBatis
SQL 执行计时器 的实现
拦截器文件
SqlCostInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42package com.sixi.enquiry.interceptor.mybatis;
import java.sql.Statement;
import java.util.Properties;
import org.apache.ibatis.executor.statement.StatementHandler;
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.ResultHandler;
import lombok.extern.slf4j.Slf4j;
/**
* @author MiaoWoo
*/
public class SqlCostInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
long startTime = System.currentTimeMillis();
try {
return invocation.proceed();
} finally {
log.info("执行耗时 : [" + (System.currentTimeMillis() - startTime) + "ms ] ");
}
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}配置类注入拦截器
1 | package com.sixi.enquiry.config; |
- 运行效果
1
2
3
42018-10-16 11:16:20.031 DEBUG [wechatapp-enquiry-service] --- 10236 [restartedMain] c.s.enquiry.mapper.ConfigMapper.get : ==> Preparing: SELECT `value` FROM config WHERE `key` = ? AND is_deleted = 0
2018-10-16 11:16:20.051 DEBUG [wechatapp-enquiry-service] --- 10236 [restartedMain] c.s.enquiry.mapper.ConfigMapper.get : ==> Parameters: REPLEASE_RULES(String)
2018-10-16 11:16:20.068 DEBUG [wechatapp-enquiry-service] --- 10236 [restartedMain] c.s.enquiry.mapper.ConfigMapper.get : <== Total: 1
2018-10-16 11:16:20.068 INFO [wechatapp-enquiry-service] --- 10236 [restartedMain] c.s.e.i.mybatis.SqlCostInterceptor : 执行耗时 : [17ms ]