1. 背景
要画ER图,一个个打费时费力,StarUML文件打开是json。那么就有可能自动生成。
2. 效果
把表结构生成好,自己只要维护关系即可。
3. 代码
import lombok.Data;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author liwei
* @version 1.0
* @className MySqlToStarUML
* @date 2022/9/21 22:47
*/
public class MySqlToStarUML {
/**
* 自动生成代码入口
*
* @author liwei
* @date 2022-09-25 00:58:45
* @param args
* @return void
*/
public static void main(String[] args) {
localTest();
}
public static void localTest() {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test_nacos?useUnicode=true&characterEncoding=UTF-8";
String userName = "root";
String password = "密码";
String filePath = "D:\test_nacos.mdj";
List tableList = getTableList(driver, url, userName, password);
saveAsFileWriter(filePath, getProject(url, tableList));
System.out.println("===============生成成功================");
}
/**
* 获取项目字符串
*
* @author liwei
* @date 2023-03-16 18:37:01
* @param url
* 地址
* @param tableList
* 表集合
* @return {@link String}
*/
public static String getProject(String url, List tableList) {
String database = getDBByUrl(url);
Project project = new Project();
// 不能使用中文
project.setName(database);
project.set_id("AAAAAA_Project");
List erddatamodels = new ArrayList();
List ownedElements = new ArrayList();
ERDDataModel erdDataModel = new ERDDataModel();
erdDataModel.setName("Data Model1");
erdDataModel.set_id("AAAAAA_DataModel1");
erdDataModel.set_parent(project.get_id());
erddatamodels.add(erdDataModel);
ERDDiagram erdDiagram = new ERDDiagram();
erdDiagram.setName("ERDDiagram1");
erdDiagram.set_id("AAAAAA_ERDDiagram1");
erdDiagram.set_parent(erdDataModel.get_id());
ownedElements.add(erdDiagram);
for (Table table : tableList) {
table.set_parent(erdDataModel.get_id());
}
ownedElements.addAll(tableList);
erdDataModel.setOwnedElements(ownedElements);
project.setOwnedElements(erddatamodels);
return project.toString();
}
/**
* 通过url获取数据库
*
* @author liwei
* @date 2022-09-23 09:21:09
* @param url
* 地址
* @return {@link String}
*/
public static String getDBByUrl(String url) {
if (null == url || url.isEmpty()) {
throw new RuntimeException("地址为空");
}
if (url.indexOf(":") == 0 && url.length() 0) {
url = url.substring(url.indexOf(":") + 1);
}
if (url.indexOf("?") > 0) {
url = url.substring(0, url.indexOf("?"));
}
if (url.indexOf("/") > 0) {
url = url.substring(url.indexOf("/") + 1);
}
return url;
}
/**
* 保存内容到文件
*
* @author liwei
* @date 2022-11-22 14:19:47
* @param filePath
* 文件路径
* @param content
* 内容
* @return void
*/
private static void saveAsFileWriter(String filePath, String content) {
FileWriter fwriter = null;
try {
fwriter = new FileWriter(filePath);
fwriter.write(content);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (null != fwriter) {
fwriter.flush();
fwriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* 获取表集合
*
* @author liwei
* @date 2022-11-22 14:20:24
* @param driver
* 驱动
* @param url
* 连接
* @param userName
* 账号
* @param password
* 密码
* @return {@link List}
*/
private static List getTableList(String driver, String url, String userName, String password) {
Connection connection;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
throw new RuntimeException("加载驱动失败,找不到:" + driver);
} catch (SQLException e) {
throw new RuntimeException("获取数据库连接失败,请检查配置和日志", e);
}
String database = getDBByUrl(url);
String sqlTable = String.format("select * from information_schema.tables where TABLE_SCHEMA='%s'", database);
List
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net