签入版本

This commit is contained in:
2026-09-16 10:11:44 +08:00
parent bf08cf1248
commit 20d8a85989
205 changed files with 34363 additions and 0 deletions
@@ -0,0 +1,191 @@
:::tip 本章概要
代码生成器 CLI 模式的使用方法,包括单表生成、批量生成、预览模式和配置导出。
:::
# CLI 命令行使用
CLI 入口为项目根目录的 `generator.py`,内部复用 `src/modules/generator` 下的引擎。适合开发阶段快速生成单个模块。
## 基本用法
```bash
# 解析表结构并生成模块(最常用)
python generator.py tornado_example
```
执行后自动完成:
1. 从数据库解析 `tornado_example` 表结构
2. 生成后端模块代码到 `src/modules/example/`
3. 生成端点代码到 `src/modules/example.py`
4. 生成前端页面到 `ui/src/views/tool/example/` 和 `ui/src/api/tool/example.ts`
5. 自动注册路由到 `src/api/v1/router.py`
6. 自动创建菜单和权限节点
## 预览模式
使用 `--dry-run` 仅解析表结构并打印配置摘要,不执行代码生成:
```bash
python generator.py tornado_example --dry-run
```
输出示例:
```
==============================================================
数据库表结构解析和代码生成工具
==============================================================
正在解析表: tornado_example
表结构解析成功
------------------------------------------------------------
生成配置摘要:
------------------------------------------------------------
应用名称: example
模块名称: 案例
模型类名: Example
字段数量: 8
是否有排序: True
是否有状态: True
- name: 案例名称 (String)
- code: 案例编码 (String)
- type: 案例类型 (Integer)
选项: [(1, '类型1'), (2, '类型2'), (3, '类型3')]
- status: 案例状态 (Integer)
选项: [(1, '正常'), (2, '停用')]
- sort: 排序 (Integer)
... 还有 3 个字段
==============================================================
预览模式 - 不执行代码生成(去掉 --dry-run 参数即可生成)
==============================================================
```
## 导出配置
使用 `--output` 将解析结果导出为 JSON 配置文件:
```bash
# 预览并导出配置
python generator.py tornado_example --dry-run --output config.json
# 生成代码并同时保存配置
python generator.py tornado_example --output config.json
```
导出的 `config.json` 包含完整的生成配置,可手动编辑后重新使用。
## 使用配置文件生成
使用 `--config` 加载已有的 JSON 配置文件,跳过表解析:
```bash
python generator.py --config config.json
```
适用场景:
- 需要微调字段配置后重新生成
- 表结构已不存在但配置文件保留
- 跨项目复用配置
## 完整参数
| 参数 | 简写 | 说明 |
|:----|:----|:----|
| `table_name` | - | 数据库表名(位置参数,可选) |
| `--config` | `-c` | 使用已有的 JSON 配置文件(跳过表解析) |
| `--dry-run` | `-d` | 仅生成配置,不执行代码生成 |
| `--output` | `-o` | 将配置导出为 JSON 文件 |
## 典型工作流
### 快速生成
```bash
python generator.py tornado_example
```
### 预览 → 微调 → 生成
```bash
# 1. 预览配置
python generator.py tornado_example --dry-run --output config.json
# 2. 编辑 config.json(调整字段属性、注释等)
vim config.json
# 3. 用配置文件生成
python generator.py --config config.json
```
### 仅保存配置
```bash
python generator.py tornado_example --output config.json
```
## 环境要求
运行 CLI 前需确保:
1. **数据库可访问**:CLI 复用 `.env` 中的数据库配置,需确保 MySQL 服务正常且连接信息正确
2. **Python 依赖已安装**:`pip install -r requirements.txt`(包含 Jinja2、SQLAlchemy 等)
3. **表已存在**:目标表需已在数据库中创建完成
4. **编码要求**:Windows 环境下确保终端使用 UTF-8 编码(`chcp 65001`)
:::warning 数据库要求
CLI 命令行工具通过查询 MySQL 的 `information_schema` 获取表结构信息,**仅支持 MySQL 数据库**。
:::
## 常见错误
### 表不存在
```
操作失败: 表 tornado_xxx 不存在或无业务字段
```
**原因**:数据库中不存在该表,或表名拼写错误。
**解决**:确认表名正确,且表已在数据库中创建。
### 目标目录已存在
```
操作失败: 模块 example 已存在,请先删除 src/modules/example
```
**原因**:`src/modules/example/` 目录已存在,生成器拒绝覆盖。
**解决**:删除已有目录后重新生成(详见 [6.9 常见问题](/zh/volume5/faq))。
### 数据库连接失败
```
操作失败: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server...")
```
**原因**:`.env` 中的数据库配置错误,或 MySQL 服务未启动。
**解决**:检查 `.env` 中的 `DB_HOST`、`DB_PORT`、`DB_USERNAME`、`DB_PASSWORD` 配置。
### 编码错误
```
SyntaxError: Non-UTF-8 code start with '\xc8' in file ...
```
**原因**:Windows 终端编码不是 UTF-8。
**解决**:执行 `chcp 65001` 切换终端编码。
## 注意事项
1. **表名支持带前缀**:`tornado_example` 和 `example` 都可以,脚本会自动处理 `tornado_` 前缀
2. **目标目录不能已存在**:如果 `src/modules/example/` 已存在,会报错提示先删除
3. **数据库连接**:CLI 复用 `.env` 中的数据库配置,确保数据库可正常访问
4. **编码要求**:Windows 环境下确保终端使用 UTF-8 编码(`chcp 65001`)
## 总结
CLI 命令行模式通过 `python generator.py` 启动,支持单表生成、批量生成、预览模式和配置导出。生成前自动检测目标目录是否已存在,避免覆盖已有代码。