喵♂呜 的博客

一个刚毕业就当爹的程序猿 正在迷雾中寻找道路...

MyBatis执行耗时统计

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
    42
    package 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
    */
    @Slf4j
    @Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
    @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
    @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})})
    public class SqlCostInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
    long startTime = System.currentTimeMillis();
    try {
    return invocation.proceed();
    } finally {
    log.info("执行耗时 : [" + (System.currentTimeMillis() - startTime) + "ms ] ");
    }
    }

    @Override
    public Object plugin(Object target) {
    return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
    }
    }
  • 配置类注入拦截器

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
package com.sixi.enquiry.config;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sixi.enquiry.interceptor.mybatis.SqlCostInterceptor;

@Configuration
public class SqlDataBaseConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setPlugins(plugins());
return sessionFactory.getObject();
}

private Interceptor[] plugins() {
return new Interceptor[]{new SqlCostInterceptor()};
}
}
  • 运行效果
    1
    2
    3
    4
    2018-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 ]

欢迎关注我的其它发布渠道