当前位置:网站首页>提升工作效率工具: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("'", "''");
}
}
效果
注意:需要将部分特殊的符号替换,具体看使用的场景
边栏推荐
猜你喜欢
The use of El cascader and the solution of error reporting
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
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
Breadth first search open turntable lock
el-cascader的使用以及报错解决
Biased sample variance, unbiased sample variance
Online yaml to CSV tool
Rsync remote synchronization
Spire Office 7.5.4 for NET
Senparc. Weixin. Sample. MP source code analysis
随机推荐
【SQL】各主流数据库sql拓展语言(T-SQL 、 PL/SQL、PL/PGSQL)
Use CAS instead of synchronized
Spire.PDF for NET 8.7.2
The difference of time zone and the time library of go language
[Luogu p3295] mengmengda (parallel search) (double)
俄外交部:日韩参加北约峰会影响亚洲安全稳定
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
【QT】Qt使用QJson生成json文件并保存
Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste
Do you regret becoming a programmer?
Zhuan: in the future, such an organization can withstand the risks
15 MySQL-存储过程与函数
时区的区别及go语言的time库
CloudCompare&PCL 点云随机添加噪声
[SQL] SQL expansion languages of mainstream databases (T-SQL, pl/sql, pl/pgsql)
云呐|公司固定资产管理系统有哪些?
How to get all the values stored in localstorage
QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
Spire. PDF for NET 8.7.2