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

最终结果如图

相关推荐
高溪流9 分钟前
3.数据库表的基本操作
数据库·mysql
alonewolf_9917 分钟前
深入剖析MySQL锁机制与MVCC原理:高并发场景下的数据库核心优化
数据库·mysql
栈与堆37 分钟前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
一路向北·重庆分伦39 分钟前
03-01:MQ常见问题梳理
java·开发语言
一 乐41 分钟前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
lhrimperial1 小时前
企业智能知识库助手落地实践:从RAG到Multi-Agent
java·spring cloud·微服务·系统架构·知识图谱
Codeking__1 小时前
Redis初识——什么是Redis
数据库·redis·mybatis
3***68841 小时前
Spring Boot中使用Server-Sent Events (SSE) 实现实时数据推送教程
java·spring boot·后端
k***1951 小时前
Spring 核心技术解析【纯干货版】- Ⅶ:Spring 切面编程模块 Spring-Instrument 模块精讲
前端·数据库·spring
C***u1761 小时前
Spring Boot问题总结
java·spring boot·后端