mybatis SQL打印插件

文章留言相册关于
欢迎来到Rain的博客@_@
FOLLOW
YOUR
HEART
文章数 : 57
日访问量 : 1
总访问量 : 1362
全文检索本站~~~
如何在java中生成pdf
2022-01-26
HttpUtils
2022-01-12
IdWorker工具类
2022-01-12
java生成二维码QRcode方式
2022-01-30
java生成简单二维码
2022-01-29
maven安装jar包到本地仓库
2022-01-30
mybaits-sql打印插件
2022-01-12

mybaits-sql打印插件
mysqltools
//
// 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 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]+", " ");
}

}
发布日期 2022-01-12
8 0 0
留下您宝贵的评论吧@_@~~~~

你可能感兴趣的:(mybatis,mybatis,sql,java)