当前位置:网站首页>提升工作效率工具:SQL批量生成工具思想
提升工作效率工具:SQL批量生成工具思想
2022-07-05 23:51:00 【haohaounique】
提升工作效率:将excel文件数据转为sql,批量生成sql执行脚本
1.适用于此类读取excel数据并加工成sql文件的场景【简单场景易推荐使用工具本身快捷键如idea notepad,vscode sublime工具自带列选】
easyexcel参考:读Excel | Easy Excel
2.maven 引用
<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>话不多说看代码
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 = "用户名")
private String name;
@ExcelProperty(value = "年龄")
private int age;
@ExcelProperty(value = "地址")
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("'", "''");
}
}
效果

注意:需要将部分特殊的符号替换,具体看使用的场景
边栏推荐
- 15 MySQL stored procedures and functions
- GFS distributed file system
- [Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
- 【GYM 102832H】【模板】Combination Lock(二分图博弈)
- 保研笔记四 软件工程与计算卷二(8-12章)
- The use of El cascader and the solution of error reporting
- 21.PWM应用编程
- Use mapper: --- tkmapper
- 关于结构体所占内存大小知识
- rsync远程同步
猜你喜欢

用列錶初始化你的vector&&initializer_list簡介

PV static creation and dynamic creation

Spire Office 7.5.4 for NET

Research notes I software engineering and calculation volume II (Chapter 1-7)

20. Migrate freetype font library

STM32__ 06 - single channel ADC

How to get all the values stored in localstorage

Spire. PDF for NET 8.7.2

C# 反射与Type

Do you regret becoming a programmer?
随机推荐
15 MySQL stored procedures and functions
Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
[EF core] mapping relationship between EF core and C data type
mysql-全局锁和表锁
【luogu P3295】萌萌哒(并查集)(倍增)
STM32__06—单通道ADC
Qt QPushButton详解
Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
有什么不起眼却挣钱的副业?
el-cascader的使用以及报错解决
保研笔记一 软件工程与计算卷二(1-7章)
[Luogu p3295] mengmengda (parallel search) (double)
Breadth first search open turntable lock
同事悄悄告诉我,飞书通知还能这样玩
Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
How to improve eloquence
21.PWM应用编程
7.5 装饰器
Doppler effect (Doppler shift)
微信小程序---WXML 模板语法(附带笔记文档)