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<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]+", " ");
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值