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

推荐订阅源

S
Secure Thoughts
P
Privacy International News Feed
T
Tenable Blog
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
月光博客
月光博客
T
The Blog of Author Tim Ferriss
Scott Helme
Scott Helme
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
C
Cisco Blogs
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
Security Latest
Security Latest
Blog — PlanetScale
Blog — PlanetScale
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
博客园 - 三生石上(FineUI控件)
I
InfoQ
Spread Privacy
Spread Privacy
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
A
About on SuperTechFans
博客园_首页
Engineering at Meta
Engineering at Meta
Project Zero
Project Zero
Latest news
Latest news

博客园 - 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 分开是合理的。