1、mybatis-config.xml配置plugin
<plugins>
<plugin interceptor="com.jd.united.marketing.dao.plugins.SqlMonitorPlugin"></plugin>
</plugins>
注意事项:
确保mybatis-config.xml中标签顺序
(properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)
2、写一个拦截器
package com.jd.united.marketing.dao.plugins;
import com.jd.united.marketing.common.enums.LogTypeEnum;
import org.apache.commons.lang3.StringUtils;
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 java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;
/**
* SQL日志拦截和监控
*
* @author wolf_love666
* @create 2019-03-07 18:51
**/
@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})})
public class SqlMonitorPlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
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();
long startTime = System.currentTimeMillis();
Object result = null;
try {
result = invocation.proceed();
} finally {
long sqlCostTime = System.currentTimeMillis() - startTime;
String sql = this.getSql(configuration, boundSql);
this.formatSqlLog(mappedStatement.getSqlCommandType(), sqlId, sql, sqlCostTime, result);
}
return result;
}
@Override
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
}
return target;
}
@Override
public void setProperties(Properties properties) {
}
/**
* 获取完整的sql语句
*
* @param configuration
* @param boundSql
* @return
*/
private String getSql(Configuration configuration, BoundSql boundSql) {
// 输入sql字符串空判断
String sql = boundSql.getSql();
if (StringUtils.isBlank(sql)) {
return "";
}
//美化sql
sql = this.beautifySql(sql);
//填充占位符, 目前基本不用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 = this.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 = this.replacePlaceholder(sql, obj);
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = this.replacePlaceholder(sql, obj);
}
}
}
}
return sql;
}
/**
* 美化Sql
*/
private String beautifySql(String sql) {
return sql.replaceAll("[\\s\n ]+", " ");
}
/**
* 填充占位符?
*
* @param sql
* @param parameterObject
* @return
*/
private String replacePlaceholder(String sql, Object parameterObject) {
String result;
if (parameterObject instanceof String) {
result = "'" + parameterObject.toString() + "'";
} else if (parameterObject instanceof Date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
result = "'" + simpleDateFormat.format(parameterObject) + "'";
} else {
result = parameterObject.toString();
}
return sql.replaceFirst("\\?", result);
}
/**
* 格式化sql日志
*
* @param sqlCommandType
* @param sqlId
* @param sql
* @param costTime
* @return
*/
private void formatSqlLog(SqlCommandType sqlCommandType, String sqlId, String sql, long costTime, Object obj) {
String sqlLog = "SqlMonitorPlugin ===> [" + sqlId + "]\n【Excute SQL】===> " + sql + "\n\n " + "【Spend Time】 ===> " + costTime + " ms\n\n";
if (sqlCommandType == SqlCommandType.UPDATE || sqlCommandType == SqlCommandType.INSERT
|| sqlCommandType == SqlCommandType.DELETE) {
if (obj!=null){
sqlLog += ", Affect Count ===> " + obj;
}
}
LogTypeEnum.FILE_SQL.warn(sqlLog);
}
}