核心代码
@Slf4j
@Component
@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})
})
public class SqlInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object returnValue = null;
try {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
String sqlId = mappedStatement.getId();
if(sqlId.contains("History") || sqlId.contains("Tmp")){
return invocation.proceed();
}
Object parameter = null;
if (invocation.getArgs().length > 1) {
parameter = invocation.getArgs()[1];
}
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
Configuration configuration = mappedStatement.getConfiguration();
long start = System.currentTimeMillis();
returnValue = invocation.proceed();
long end = System.currentTimeMillis();
long time = (end - start);
String sql = getSql(configuration, boundSql, sqlId, time);
log.info("SQL 监控: {}", sql);
} catch (Exception e) {
log.error("SQL拦截器异常");
return invocation.proceed();
}
return returnValue;
}
@Override
public Object plugin(Object target) {
log.info("Interceptor::plugin#target={}", target);
if(target instanceof Executor){
return Plugin.wrap(target, this);
}
return target;
}
@Override
public void setProperties(Properties properties) {
}
public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time) {
String sql = showSql(configuration, boundSql);
StringBuilder sb = new StringBuilder(100);
sb.append("[sqlId]: ").append(sqlId);
sb.append(" [SQL耗时: ").append(time).append(" 毫秒]");
sb.append(" [SQL]:").append(sql);
log.debug(sb.toString());
return sb.toString();
}
public static String showSql(Configuration configuration, BoundSql boundSql) {
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
if (parameterMappings.size() > 0 && parameterObject != null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
sql = sql.replaceFirst("\\?", getParameterValue(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 = sql.replaceFirst("\\?", getParameterValue(obj));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
}
}
}
}
return sql;
}
private static String getParameterValue(Object obj) {
String value = null;
if (obj instanceof String) {
value = "'" + obj + "'";
value = value.replaceAll("\\\\", "\\\\\\\\");
value = value.replaceAll("\\$", "\\\\\\$");
} else if (obj instanceof Date) {
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
value = "'" + formatter.format(obj) + "'";
} else {
if (obj != null) {
value = obj.toString();
} else {
value = "";
}
}
return value;
}
}
拦截器
-
plugin方法,每种代理类型都会调用该方法
plugin#target=org.apache.ibatis.executor.CachingExecutor@aadddebplugin#target=org.apache.ibatis.scripting.defaults.DefaultParameterHandler@1bb554b0plugin#target=org.apache.ibatis.executor.resultset.DefaultResultSetHandler@6824a9b0plugin#target=org.apache.ibatis.executor.statement.RoutingStatementHandler@560f1f4b
-
@Intercepts注解指定需要创建代理的类型,会为该类型创建代理,而后会执行interceptor方法@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}) })- type:拦截类型
- method:拦截类型接口中定义的方法
- args:拦截类型接口中定义的方法 的 参数,method + args才能定位一个方法,可能会有多个重载的同名方法