Navicat 导出数据库脚本
通过代码转换脚本
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* SQL 脚本转换为 drawio ER 脚本
*/
public class SQLToMermaid {
public static void main(String[] args) {
String inputFilePath = "/Users/admin/Desktop/xxx.sql"; // 输入文件路径
String outputFilePath = "/Users/admin/Desktop/xxx.txt"; // 输出文件路径
System.out.println("Reading from: " + inputFilePath);
System.out.println("Writing to: " + outputFilePath);
try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath));
FileWriter fw = new FileWriter(outputFilePath)) {
StringBuilder sqlContent = new StringBuilder();
String line;
// 读取整个文件内容
while ((line = br.readLine()) != null) {
sqlContent.append(line).append("\n");
}
String sql = sqlContent.toString();
// 正则表达式
Pattern tablePattern = Pattern.compile("CREATE TABLE `(\\w+)` \\((.*?)\\) ENGINE.*?COMMENT='(.*?)';",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
// 匹配
Matcher tableMatcher = tablePattern.matcher(sql);
while (tableMatcher.find()) {
String tableName = tableMatcher.group(1);
String tableComment = tableMatcher.group(3);
fw.write(tableName + "(" + tableComment + ")\n");
String columnDefinitions = tableMatcher.group(2);
String[] columns = columnDefinitions.split(",(?=\\s*`)");
for (String column : columns) {
Pattern columnPattern = Pattern.compile(
"`(\\w+)`\\s+\\w+.*?COMMENT\\s+'(.*?)'",
Pattern.DOTALL
);
Matcher columnMatcher = columnPattern.matcher(column);
if (columnMatcher.find()) {
String columnName = columnMatcher.group(1);
String columnComment = columnMatcher.group(2);
fw.write(" " + columnName + ": " + columnComment + "\n");
}
}
fw.write("\n"); // 表之间空一行
}
System.out.println("Output written to " + outputFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
得到的脚本:
custom(自定义表)
id: 主键id
name: 名称
module: 引用类型1:报事类型模板2:报事详情
ref_id: 关联表id
type:
is_required: 是否必填
subsidiary_rule_str: 附属规则json串
is_del: 是否删除
sort_code: 排序编码
create_time: 创建时间
creator_uid: 创建人id
modified_time: 修改时间
modified_uid: 修改人id
identity(模板通知规则关联执行人)
id: 主键
ref_id: type区分关联键0工单类型id 1 SLA配置id
type: 关联类型 0工单类型执行1工单类型分派2SLA配置
label_id: 类型标签type = 0: 0抄送;
step: 步骤0非步骤 1,2,3,4,5升级步骤
identity_type: 身份类型 0角色1人员
identity_id: 身份id
脚本导入drawio
得到了表: