一个实用技巧,告别手动画图,自动生成数据库 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图

最终结果如图

相关推荐
wyt5314298 分钟前
Redis的安装教程(Windows+Linux)【超详细】
linux·数据库·redis
CeshirenTester21 分钟前
从数据库到结构化用例:一套可落地的测试智能体架构
数据库·架构
2301_7938046942 分钟前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
麦麦鸡腿堡42 分钟前
JavaWeb_请求参数,设置响应数据,分层解耦
java·开发语言·前端
不想看见4042 小时前
Qt 项目中实现良好封装(模块化设计)的详细流程指南
数据库·系统架构
mygljx2 小时前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
没有bug.的程序员2 小时前
Serverless 弹性扩容引发的全线熔断:Spring Boot 启动耗时从 1s 压缩至 0.3s 的物理级绞杀
java·spring boot·kubernetes·serverless·扩容·线上
Jeremy爱编码2 小时前
软考数据库
数据库
bearpping2 小时前
java进阶知识点
java·开发语言
独自破碎E2 小时前
【面试真题拆解】你知道ThreadLocal是什么吗
java·jvm·面试