1. 程式人生 > 其它 >mybaits-sql列印外掛

mybaits-sql列印外掛

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.mybatisplugin.plugin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
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.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Intercepts({@Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
), @Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
type = Executor.class,
method = "update",
args = {MappedStatement.class, Object.class}
)})
public class SqlStd implements Interceptor {
private static final Logger log = LoggerFactory.getLogger(SqlStd.class);

public SqlStd() {
}

public Object intercept(Invocation invocation) throws Throwable {
Long start = System.currentTimeMillis();
Object paramter = invocation.getArgs()[1];
MappedStatement mappedStatement = (MappedStatement)invocation.getArgs()[0];
Configuration configuration = mappedStatement.getConfiguration();
BoundSql boundSql = mappedStatement.getBoundSql(paramter);
boolean var14 = false;

Object proceed;
try {
var14 = true;
proceed = invocation.proceed();
var14 = false;
} finally {
if (var14) {
Long end = System.currentTimeMillis();
String replace = this.getSql(boundSql, configuration);
log.info("sql語句:\n");
log.info(" " + replace);
log.info("執行時間:" + (end - start) + "ms");
}
}

Long end = System.currentTimeMillis();
String replace = this.getSql(boundSql, configuration);
log.info("sql語句:\n");
log.info(" " + replace);
log.info("執行時間:" + (end - start) + "ms");
return proceed;
}

private String getSql(BoundSql boundSql, Configuration configuration) {
String boundSqlSql = boundSql.getSql();
String replace = "";
if (boundSqlSql != null && boundSqlSql.length() != 0) {
String beautifySql = this.beautifySql(boundSqlSql);
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterObject != null && !"".equals(parameterObject)) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
beautifySql = this.replace(beautifySql, parameterObject);
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
Iterator var10 = parameterMappings.iterator();

while(var10.hasNext()) {
ParameterMapping parameterMapping = (ParameterMapping)var10.next();
String propertyNmae = parameterMapping.getProperty();
Object value;
if (metaObject.hasGetter(propertyNmae)) {
value = metaObject.getValue(propertyNmae);
beautifySql = this.replace(beautifySql, value);
} else if (boundSql.hasAdditionalParameter(propertyNmae)) {
value = boundSql.getAdditionalParameter(propertyNmae);
beautifySql = this.replace(beautifySql, value);
}
}
}
}

return beautifySql;
} else {
return "";
}
}

private String replace(String beautifySql, Object parameterObject) {
String result = null;
if (String.class == parameterObject.getClass()) {
result = "'" + parameterObject.toString() + "'";
} else if (parameterObject.getClass() == Date.class) {
result = "'" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(parameterObject) + "'";
} else {
result = parameterObject.toString();
}

return beautifySql.replaceFirst("\\?", result);
}

private String beautifySql(String boundSqlSql) {
return boundSqlSql.replaceAll("[\\s\n]+", " ");
}
}