当前位置:网站首页>Tools to improve work efficiency: the idea of SQL batch generation tools

Tools to improve work efficiency: the idea of SQL batch generation tools

2022-07-05 23:57:00 haohaounique

Improve work efficiency : take excel Convert to file sql, Batch build sql Execute the script

1. Suitable for such reading excel Data and processing into sql File scenario 【 For simple scenarios, it is easy to recommend using the shortcut keys of the tool itself, such as idea notepad,vscode  sublime The tool has its own column selection 】

easyexcel Reference resources : read Excel | Easy Excel

2.maven quote

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.1.1</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

Don't talk about the code

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

/**
 * description:
 */
@Data
@Slf4j
public class User {

    @ExcelProperty(value = " user name ")
    private String name;
    @ExcelProperty(value = " Age ")
    private int age;
    @ExcelProperty(value = " Address ")
    private String address;

    public static void main(String[] args) throws Exception {
        List<User> list = EasyExcel.read("C:\\Users\\haoha\\Desktop\\user.xls", User.class, null).doReadAllSync();
        System.out.println(list);
        StringBuilder sb = new StringBuilder();
        for (User user : list) {
            sb.append("insert into user(user_name,age,address)value");
            sb.append("(");
            sb.append("'").append(replaceWord(user.getName())).append("',");
            sb.append("'").append(user.getAge()).append("',");
            sb.append("'").append(replaceWord(user.getAddress())).append("',");
            sb.append(");");
            sb.append("\r\n");
        }
        try (FileWriter writer = new FileWriter(new File("D:\\develop\\xk\\common-module\\src\\main\\java\\com\\bean\\user.sql"));) {
            writer.write(sb.toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static String replaceWord(String word) {
        if (word == null) {
            return word;
        }
        return word.replace("'", "''");
    }
}

effect

Be careful : Some special symbols need to be replaced , It depends on the use scenario

原网站

版权声明
本文为[haohaounique]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207052351031365.html