Files
easygoadmin-wiki/EasyGoAdmin_Gin_AntdVue/zh/generator/code-engine.md
T
2026-10-25 17:24:27 +08:00

52 lines
1.7 KiB
Markdown
Raw 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.4 表结构解析与渲染引擎
:::tip 概述
生成引擎两段:**内省**(表结构 → 生成模型)与**渲染**(生成模型 + 模板 → 产物)。理解模型字段如何映射到代码,是定制模板与排障的基础。
:::
## 内省
读取目标库的表元数据:
```text
表级:表名、注释、引擎、字符集、自增、记录数
列级:列名、类型、可空、默认值、注释、自增、主键
索引:普通/唯一索引(uk_ 影响 UniqueFields 推断)
```
内省只读——CLI 与 Web 都不写库表结构。
## 生成模型的推断规则
| 表特征 | 推断产物 |
|---|---|
| 列注释含枚举取值(`1-在用 2-停用`) | SerializeMaps 字典声明 + 字典初始化 |
| uk_ 唯一索引 | UniqueFields 唯一性规则 |
| 含 tenant_id 列 | 租户级模块写法 |
| parent_id 自关联列 | 树形模板(ui2 前端 + IsTree 声明) |
| 文件类列(语义注释) | FileFields 声明 |
| del_flag/create_* 等公共列 | 不进业务字段(归钩子) |
**列注释质量直接决定推断质量**——这就是建表规约强调 COMMENT 的原因。
## 渲染
```text
模板(templates/*.tpl,Go text/template 语法)
× 生成模型(表/列/索引 + 推断结果)
= 产物文本(dto/repository/service/handler/api.ts/index.vue/...)
```
模板变量集中在模型上,模板内不做业务推断——推断逻辑在引擎,模板只负责排版。
## 幂等落盘
- overwrite=false:目标存在即跳过(保护手写)
- overwrite=true:全部覆盖
- router.go 声明块:按模块标记定位,幂等追加(重复生成不重复插入)
## 相关
- [10.5 模板体系](/zh/generator/templates.html)
- [10.7 实战案例](/zh/generator/demo.html)