一个实用技巧,告别手动画图,自动生成数据库 ER 图

背景

我们在做数据库设计的时侯一般需要画ER图方便数据库分析。 有很多ER设计工具,一般都是物理外键关系生成。

但是真实生产环境,我们很少会建立这种真实物理外键关系,仅仅只会在逻辑上生成一个虚拟的外键的关系。

具体原因可以参考阿里巴巴『Java 开发手册

IDEA 针对上边的问题,在IDEA 2019.3.2 新增了虚拟外键的功能,在 ER 图增加生成虚拟外键的功能。

不过如果你的表中外键全名规则不符合规范,IDEA 是不会生成外键的。需要在 Setting|Editor|Code Competion设置。

上面功能比较适合表结构比较规范的表,但是真实场景可能比较复杂,外键命名也不是规范。为了生成这个外键关系,还要配置正则规则,好难啊,靠,放弃!!!!

有没有傻子也会操作的办法呀?

我们发现一个傻子都会的好办法,亲测可用!还是IDEA哟

首先我们需要打开一个 IDEA Database console 窗口,在里面输入连表 sql,然后使用 Alt+Enter 快捷键选择 Store table relation

在工程根目录下会生成如下配置文件

sql 复制代码
select * from comment  
join reply_comment rc  
on  
comment.id = rc.root_reply_comment_id;
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>  
<data>  
<object-set>  
  
<table path="sparrow.reply_comment">  //select from comment
    <table path="sparrow.reply_comment" //join reply_comment rc
           from="id" to="reply_comment_id"/>  //on comment.id =rc.root_reply_comment_id;
</table>  
</object-set>  
</data>

如果再添加其他虚拟外键,按以上规则生成即可!

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>  
<data>  
<object-set>  
<table path="sparrow.user_behavior">  
<table path="sparrow.user" from="user_id" to="user_id"/>  
</table>  
<table path="sparrow.comment">  
<table path="sparrow.user" from="create_user_id" to="user_id"/>  
</table>  
<table path="sparrow.comment">  
<table path="sparrow.reply_comment" from="id" to="root_reply_comment_id"/>  
</table>  
  
<table path="sparrow.reply_comment">  
<table path="sparrow.reply_comment" from="id" to="reply_comment_id"/>  
</table>  
<table path="sparrow.reply_comment">  
<table path="sparrow.user" from="create_user_id" to="user_id"/>  
</table>  
<table path="sparrow.article">  
<table path="sparrow.tag" from="tags" to="id"/>  
</table>  
</object-set>  
</data>

最后选择Show Diagram 生成ER图

最终结果如图

相关推荐
卷心菜好6啊6 分钟前
特辣的海藻!2
java
心态与习惯9 分钟前
mac 下 java 调用 gurobi 不能加载 jar
java·jar·mac·cplex·gurobi
he2581910 分钟前
centOS 7.9 安装JDK MYSQL
java·mysql·centos
找了一圈尾巴22 分钟前
Spring Boot 日志管理(官网文档解读)
java·spring boot
升讯威在线客服系统22 分钟前
如何通过 Docker 在没有域名的情况下快速上线客服系统
java·运维·前端·python·docker·容器·.net
s:1031 小时前
【框架】参考 Spring Security 安全框架设计出,轻量化高可扩展的身份认证与授权架构
java·开发语言
夜泉_ly2 小时前
MySQL -安装与初识
数据库·mysql
qq_529835353 小时前
对计算机中缓存的理解和使用Redis作为缓存
数据库·redis·缓存
南山十一少4 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
月光水岸New6 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu