java springboot 中mybatis sql日志分析

由于某某的不小心,把生产库给清了,数据没法恢复,情急之下分析日志填充数据,结果本人写了一个日志分析程序,代码如下:

package com.zby.exercise;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @author :zoboy
 * @Description:
 * @ Date: Created in 2018-08-21 10:15
 */
public class Demo {

    public static void main(String[] args) {
//        String pattern = "insert into";
//        String table="t_order_info";
        String fileName = "D://console23-1842_1427.txt";
//        String fileName = "D://console9096.txt";
        String pattern = "UPDATE";
        String table = "t_order_info";
        updateSqlHandle(fileName, pattern, table);
    }

    public static void updateSqlHandle(String file, String pattern, String table) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            while ((str = in.readLine()) != null) {
                if (Pattern.matches(".*UPDATE t_ticket_info.*", str)) {
                    String sql = str.substring(str.indexOf(pattern), str.length());
//                    System.out.println(sql);
                    String pram = in.readLine();
                    String params = pram.substring(pram.indexOf("Parameters:") + 11, pram.length());
//                    System.out.println(params);
                    updateParamHandle(sql, params);
                }
            }
        } catch (IOException e) {
        }
    }

    public static void updateParamHandle(String sql, String pram) {


        String[] prams = pram.split(",");
        String[] pp = new String[prams.length];
        int index = 0;
        for (String str1 : prams) {
            if (str1==null || str1.equals(" null")) {
                pp[index] = str1;
            } else if (Pattern.matches(".*BigDecimal.*", str1) || Pattern.matches(".*Integer.*", str1)) {
                str1 = str1.substring(0, str1.indexOf("(")) + ",";
            } else {
                str1 = "'" + str1.substring(1, str1.indexOf("(")) + "'" + ",";
            }
            pp[index] = str1;
            index++;
        }
        String[] asql = sql.split("\\?");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < asql.length - 1; i++) {
            asql[i] += pp[i];
            stringBuilder.append(asql[i].substring(0, asql[i].length() - 1));
        }
        System.out.println(stringBuilder.toString() + ";");
    }


    public static void insertSqlHandle(String file, String pattern, String table) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            while ((str = in.readLine()) != null) {
                if (Pattern.matches(".*" + pattern + " " + table + ".*", str)) {
                    String pram = in.readLine();
                    String params = pram.substring(pram.indexOf("Parameters:") + 11, pram.length());
                    paramHandle(str.substring(str.indexOf(pattern), str.length()), params);
                }
            }
        } catch (IOException e) {
        }
    }

    public static void paramHandle(String sql, String pram) {
        String sqlResult = sql.substring(0, sql.indexOf("( ?")) + " (";
        String[] prams = pram.split(",");
        StringBuilder stringBuilder = new StringBuilder();
        for (String str1 : prams) {
            if (Pattern.matches(".*BigDecimal.*", str1) || Pattern.matches(".*Integer.*", str1)) {
                str1 = str1.substring(0, str1.indexOf("(")) + ",";
            } else {
                str1 = "'" + str1.substring(1, str1.indexOf("(")) + "'" + ",";
            }
            stringBuilder.append(str1);
        }
        sqlResult += stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1) + ");";
        System.out.println(sqlResult);
    }

}

主要分析insert和update,delete的没有写,不过也很简单。执行之后直接生产sql代码,复制到数据库一执行数据就回来了:

效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值