Files
2026-10-20 21:19:44 +08:00

101 lines
4.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 10.8 实战案例
:::tip 概述
本节以数据库中真实存在的 `goframe_example`(案例演示表)为实操对象,演示生成器全流程:预览 → 生成(幂等)→ 副作用核对。你可以直接执行每条命令。开发新模块时把表名换成自己的表即可。
:::
## 实操对象:goframe_example
```sql
-- 案例演示表(定稿脚本已含,可直接 SELECT 查看结构)
CREATE TABLE `goframe_example` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(100) NOT NULL DEFAULT '' COMMENT '案例名称',
`avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '案例图片',
`type` int NOT NULL DEFAULT 0 COMMENT '案例类型',
`status` int NOT NULL DEFAULT 1 COMMENT '状态:1-正常 2-禁用',
`sort` int NOT NULL DEFAULT 0 COMMENT '排序',
`create_user` varchar(50) NOT NULL DEFAULT '' COMMENT '创建人',
`create_time` datetime NULL DEFAULT (now()) COMMENT '创建时间',
`update_user` varchar(50) NOT NULL DEFAULT '' COMMENT '更新人',
`update_time` datetime NULL DEFAULT (now()) COMMENT '更新时间',
`del_flag` int NOT NULL DEFAULT 0 COMMENT '删除标志:0-正常 1-已删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='案例演示表';
```
规范自检(生成器识别依据):审计列齐备 ✓;status 注释枚举(自动识别 `example_status` 字典)✓;avatar 注释含"图片"(识别为图片列 → FileFields)✓;无 parent_id(用 ui 普通模板)✓。
## 第一步:预览(不落盘)
```sh
go run ./cmd/generator -tables "goframe_example|案例演示表" -preview
```
输出将产出的文件清单与渲染全文——这是核对"筛选字段/枚举识别/命名"的最佳时机。对照第 3 章([3.10 完整代码清单](../guide/code-list))的真实产物,你会看到模板渲染结果与项目内 example 模块实现同构。
## 第二步:生成(体验幂等保护)
```sh
go run ./cmd/generator -tables "goframe_example|案例演示表"
```
因为 example 模块已完整实现,本次执行将演示生成器的**幂等保护**:
```text
文件产出(overwrite=false 时):
api/v1/example.go · controller · logic · service · dao 四件 · 前端文件
——全部 Skipped(已存在,保护你的手工修改)
副作用(幂等):
Menu: "已存在"(按 path+type=0 查重命中)
Dict: example_status "已存在"(按 code 查重命中)
Route: "路由已存在"(router.go 已含 Bind 行)
```
如需体验全新写入,可临时把 `generator.overwrite` 改为 `true` 覆盖重生成(**先 git 提交保护手工改动**),观察 Overwritten 清单后用 git diff 审查合并。
## 第三步:理解"新模块"流程(换表名即可)
```sh
# 真实新模块:建好表后,把表名换成你的表
go run ./cmd/generator -tables "goframe_xxx|中文名"
# 多表示例(均为库中真实存在的表)
go run ./cmd/generator -tables "goframe_example|案例演示表,goframe_file_template|文件模板"
```
新模块生成后的完整链路:
```text
1. go build ./... && 重启(init 注册/路由生效)
2. 角色管理 → 勾选新模块权限节点(7 固定 + status/import/export)
3. 访问新菜单冒烟:增删改查/导入导出
4. 租户级新表 → internal/model/hooks 的 tenantTables 登记(再重启)
5. 定制收尾:
querySchemas options 改字典驱动(getDictItemList)
columns 金额/复杂列 render
spec 钩子扩展业务(BeforeAdd 默认值等)
```
## 建表侧配合(新模块第一杠杆)
```sql
-- 注释写好,生成器才能"全自动":
`status` int DEFAULT 1 COMMENT '状态:1-正常 2-禁用', -- → 字典 + Select
`cover` varchar(255) DEFAULT '' COMMENT '封面图片', -- → FileFields 图片列
`content` text COMMENT '内容富文本', -- → ContentFields 富文本
UNIQUE KEY uk_code (code) -- → 唯一性语义清晰
```
## 经验总结
```text
* 用库中真实表练手(如 goframe_example)——每条命令可直接执行验证
* 注释写得好 → 字典/标签/前端选项全自动(枚举注释是最大杠杆)
* 无 parent_id 用 ui 模板;树形表(有 parent_id/pid)自动切 ui2
* 生成器产出即可运行——先跑通再定制,不要反过来
* 重生成前 git commit(手工改动与生成产物分离管理)
* 租户级表务必登记 tenantTables(生成器管不到这一步)
```