- Maven依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
- 配置文件 application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://47.93.48.199:3306/test
username: root
password: root
logging:
level:
com.lzq.mapper: debug
- 自定义MyBatis的SQL打印拦截器
package com.lzq.config;
import com.mysql.cj.jdbc.ClientPreparedStatement;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;
import java.lang.reflect.Proxy;
import java.sql.Statement;
@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 SQLInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Statement statement;
Object firstArg = invocation.getArgs()[0];
if (Proxy.isProxyClass(firstArg.getClass())) {
statement = (Statement) SystemMetaObject.forObject(firstArg).getValue("h.statement");
} else {
statement = (Statement) firstArg;
}
MetaObject stmtMetaObj = SystemMetaObject.forObject(statement);
ClientPreparedStatement clientPreparedStatement = (ClientPreparedStatement) stmtMetaObj.getValue("delegate");
String originalSql = clientPreparedStatement.asSql();
originalSql = originalSql.replaceAll("[\\s]+", " ");
long start = System.currentTimeMillis();
Object result = invocation.proceed();
long timing = System.currentTimeMillis() - start;
Object realTarget = realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(realTarget);
MappedStatement ms = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
String formatSql = "Time:" + timing + "ms" + "\n" +
"ID:" + ms.getId() + "\n" +
"SQL:" + originalSql;
System.out.println(formatSql);
return result;
}
@Override
public Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
}
return target;
}
@SuppressWarnings("unchecked")
private static <T> T realTarget(Object target) {
if (Proxy.isProxyClass(target.getClass())) {
MetaObject metaObject = SystemMetaObject.forObject(target);
return realTarget(metaObject.getValue("h.target"));
}
return (T) target;
}
}
- 拦截器配置
package com.lzq.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class MybatisConfiguration {
@Bean
public SQLInterceptor mybatisInterceptor() {
SQLInterceptor interceptor = new SQLInterceptor();
Properties properties = new Properties();
interceptor.setProperties(properties);
return interceptor;
}
}