配置
#拦截器打印sql开关
mybatis-plus.configuration-properties.sqlLogSwitch=true
#最小打印时间 sql时间超过这个值才打印日志 毫秒
mybatis-plus.configuration-properties.minSize=0
拦截器实现
package org.example.config.mybatisPlus;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/**
* 1.可以用来分析SQL执行效率 2.可以用来获取实际执行的SQL
*/
@Intercepts({
@Signature(type = Executor.class, method = "query", args =
{MappedStatement.class, Object.class, RowBounds.class, ResultHandler.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 = "update", args =
{MappedStatement.class, Object.class})}
)
@Slf4j
@Component
@RequiredArgsConstructor
public class SqlInterceptor implements Interceptor {
//拦截器打印sql开关
boolean sqlLogSwitch;
//最小打印时间 sql时间超过这个值才打印日志 毫秒
long minSize;
private final MybatisPlusProperties configurationProperties;
@Override
public Object intercept(Invocation invocation) {
Object result = null;
long startTime = System.currentTimeMillis();
try {
result = invocation.proceed();
long sqlCostTime = System.currentTimeMillis() - startTime;
if (sqlLogSwitch && (sqlCostTime > minSize)) {
long size = result instanceof Collection<?> ? ((Collection<?>) result).size() : Convert.toLong(result);
SqlLogUtil.excAsync(invocation, size, sqlCostTime);
}
} catch (Exception e) {
log.error("SQL插件执行失败", e);
}
return result;
}
@PostConstruct
public void afterPropertiesSet() {
Properties properties = configurationProperties.getConfigurationProperties();
if (properties == null) {
return;
}
if (properties.containsKey("minSize")) {
minSize = Integer.parseInt(properties.getProperty("minSize"));
}
if (properties.containsKey("sqlLogSwitch")) {
sqlLogSwitch = Boolean.parseBoolean(properties.getProperty("sqlLogSwitch"));
}
}
}
@Slf4j
class SqlLogUtil {
private static final String SQL_LOG =
"""
╔════════════════════════════════ SQL INFO ════════════════════════════════╗
║ 执行耗时: {}ms
║ {}行数: {}
║ 执行方法: {}
║ 执行语句: {}
╚══════════════════════════════════════════════════════════════════════════╝
""";
public static void excAsync(Invocation invocation, Object result, long sqlCostTime) {
ThreadUtil.execAsync(() -> {
paramBuild(invocation, result, sqlCostTime);
}, true);
}
public static void paramBuild(Invocation invocation, Object result, long sqlCostTime) {
log.info("拦截器异步打印sql~");
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object parameter = null;
if (invocation.getArgs().length > 1) {
parameter = invocation.getArgs()[1];
}
String sqlId = mappedStatement.getId();
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
Configuration configuration = mappedStatement.getConfiguration();
String sql = getSql(configuration, boundSql);
formatSqlLog(mappedStatement.getSqlCommandType(), sqlId, sql, sqlCostTime, result);
}
private static String getSql(Configuration configuration, BoundSql boundSql) {
// 输入sql字符串空判断
String sql = boundSql.getSql();
if (StrUtil.isBlank(sql)) {
return "";
}
//去掉换行符
sql = sql.replaceAll("[\\s\n ]+", " ");
//填充占位符, 目前基本不用mybatis存储过程调用,故此处不做考虑
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (!parameterMappings.isEmpty() && parameterObject != null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
sql = replacePlaceholder(sql, parameterObject);
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
for (ParameterMapping parameterMapping : parameterMappings) {
String propertyName = parameterMapping.getProperty();
if (metaObject.hasGetter(propertyName)) {
Object obj = metaObject.getValue(propertyName);
sql = replacePlaceholder(sql, obj);
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = replacePlaceholder(sql, obj);
}
}
}
}
return sql;
}
private static String replacePlaceholder(String sql, Object parameterObject) {
String result;
if (parameterObject == null) {
result = "NULL";
} else if (parameterObject instanceof String) {
result = String.format("'%s'", parameterObject.toString());
} else if (parameterObject instanceof Date) {
result = String.format("'%s'", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(parameterObject));
} else {
result = parameterObject.toString();
}
return sql.replaceFirst("\\?", result);
}
private static void formatSqlLog(SqlCommandType sqlCommandType, String sqlId, String sql, long costTime, Object obj) {
String str = "";
if (sqlCommandType == SqlCommandType.UPDATE ||
sqlCommandType == SqlCommandType.INSERT ||
sqlCommandType == SqlCommandType.DELETE) {
str = "影响";
}
if (sqlCommandType == SqlCommandType.SELECT) {
str = "结果";
}
log.info(SQL_LOG, costTime, str, obj, sqlId, sql);
}
}