Java替换mybatis框架SQL日志中的预编译问号“?“

当我们去排查问题的时候,经常需要查看SQL来判断执行逻辑,但是打开日志后,往往会发现是这样的:

2022-03-07 11:45:25,959 [http-nio2-8080-exec-10] DEBUG [16466247258377172025468] com.test.crm.product.mapper.ProductMapper.pageList:137 - ==>  Preparing: SELECT p.id, p.product_name, p.product_code, p.shelf_status, p.config, p.has_u8, p.sale_scope, p.gmt_modified, p.price_group FROM basic_product AS p WHERE p.deleted = 0 AND p.product_category_id IN ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) AND (p.product_code LIKE CONCAT('%', ?, '%') OR p.product_name LIKE CONCAT('%', ?, '%')) ORDER BY p.product_code LIMIT ?
2022-03-07 11:45:25,961 [http-nio2-8080-exec-10] DEBUG [16466247258377172025468] com.test.crm.product.mapper.ProductMapper.pageList:137 - ==> Parameters: 0(Long), 18865823302549504(Long), 18865823308840960(Long), 18865823315132416(Long), 18865823321948160(Long), 18865823328239616(Long), 18865823334531072(Long), 18865823340822528(Long), 18865823346589696(Long), 18865823352881152(Long), 18865823358124032(Long), 18865823364415488(Long), 18865823369658368(Long), 18865823375425536(Long), 18865823381192704(Long), 存储(String), 存储(String), 10(Long)
2022-03-07 11:45:25,965 [http-nio2-8080-exec-10] DEBUG [16466247258377172025468] com.test.crm.product.mapper.ProductMapper.pageList:137 - <==      Total: 5

因为预编译里面有PreparingParameters,固有很多问号,每次手动去变动问号,比较麻烦,所以写了个程序用于处理:

  • 一个方法填入预编译(带问号)的字符串
  • 一个方法填入参数字符串
  • 运行程序,便会打印替换后的
public class Main {
    public static void main(String[] args) {
        String originSql = getOriSql();
        String paramStr = getParamStr();

        originSql = originSql.trim();
        paramStr = paramStr.trim();
        originSql = originSql.replace("Preparing:", "");
        paramStr = paramStr.replace("Parameters:", "");


        String[] params = paramStr.split(",");
        for (String param : params) {
            param = adjustParam(param);
            originSql = originSql.replaceFirst("\\?", param);
        }
        System.out.println(originSql);

    }

    private static String adjustParam(String param) {
        //去掉第一个空格
        param = param.substring(1);
        int i1 = param.indexOf("(");
        int i2 = param.indexOf(")");
        String subStr = param.substring(i1, i2 + 1);
        //移除类型
        param = param.replace(subStr, "");
        switch (subStr) {
            case "(Long)":
            case "(Integer)":
            case "(Boolean)":
            case "(BigDecimal)":
                return param;

            case "(String)":
            case "(Timestamp)":
                return "'" + param + "'";
            default:
        }
        throw new RuntimeException("Not Match Param Type");
    }

    //参数填充
    private static String getParamStr() {
        return "Parameters: 0(Long), 18865823302549504(Long), 18865823308840960(Long), 18865823315132416(Long), 18865823321948160(Long), 18865823328239616(Long), 18865823334531072(Long), 18865823340822528(Long), 18865823346589696(Long), 18865823352881152(Long), 18865823358124032(Long), 18865823364415488(Long), 18865823369658368(Long), 18865823375425536(Long), 18865823381192704(Long), 存储(String), 存储(String), 10(Long)";
    }

    //原始预编译SQL
    private static String getOriSql() {
        return "Preparing: SELECT p.id, p.product_name, p.product_code, p.shelf_status, p.config, p.has_u8, p.sale_scope, p.gmt_modified, p.price_group FROM basic_product AS p WHERE p.deleted = 0 AND p.product_category_id IN ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) AND (p.product_code LIKE CONCAT('%', ?, '%') OR p.product_name LIKE CONCAT('%', ?, '%')) ORDER BY p.product_code LIMIT ?";
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值