惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
WordPress大学
WordPress大学
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
L
LangChain Blog
雷峰网
雷峰网
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
量子位
S
Security Affairs
T
Threat Research - Cisco Blogs
博客园_首页
云风的 BLOG
云风的 BLOG
D
Docker
AWS News Blog
AWS News Blog
腾讯CDC
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
U
Unit 42
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
I
InfoQ

博客园 - mabiao008

idea更新代码时Merge和Rebase区别 CC skill Mac下载免费软件遇到问题解决 错误码code是int类型好还是String类型好 idea2025版本破解 pgsql切换当前会话的模式 java程序中增加mongodb的联合索引 文档启动脚本报错:-bash: ./app.sh: /bin/bash^M: bad interpreter: No such file or directory Milvus索引 pandoc使用 idea把unicode转为中文 idea 插件分享 java项目处理OFD文件 java项目无法读取resources目录下的文件 三种数据对象的区别 Linux环境aspose插件word转pdf中文乱码解决方案 java List报错Method threw ‘java.lang.UnsupportedOperationException‘ exception. 解决 java字段值为null,转json后不存在该字段对应的key 格式化json大文件 springboot项目启动报错Command line is too long. Shorten the command line via JAR manifest or via a classpath file and rerun. com.alibaba.excel.exception.ExcelGenerateException: Can not close IO linux的TCP端口问题 关于linux端口号 linux下生成pdf文件名遇到问题 把页面查询到的数据导出PDF文件(html中的在线表单下载为pdf文件)
CQRS
mabiao008 · 2026-05-15 · via 博客园 - mabiao008

CQRS(Command Query Responsibility Segregation)是命令与查询职责分离的模式。

核心思想:把数据的写操作(Command)和读操作(Query)分开处理。

传统方式 vs CQRS
传统:同一个 Service/Repository 既读又写
┌────────────────┐
│ BudgetService │
│ ├─ getById() │ ← 读
│ ├─ save() │ ← 写
│ ├─ update() │ ← 写
│ └─ delete() │ ← 写
│ └─ list() │ ← 读
└────────────────────┘

CQRS:读写分离
┌────────────────┐ ┌────────┐
│ Query 侧 │ Command 侧 │
│ ├─ getById() │ │ ├─ save() │
│ ├─ list() │ │ ├─ update() │
│ ├─ search() │ │ ├─ delete() │
│ └─ 绑定查询模型 │ │ └─ 绑定写模型 │
│ (BudgetControlModel)│ │ (BudgetPackageControl)│
└────────────────────┘ └────────────────┘

我们项目中的应用
刚才的重构其实就是 CQRS 思路:

| 层 | 职责 | 模型 | |----|------|

| Repository(查询侧) | 只做数据读取,返回 Entity | BudgetPackageControl |

| Service(命令侧) | 业务编排:校验、ID生成、事务控制 | BudgetControlModel / BudgetControlSaveDTO |

好处:

读侧可以优化查询性能(比如用不同的 SQL、缓存、甚至不同的数据库)
写侧专注业务逻辑和事务一致性
模型解耦:查询返回的 Model 和写入用的 DTO/Entity 可以不一样,各自演化不互相影响

什么时候该用 CQRS?
| 场景 | 建议 | |------|

| 简单 CRUD | 不需要,传统方式够用 |

| 读多写少,读逻辑复杂 | 适合,查询侧可以独立优化 | | 写操作有复杂业务编排 | 适合,命令侧集中管理事务 | | 读写模型差异大 | 适合,避免一个模型既满足读又满足写 |

我们项目当前属于"写操作有复杂业务编排"的场景(copy、batchSave 涉及多表事务),所以用 CQRS 分开是合理的。