时序图:Note 用法全解

时序图:Note 用法全解

puml.online

简介 / Introduction

时序图(Sequence Diagram)用于描述对象之间的消息交互,Note(备注)是其重要辅助元素,用来在参与者之间添加注释说明。PlantUML 和 Mermaid 都支持在时序图中使用 Note,且语法各有特色。本文通过同一场景演示两种引擎的 Note 用法,并做对比。

PlantUML 版

1
2
3
4
5
6
7
8
9
10
11
12
13
@startuml
participant Alice as A
participant Bob as B
participant Carol as C

Note over A: 用户登录
Note left of A: 发起认证请求
A -> B: POST /login\n{username, password}
Note right of B: 验证凭证
B --> A: 200 OK\n{token}
Note over A,B: 登录成功
Note over C: Carol 旁观\n不参与此流程
@enduml

Mermaid 版

1
2
3
4
5
6
7
8
9
10
11
12
sequenceDiagram
participant A as Alice
participant B as Bob
participant C as Carol

Note over A: 用户登录
Note left of A: 发起认证请求
A->>+B: POST /login<br/>{username, password}
Note right of B: 验证凭证
B-->>-A: 200 OK<br/>{token}
Note over A,B: 登录成功
Note over C: Carol 旁观<br/>不参与此流程

对比 / Side-by-side

特性 PlantUML Mermaid
左侧 Note Note left of <participant> Note left of <participant>
右侧 Note Note right of <participant> Note right of <participant>
跨越多参与者 Note over <p1,p2> Note over <p1,p2>
跨越范围写法 Note over <p1> 仅单方 Note over <p1> 仅单方
多行文本 直接换行,\n 可选 <br/> 换行

小结 / Wrap-up

PlantUML 的 Note 语法更简洁,Note left/right ofNote over 分工明确,适合复杂场景。Mermaid 的 Note 语法与消息语法一致,学习成本低,但需要用 <br/> 显式换行。两者均支持跨越多个参与者的 Note,用于描述跨对象共享的状态或上下文。

  • 标题: 时序图:Note 用法全解
  • 作者: puml.online
  • 创建于 : 2026-08-29 01:00:44
  • 更新于 : 2026-08-29 01:06:40
  • 链接: https://puml.online/blog/sequence-diagram-notes/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。