使用 PlantUML 绘制的 UML——代码画时序图

前言

在看源码的时候,看见有些大佬画的各种图,能更加方便可视的理解源码逻辑。就想着学习自己画,但是在找了一些画UML图的工具,但是不仅难画,而且难看(仅仅表示个人想法,也许正真的大佬用到也很6,我比较菜)。偶然看到了相关技术的帖子,就跟着学习学习,并在此基础上找到了使用指南将功能得以增强,收益匪浅,终于实现了看源码画时序图的自由。

插件安装

此插件 PlantUML Integration 是基于idea使用的,在idea插件市场很方便搜索到后安装即可

java代码转 uml时序图 代码生成uml时序图_intellij-idea

简单使用

以时序图为例:

  1. 首先创建Sequence文件


    创建成功之后会自动生成一个例图
  2. 在原帖上稍微修改
@startuml
title 时序图案例
'用户
actor User as user

'普通主要成员
participant "gateway" as gateway
participant "user-core" as userCore
database "MySQL" as mysql
database "Redis" as redis

autonumber
user-> gateway:login()
activate gateway
gateway-> userCore:forward the login request
activate userCore
userCore-> userCore :check the login param
activate userCore
userCore-> mysql:query user info from mysql by username
activate userCore
activate mysql
mysql-> userCore:response with username and password
deactivate mysql
userCore->userCore:compare the requested password with the DB's password
userCore-> userCore: generate an unique token
userCore--> redis: save the token to redis

userCore-> gateway: response with the token
deactivate userCore
gateway-> user: login success with the token
deactivate gateway
@enduml

可生成如下图
在这里插入图片描述

  1. 主要语法说明
title:表示该UML用示例图的标题
 actor:表示人形的参与者
 as:可以理解为别名
 participant:表示普通的主要演员
 database:代表人物形象是数据库
 显示的顺序是如何定义的: 声明的参与者顺序是(默认的)显示顺序
 autonumber:可以给参与者添加顺序
 ->:表示如果你希望参与者是虚线,可以使用-->
 activate和deactivate:表演者的生命线
 skinparam sequenceMessageAlign center: 文本居中显示
 skinparam responseMessageBelowArrow true 响应信息显示在箭头下面
 hide footbox 隐藏页脚
 autonumbe 显示顺序 (声明的参与者顺序是(默认的)显示顺序)增强
 ...

功能增强

改插件还有许多功能,在这里不一一例举了,具体可参照官网用户指南

java代码转 uml时序图 代码生成uml时序图_开发语言_02


当然,只是看时序图,可以安装插件 SequenceDiagram 之后即可查看指定方法的时序(自动生成)关系,本人没有仔细看过,有兴趣可以试试

java代码转 uml时序图 代码生成uml时序图_java代码转 uml时序图_03

参考文档

原始帖子 https://mp.weixin.qq.com/s/_C3LTUxanL-KrzKe0Pkd0A
用户指南下载地址 https://plantuml.com/zh/json