Fundamentals 10 min read

Master PlantUML: Create Sequence, Use Case, Mind Map & Activity Diagrams with Code

This tutorial introduces PlantUML, an open‑source tool for generating UML, mind‑map, and activity diagrams directly from code, covering installation, key syntax, and step‑by‑step examples for sequence, use‑case, mind‑map, and activity diagrams.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Master PlantUML: Create Sequence, Use Case, Mind Map & Activity Diagrams with Code

Preface

Hello, I’m Su San. I recently used code to draw sequence diagrams and UML use‑case diagrams and want to share the experience.

1. PlantUML Overview

PlantUML is an open‑source project that quickly creates UML diagrams from textual descriptions. It supports sequence diagrams, use‑case diagrams, class diagrams, mind maps, ER diagrams, and more.

Below is a login sequence diagram and the corresponding PlantUML source code:

/**
 * 关注公众号:键捡田螺的小男孩
 */
@startuml
title Sequence Diagram of User login
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 request, param: username, password
activate gateway
gateway->userCore: forward the login request
activate userCore
userCore->userCore: check the login param
userCore->mysql: query user info from mysql by username
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

2. Installing PlantUML

Installation is straightforward via the “PlantUML Integration” plugin in your IDE’s marketplace. After installing, create a new PlantUML file, paste the code above, and the diagram renders automatically. For non‑sequence diagrams you also need Graphviz installed.

3. How to Draw a Sequence Diagram

A sequence diagram shows the time‑ordered interaction between objects. In PlantUML you create a new file, select “Sequence”, give it a name, and the tool generates a basic diagram. The same login example code is used, and the following keywords are explained: title: diagram title actor: human participant as: assign an alias to a participant participant: regular participant (different shape from actor) database: participant shaped as a database autonumber: automatically number messages -> and -->: message arrows (solid or dashed) activate / deactivate: show lifeline activation

4. How to Draw a Use‑Case Diagram

A use‑case diagram depicts users (actors) and the system functions (use cases) they interact with. Create a PlantUML file, choose “use case”, and define actors, packages, and use cases. Example code and keyword meanings:

@startuml
left to right direction
actor Guest as g
package Professional {
  actor Chef as c
  actor "Food Critic" as fc
}
package Restaurant {
  usecase "Eat Food" as UC1
  usecase "Pay for Food" as UC2
  usecase "Drink" as UC3
  usecase "Review" as UC4
}
fc --> UC4
g --> UC1
g --> UC2
g --> UC3
@enduml
left to right direction

: layout direction actor Guest as g: define an actor with alias package: group elements usecase "..." as UCx: define a use case -->: relationship between actor and use case

5. How to Draw a Mind Map

A mind map visualizes divergent thinking. PlantUML supports it with the @startmindmap block. Example:

@startmindmap
* 公众号:捡田螺的小男孩,干货面试题
** 计算机网络面试题
*** TCP/IP十五连问
*** 两万字计算机面试题汇总
** MySQL面试题
** Redis面试题
** 大厂面试真题
*** 虾皮十五连问
*** 五年Oppo后端面试真题
*** 腾讯云十五连问
@endmindmap

6. How to Draw an Activity Diagram

An activity diagram describes the workflow of a business use case. The following login activity diagram code demonstrates the syntax:

@startuml
title Activity Diagram of User login

start
: user request login;
if (is request param null?) then (N)
  : query user info by username;
  if (is user info null?) then (N)
    : compare the password;
    if (Is password right?) then (Y)
      : generate a token, set it to redis;
      : response with login success;
    else (N)
      : response with wrong password code;
      stop
    endif
  else (Y)
    : response with error userinfo;
    stop
  endif
else (Y)
  : response with error param;
  stop
endif
stop
@enduml
start

/ stop: begin and end of the flow :: activity node if … then … else … endif: conditional branching

PlantUML provides a lightweight, code‑centric way to produce many kinds of diagrams directly from the IDE.

PlantUMLSequence DiagramUMLUse Case DiagramMind Mapactivity diagram
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.