用MyBatis拦截器记录SQL执行时间
来源:互联网 发布:喝咖啡的利弊 知乎 编辑:程序博客网 时间:2024/04/30 14:12
1、在项目的applicationContext-dao.xml 文件中增加一个plugins:sqlStatementInterceptor
<bean id="dao-sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"> <list> <value>classpath:com/test/mapper/*.xml</value> <value>classpath:com/test/mapper/extend/*.xml</value> </list> </property> <property name="plugins"> <array> <bean id="sqlStatementInterceptor" class="com.test.SqlStatementInterceptor"/> </array> </property></bean>
2、编写sqlStatementInterceptor的实现代码
package com.test;import java.util.Properties;import org.apache.ibatis.cache.CacheKey;import org.apache.ibatis.executor.Executor;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;/** * 数据库操作性能拦截器,记录耗时 */@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 SqlStatementInterceptor implements Interceptor{ private static Logger logger = LoggerFactory.getLogger(SqlStatementInterceptor.class); private Properties properties; @Override public Object intercept(Invocation arg0) throws Throwable { MappedStatement mappedStatement = (MappedStatement) arg0.getArgs()[0]; String sqlId = mappedStatement.getId(); Object returnValue; long start = System.currentTimeMillis(); returnValue = arg0.proceed(); long end = System.currentTimeMillis(); long time = end - start; StringBuilder str = new StringBuilder(100); str.append(sqlId); str.append(": "); str.append("cost time "); str.append(time); str.append(" ms."); String sqlInfo = str.toString(); logger.debug(sqlInfo); return returnValue; } @Override public Object plugin(Object arg0) { return Plugin.wrap(arg0, this); } @Override public void setProperties(Properties arg0) { this.properties = arg0; }}
3、对sqlStatementInterceptor.java类里面的输出级别(上面代码块是debug级别),进行相应的logback.xml设置(只需修改文件内的level标签)
4、启动项目,从log文件中可看到日志打印
[http-8080-5] DEBUG [Caller+0 at com.test.SqlStatementInterceptor.intercept(SqlStatementInterceptor.java:49) ] - com.test.testMapper.selectByExample: cost time 1 ms.
参考文章:
https://my.oschina.net/u/2265143/blog/786295
http://blog.csdn.net/tq02h2a/article/details/50772652
0 0
- 用MyBatis拦截器记录SQL执行时间
- java利用mybatis拦截器统计sql执行时间示例
- mybatis的拦截器实现统计分析sql的执行时间
- mybatis的拦截器实现统计分析sql的执行时间
- mybatis拦截sql,日志输出sql详情及其执行时间
- javaweb用拦截器计算action执行时间
- mybatis 拦截器拦截所有sql
- Mybatis SQL拦截器实现
- mysql如何记录sql执行时间
- Mybatis拦截器实现SQL性能监控
- Mybatis拦截器实现SQL性能监控
- Mybatis拦截器实现SQL性能监控
- mybatis 拦截器获取sql语句
- mybatis插件-打印 SQL 及其执行时间
- Postgres log_duration设置记录SQL执行时间
- Mybatis 拦截器实现SQL性能监控配置
- jeesite mybatis拦截器sql语句与activemq的使用
- 【myBatis】Mybatis中的拦截器
- 快捷键总结
- 如何构建一个简单链表
- 利用SecureCRT的VBScript进行自动化控制
- UWP入门(六)-- ResourceDictionary 和 XAML 资源引用
- SSL P2384 2014年初中竞赛试题(南海) 字符串
- 用MyBatis拦截器记录SQL执行时间
- Python3-strip()
- GYM 101147 F.Bishops Alliance(dp+BIT)
- 交换排序
- UVALive 4225 Prime Bases 贪心
- gmapping链接收集
- ACM程序设计题目 Problem M-13
- Sublime Text 设置
- 栈的链式存储结构操作及实现