网站推荐、资源下载等 | 个人网站

MySql生成ER【StarUML】文件

1. 背景

要画ER图,一个个打费时费力,StarUML文件打开是json。那么就有可能自动生成。

2. 效果

把表结构生成好,自己只要维护关系即可。

image

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<Table> 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<Table> tableList) {
        String database = getDBByUrl(url);

        Project project = new Project();
        // 不能使用中文
        project.setName(database);
        project.set_id("AAAAAA_Project");
        List<OwnedElement> erddatamodels = new ArrayList<>();
        List<OwnedElement> 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() <= 1) {
            throw new RuntimeException("地址有误");
        }
        while (url.indexOf(":") > 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< Table>}
     */
    private static List<Table> 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<Map<String, String>> mapList = runSql(connection, sqlTable);

        List<Table> tableList = new ArrayList<>();
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, String> map = mapList.get(i);
            Table table = new Table();
            String tableId = String.valueOf(i + 1);
            table.set_id(tableId);
            table.setName(map.get("TABLE_NAME"));
            table.setDocumentation(map.get("TABLE_COMMENT"));
            String sqlColumn = String.format("select * from information_schema.columns where TABLE_SCHEMA='%s' and TABLE_NAME = '%s'", database, table.getName());
            List<Map<String, String>> mapList2 = runSql(connection, sqlColumn);
            List<Column> columnList = new ArrayList<>();
            for (Map<String, String> stringMap : mapList2) {
                Column column = new Column();
                column.setTableId(tableId);
                column.setName(stringMap.get("COLUMN_NAME"));
                column.setType(stringMap.get("DATA_TYPE"));
                String columnType = stringMap.get("COLUMN_TYPE");
                if (columnType.indexOf("(") > 0) {
                    column.setLength(columnType.substring(columnType.indexOf("(") + 1, columnType.indexOf(")")));
                } else {
                    column.setLength(stringMap.get("CHARACTER_MAXIMUM_LENGTH"));
                }
                column.setOrdinalPosition(Integer.parseInt(stringMap.get("ORDINAL_POSITION")));
                column.setNullable("YES".equals(stringMap.get("IS_NULLABLE")));
                column.setPrimaryKey("PRI".equals(stringMap.get("COLUMN_KEY")));
                column.setUnique("UNI".equals(stringMap.get("COLUMN_KEY")));
                columnList.add(column);
            }
            columnList.sort((c1, c2) -> c1.ordinalPosition - c2.getOrdinalPosition());
            table.setColumns(columnList);
            tableList.add(table);
        }

        close(null, connection, null);
        return tableList;
    }

    /**
     * 关闭连接
     *
     * @author liwei
     * @date 2022-09-23 09:21:53
     * @param pstmt
     *        预编译
     * @param conn
     *        连接
     * @param rs
     *        结果集
     * @return void
     */
    public static void close(PreparedStatement pstmt, Connection conn, ResultSet rs) {
        try {
            if(null != rs) {
                rs.close();
                rs = null;
            }
            if(null != pstmt) {
                pstmt.close();
                pstmt = null;
            }
            if(null != conn) {
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            throw new RuntimeException("关闭数据库连接异常", e);
        }
    }

    /**
     * 运行sql
     *
     * @author liwei
     * @date 2022-11-22 14:21:40
     * @param conn
     *        连接
     * @param sql
     *        执行的sql
     * @return {@link List< Map< String, String>>}
     */
    public static List<Map<String, String>> runSql(Connection conn, String sql) {
        if (null == sql || sql.isEmpty()) {
            throw new RuntimeException("执行的sql不可为空");
        }
        List<Map<String, String>> list = new ArrayList<>();
        if(null == conn) {
            throw new RuntimeException("获取数据库连接失败");
        }

        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Map<String, String> map = new HashMap<>();
                ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    map.put(metaData.getColumnName(i), rs.getString(i));
                }
                list.add(map);
            }
        } catch (SQLException e) {
            throw new RuntimeException("执行sql异常", e);
        } finally {
            close(pstmt, null, rs);
        }
        return list;
    }

    @Data
    static class Table extends OwnedElement {
        private String _type = "ERDEntity";
        private String documentation;
        private List<Column> columns;

        @Override
        public String toString() {
            return "{" +
                    "\"_type\":\"" + _type + '\"' +
                    ", \"_id\":\"" + super._id + '\"' +
                    ", \"_parent\":{\"$ref\":\"" + super._parent + "\"}" +
                    ", \"name\":\"" + super.name + " " + documentation + '\"' +
                    ", \"documentation\":\"" + documentation + '\"' +
                    ", \"columns\":" + columns +
                    '}';
        }
    }

    @Data
    static class Column {
        private String _type = "ERDColumn";
        private String name;
        private String tableId;
        private String type;
        private String length;
        // UNI、PRI
        private String columnKey;
        private int ordinalPosition;
        private Boolean primaryKey;
        private Boolean unique;
        private Boolean nullable;

        @Override
        public String toString() {
            return "{" +
                    "\"_type\":\"ERDColumn\"" +
                    ", \"_parent\":{\"$ref\":\"" + tableId + "\"}" +
                    ", \"name\":\"" + name + '\"' +
                    ", \"type\":\"" + type + '\"' +
                    (null != length ? ", \"length\":\"" + length + '\"' : "") +
                    (primaryKey ? ", \"primaryKey\":\"" + primaryKey + "\"" : "") +
                    (unique ? ", \"unique\":\"" + unique + "\"" : "") +
                    (nullable ? ", \"nullable\":\"" + nullable + "\"" : "") +
                    '}';
        }
    }

    @Data
    static class Project extends OwnedElement {
        private String _type = "Project";

        @Override
        public String toString() {
            return "{" +
                    "\"_type\":\"Project\"" +
                    ", \"_id\":\"" + super._id + '\"' +
                    ", \"name\":\"" + super.name + '\"' +
                    ", \"ownedElements\":" + super.ownedElements +
                    "}";
        }
    }

    @Data
    static class OwnedElement {
        private String _type;
        private String _id;
        private String _parent;
        private String name;
        private List<OwnedElement> ownedElements;
    }

    @Data
    static class ERDDataModel extends OwnedElement {
        private String _type = "ERDDataModel";

        @Override
        public String toString() {
            return "{" +
                    "\"_type\":\"ERDDataModel\"" +
                    ", \"_id\":\"" + super._id + '\"' +
                    ", \"_parent\":{\"$ref\":\"" + super._parent + "\"}" +
                    ", \"name\":\"" + super.name + '\"' +
                    ", \"ownedElements\":" + super.ownedElements +
                    "}";
        }
    }

    @Data
    static class ERDDiagram extends OwnedElement {
        private String _type = "ERDDiagram";

        @Override
        public String toString() {
            return "{" +
                    "\"_type\":\"ERDDiagram\"" +
                    ", \"_id\":\"" + super._id + '\"' +
                    ", \"_parent\":{\"$ref\":\"" + super._parent + "\"}" +
                    ", \"name\":\"" + super.name + '\"' +
                    (null != super.ownedElements ? ", \"ownedViews\":" + super.ownedElements : "") +
                    "}";
        }
    }

    @Data
    static class ERDEntityView extends OwnedElement {
        private String _type = "ERDEntityView";
        private String tableId;

        @Override
        public String toString() {
            return "{" +
                    "\"_type\":\"ERDEntityView\"" +
                    ", \"model\":{\"" + tableId + "\"}" +
                    (null != super.ownedElements ? ", \"subViews\":" + super.ownedElements : "") +
                    "}";
        }
    }
}
posted @ 2023-03-16 20:45  xiaostudy  阅读(608)  评论(0编辑  收藏  举报
网站推荐
[理工最爱]小时百科 |  GitHub |  Gitee |  开源中国社区 |  牛客网 |  不学网论坛 |  r2coding |  冷熊简历 |  爱盘 |  零散坑 |  bootstrap中文网 |  vue.js官网教程 |  源码分享站 |  maven仓库 |  楼教主网站 |  廖雪峰网站 |  w3cschool |  在线API |  代码在线运行 |  [不学网]代码在线运行 |  JS在线运行 |  PHP中文网 |  深度开源eclipse插件 |  文字在线加密解密 |  菜鸟教程 |  慕课网 |  千图网 |  手册网 |  素材兔 |  盘多多 |  悦书PDF |  sumatra PDF |  calibre PDF |  Snipaste截图 |  shareX截图 |  vlc-media-player播放器 |  MCMusic player |  IDM下载器 |  格式工厂 |  插件网 |  谷歌浏览器插件 |  Crx搜搜 |  懒人在线计算器 |  leetcode算法题库 |  layer官网 |  layui官网 |  formSelects官网 |  Fly社区 |  程序员客栈 |  融云 |  华为云 |  阿里云 |  ztree官网API |  teamviewer官网 |  sonarlint官网 |  editormd |  pcmark10官网 |  crx4chrome官网 |  apipost官网 |  花生壳官网 |  serv-u官网 |  杀毒eset官网 |  分流抢票bypass官网 |  懒猴子CG代码生成器官网 |  IT猿网 |  natapp[内网穿透] |  ngrok[内网穿透] |  深蓝穿透[内网穿透] |  WakeMeOnLan[查看ip] |  iis7 |  [漏洞扫描]Dependency_Check官网 |  [图标UI]fontawesome官网 |  idea插件官网 |  路过图床官网 |  sha256在线解密 |  在线正则表达式测试 |  在线文件扫毒 |  KuangStudy | 
资源下载
电脑相关: Windows原装下载msdn我告诉你 |  U盘制作微PE工具官网下载 |  Linux_CentOS官网下载 |  Linux_Ubuntu官网下载 |  Linux_OpenSUSE官网下载 |  IE浏览器官网下载 |  firefox浏览器官网下载 |  百分浏览器官网下载 |  谷歌google浏览器历史版本下载 |  深度deepin系统官网下载 |  中兴新支点操作系统官网下载 |  文件对比工具Beyond Compare官网下载 |  开机启动程序startup-delayer官网下载 |  openoffice官网下载 |  utorrent官网下载 |  qbittorrent官网下载 |  cpu-z官网下载 |  蜘蛛校色仪displaycal官网下载 |  单文件制作greenone下载 |  win清理工具Advanced SystemCare官网下载 |  解压bandizip官网下载 |  内存检测工具memtest官网下载 |  磁盘坏道检测与修复DiskGenius官网下载 |  磁盘占用可视化SpaceSniffer官网下载 |  [磁盘可视化]WizTree官网下载 |  win快速定位文件Everything官网下载 |  文件定位listary官网下载 |  动图gifcam官网下载 |  7-Zip官网下载 |  磁盘分区工具diskgenius官网下载 |  CEB文件查看工具Apabi Reader官网下载 |  罗技鼠标options官网下载 |  [去除重复文件]doublekiller官网下载 | 
编程相关: ApacheServer官网下载 |  Apache官网下载 |  Git官网下载 |  Git高速下载 |  Jboss官网下载 |  Mysql官网下载 |  Mysql官网历史版本下载 |  NetBeans IDE官网下载 |  Spring官网下载 |  Nginx官网下载 |  Resin官网下载 |  Tomcat官网下载 |  jQuery历史版本下载 |  nosql官网下载 |  mongodb官网下载 |  mongodb_linux历史版本下载 |  mongodb客户端下载 |  VScode官网下载 |  cxf官网下载 |  maven官网下载 |  QT官网下载 |  SVN官网下载 |  SVN历史版本下载 |  nodeJS官网下载 |  oracle官网下载 |  jdk官网下载 |  STS官网下载 |  STS历史版本官网下载 |  vue官网下载 |  virtualbox官网下载 |  docker desktop官网下载 |  github desktop官网下载 |  EditPlus官网下载 |  zTree下载 |  layui官网下载 |  jqgrid官网下载 |  jqueryui官网下载 |  solr历史版本下载 |  solr分词器ik-analyzer-solr历史版本下载 |  zookeeper历史版本官网下载 |  nssm官网下载 |  elasticsearch官网下载 |  elasticsearch历史版本官网下载 |  redis官网下载 |  redis历史版本官网下载 |  redis的win版本下载 |  putty官网下载 |  查看svn密码TSvnPD官网下载 |  MongoDB连接工具Robo官网下载 |  dll查看exescope官网下载 |  dll2c官网下载 |  接口测试apipost官网下载 |  接口测试postman官网下载 |  原型设计工具AxureRP官网下载 |  canal官网下载 |  idea主题样式下载 |  vue的GitHub下载 |  finalShell官网下载 |  ETL工具kafka官网下载 |  cavaj[java反编译]官网下载 |  jd-gui[java反编译]官网下载 |  radmin[远程连接]官网下载 |  tcping[win ping端口]下载 |  jQueryUploadFile官网下载 |  RedisPlus下载 |  aiXcoder智能编程助手官网下载 |  [表单效验]validform官网下载 |  idea官网下载 |  RedisStudio下载 |  MD转word含公式pandoc官网下载 |  logviewer官网下载 |  Kafka官网下载 |  hbase高速下载 |  hadoop官网下载 |  hadooponwindows的GitHub下载 |  hive官网下载 |  soapui官网下载 |  flink官网下载 |  kafkatool官网下载 |  MinIO官网下载 |  MinIO中国镜像下载 | 
办公相关工具
免费在线拆分PDF【不超过30M】 |  免费在线PDF转Word【不超过10M】 |  在线文字识别转换【不超过1M】 |  PDF转换成Word【不超过50M】 |  在线OCR识别 |  Smallpdf |  文件转换器Convertio |  迅捷PDF转换器 |  字母大小写转换工具 |  档铺 |  快传airportal[可文字] |  快传-文叔叔 |  P2P-小鹿快传 |  [图床]ImgURL | 
网站入口
腾讯文档 |  有道云笔记网页版 |  为知笔记网页版 |  印象笔记网页版 |  蓝奏云 |  QQ邮箱 |  MindMaster在线思维导图 |  bilibili |  PDM文件在线打开 |  MPP文件在线打开 |  在线PS软件 |  在线WPS |  阿里云企业邮箱登陆入口 | 
其他
PDF转换 |  悦书PDF转换 |  手机号注册查询 |  Reg007 |  akmsg |  ip8_ip查询 |  ipip_ip查询 |  天体运行testtubegames |  测试帧率 |  在线网速测试 |