代码生成
youlai-gin 内置代码生成模块,用于基于数据库表结构生成前后端代码,并支持在线预览与下载 zip。
接口
接口前缀:/api/v1/codegen
GET /api/v1/codegen/table- 数据表分页(从
information_schema读取表信息)
- 数据表分页(从
GET /api/v1/codegen/{tableName}/config- 获取生成配置
POST /api/v1/codegen/{tableName}/config- 保存生成配置
DELETE /api/v1/codegen/{tableName}/config- 删除生成配置
GET /api/v1/codegen/{tableName}/preview?pageType=classic&type=ts- 在线预览生成结果
GET /api/v1/codegen/{tableName}/download?pageType=classic&type=ts- 下载 zip(
tableName支持逗号分隔批量生成)
- 下载 zip(
配置与持久化
生成配置会落库(用于保存字段配置、页面类型等),涉及表:
gen_tablegen_table_column
模板与产物
- 后端模板:生成
handler/service/repository/model/router等 Go 代码骨架 - 前端模板:默认生成 TypeScript 版本的 API、类型定义与页面;可选 JS 输出
关键实现
- 路由:
internal/codegen/router.go - Handler:
internal/codegen/handler/codegen_handler.go - 核心服务:
internal/codegen/service/codegen_service.go
