的执⾏时间
### ⼀、⾸先如需使⽤log4j2,得引⼊maven依赖 ###
org.apache.logging.log4j
log4j-api
${log4j2.version}
org.apache.logging.log4j
log4j-core
${log4j2.version}
org.apache.logging.log4j
log4j-web
${log4j2.version}
注:本⼈使⽤的是2.7版本的log4j2
### ⼆、然后在项⽬的classpath下编写l ###
log4j2在启动的时候会默认加载名为l的配置⽂件
D:/log4j2/mybatis/genertor/logs/
error
fileName="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log"
filePattern="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log.%d{yyyy-MM-dd}.gz">
pattern="%d %-5p (%F:%L) - %m%n"/>
### 三、在l中配置log4j2的加载启动 ###
org.apache.logging.log4j.web.Log4jServletContextListener
log4jServletFilter
org.apache.logging.log4j.web.Log4jServletFilter
log4jServletFilter
/*
REQUEST
FORWARD
INCLUDE
ERROR
注:加上上述配置后,就能在项⽬启动是加载log4j2
### 四、在Mybatis的配置⽂件中,加⼊上述配置 ###
/p>
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
注:settings配置完毕后,即可实现SQL语句的打印!控制台打印如下结果:
2017-10-23 16:38:01,021 DEBUG (BaseJdbcLogger.java:145) - ==> Preparing: SELECT count(*) FROM t_user
2017-10-23 16:38:01,056 DEBUG (BaseJdbcLogger.java:145) - ==> Parameters:
2017-10-23 16:38:01,084 DEBUG (BaseJdbcLogger.java:145) - <== Total: 1
### 五、实现Mybatis官⽅提供的,⽤于记录SQL语句的执⾏时间 ###
package com.stu.interceptor;
import java.sql.Statement;
import java.util.Properties;
import org.utor.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;
/**
* Sql执⾏时间记录
*/
@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 {
long endTime = System.currentTimeMillis();
long sqlCost = endTime - startTime;
System.out.println("执⾏耗时 : [" + sqlCost + "ms ] ");
}
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}
注:Interceptor接⼝是Mybatis官⽅提供的拦截接⼝,创建⼀个类实现该接⼝并重写其三个⽅法并将该类配置在Mybatis的配置⽂件中,即可拦截SQL语句的执⾏过程
### 六、将⼿动编写的配置在Mybatis配置⽂件中: ###
log4j2 配置详解properties/p>
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
### 七、测试SQL的控制台打印结果 ###
2017-10-23 16:38:01,091 DEBUG (BaseJdbcLogger.java:145) - ==> Preparing: select id, username, password, state, isdel, add_time, money, left_money from t_user limit ?,?
2017-10-23 16:38:01,093 DEBUG (BaseJdbcLogger.java:145) - ==> Parameters: 0(Integer), 15(Integer)
2017-10-23 16:38:01,128 DEBUG (BaseJdbcLogger.java:145) - <== Total: 15
执⾏耗时 : [36ms ]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论