
MakeGoCodes是goweb3项目中的代码生成工具可根据数据库表结构自动生成Go实体类、DAO等代码。核心功能包括1通过MetaFactory获取表元数据并缓存2支持MySQL/PostgreSQL多数据库3自动映射数据库类型到Go类型4生成带GORM标签的实体结构体和配套DAO5支持批量生成和驼峰命名转换。工具采用模块化设计包含元数据查询、代码生成、文件写入等流程输出路径为data/code/dbentity/目录下的.go和_dao.go文件。扩展功能包括Elasticsearch索引生成和注释同步。该工具适用于快速构建CRUD应用、同步表结构变更等场景。makego.MakeGoCodes 代码生成工具解析MakeGoCodes是 goweb3 项目中的代码生成工具根据数据库表结构自动生成 Go 实体类、DAO 等代码文件。一、函数定义与入口gofunc MakeGoCodes(tableName ...string) error { for i : 0; i len(tableName); i { var err MakeGoCode(tableName[i]) if err ! nil { return err } } return nil } func MakeGoCode(tableName string) error { var _, err metapropfactroy.FindBeanMetaFactroy().MakeCode( metaprop.FindBeanMetapropService(), tableName) return err }调用链plainTextMakeGoCodes(po_apply) ↓ MakeGoCode(po_apply) ↓ MetaFactroy.MakeCode(IMetapropService, po_apply) ↓ MetaTableCamel(po_apply, false, true) ↓ TableEntity.ToGocodeFile(false)二、核心生成流程1. 元数据获取gofunc (self *MetaFactroy) MakeCode(imeta metaiface.IMetapropService, table string) (string, error) { self.imeta imeta var dto, err self.MetaTableCamel(table, false, true) if err ! nil { return , err } return dto.ToGocodeFile(false) } func (self *MetaFactroy) MetaTableCamel(table string, trimPre bool, camelCase bool) (*tableentity.TableEntity, error) { var meta self.FindMetaprop2Db(table) // 从数据库读取元数据 self.imeta.SaveTableProp(meta) // 保存属性配置 if !meta.TableExist { return nil, errors.New(MetaTableCamel table not found) } var dto tableentity.NewTableEntity() dto.EntityName table for _, column : range meta.Columns { var field dto.AppendField(column.ColumnName, column.DataType) field.GormTag column.ToGormTag(meta.PkInfo.PkName) field.FieldDesc column.ColumnComment } dto.CamelCase camelCase return dto, nil }2. 元数据查询MySQL/PostgreSQL 适配gofunc (this *MetaFactroy) FindMetadata(table string) *metadb.MetadataTable { // 先查缓存 var c, found metadb.InstMetadataCache.CacheGet(table) if found { return c } this.IniDb() this.Table table var metadataTable metadb.NewMetadataTable() if this.IsMysql() { // MySQL 查询 metadataTable.Columns *this.mysql.FindColumns() } else { // PostgreSQL 查询 metadataTable.Columns *this.postgres.FindColumns() } metadataTable.TableSchema this.DbClientDto.Dbname metadataTable.TableName table metadataTable.BuildGoFields() // 构建 Go 字段 // 缓存元数据 metadb.InstMetadataCache.CacheSet(table, meta) return meta }3. 代码生成与文件写入gofunc (self *TableEntity) ToGocodeFile(trimPre bool) (string, error) { // 生成实体代码 var code self.ToGocode(trimPre) err : self.Write2File(self.FullName(), code) // 生成 DAO 代码 var codeDao self.ToGocodeDao(trimPre) err self.Write2File(self.FullNameDao(), codeDao) return code, err } func (self *TableEntity) FullName() string { self.FileName self.DataCode dbentity/ self.EntityName .go return self.FileName } func (self *TableEntity) FullNameDao() string { self.FileName self.DataCode dbentity/ self.EntityName _dao.go return self.FileName }三、生成的文件结构文件类型路径模式说明实体类{DataCode}dbentity/{tableName}.go数据库实体结构DAO类{DataCode}dbentity/{tableName}_dao.go数据访问对象实体类模板结构go// po_apply.go type PoApply struct { Id int64 gorm:column:id;primaryKey ApplyNo string gorm:column:apply_no Status int gorm:column:status CreatedAt time.Time gorm:column:created_at // ... 其他字段 } func (PoApply) TableName() string { return po_apply }DAO类模板结构go// po_apply_dao.go type PoApplyDao struct { *basedao.BaseDao[int64, *PoApply] } func NewPoApplyDao() *PoApplyDao { return PoApplyDao{ BaseDao: basedao.NewBaseDao[int64, *PoApply](), } }四、元数据类型映射从数据库类型到 Go 类型的映射gofunc (this *MetaFactroy) FindGoType(fieldType string) (goType string) { goType metacontext.FindBeanMetadataContext().FindGoType(fieldType) return }典型映射数据库类型Go 类型int,int64int64varchar,textstringdatetime,timestamptime.Timedecimal,floatfloat64booleanbool五、使用示例1. 基本用法go// 生成单个表的代码 makego.MakeGoCodes(po_apply) // 生成多个表的代码 makego.MakeGoCodes(user, order, product)2. 测试代码中的使用gofunc TestMakeGoCode(t *testing.T) { // 生成 po_apply 表的代码 makego.MakeGoCodes(po_apply) }3. 命令行使用通过 goweb3 CLIbash# 生成指定表的代码 goweb3 code -t po_apply # 生成多个表 goweb3 code -t user,order,product六、扩展功能1. Elasticsearch 索引代码生成gofunc MakeGoEsCode(esIndex string) error { var err metafacade.FindBeanMetaFacade().MetaEs(esIndex) return err } func MakeGoEsCodes(esIndex ...string) error { for i : 0; i len(esIndex); i { var err MakeGoEsCode(esIndex[i]) if err ! nil { return err } } return nil }2. 注释同步gofunc (this *MetaFactroy) AlterTableComment(table string, comment string) error { var mt metadb.NewMetadataTable() mt.TableComment comment mt.TableName table if this.IsMysql() { err : mt.AlterTableComment(this.funcGetDb()) return err } return errors.New(invalid dbtype) }七、生成流程图plainText┌─────────────────────────────────────────────────────────────────┐ │ MakeGoCodes 执行流程 │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ 1. MakeGoCodes(po_apply) │ │ ↓ │ │ 2. MakeGoCode(po_apply) │ │ ↓ │ │ 3. MetaFactroy.MakeCode(IMetapropService, po_apply) │ │ ↓ │ │ 4. MetaTableCamel(po_apply, false, true) │ │ │ │ │ ├─→ FindMetaprop2Db(po_apply) ← 读取数据库元数据 │ │ ├─→ 遍历列信息构建 TableEntity.Fields │ │ └─→ 设置 GormTag、FieldDesc │ │ ↓ │ │ 5. TableEntity.ToGocodeFile(false) │ │ │ │ │ ├─→ ToGocode() → 生成实体代码 → Write2File(xxx.go) │ │ └─→ ToGocodeDao() → 生成DAO代码 → Write2File(xxx_dao.go)│ │ ↓ │ │ 输出文件: │ │ - data/code/dbentity/po_apply.go │ │ - data/code/dbentity/po_apply_dao.go │ │ │ └─────────────────────────────────────────────────────────────────┘八、设计特点特点说明元数据缓存避免重复查询数据库提升性能多数据库支持支持 MySQL、PostgreSQL驼峰命名支持下划线到驼峰的自动转换GORM标签自动生成 gorm 结构体标签注释保留保留数据库字段注释作为代码注释批量生成支持同时生成多个表DAO自动生成自动生成数据访问对象九、配置与扩展1. 数据代码路径配置goconst meta_table_out /data/output/meta/dbdict/2. 生成策略gotype TableEntity struct { *metaentity.EntityDto CamelCase bool // 是否使用驼峰命名 }3. 扩展接口gotype IMetapropService interface { SaveTableProp(meta *metadb.MetadataTable) // ... 其他方法 }十、总结makego.MakeGoCodes是一个数据库表结构到 Go 代码的自动生成工具核心能力包括元数据采集从数据库读取表结构信息类型映射数据库类型 → Go 类型代码模板生成实体类和 DAO 类文件输出自动写入指定目录典型应用场景快速构建 CRUD 应用数据库表变更后的代码同步脚手架工具集成团队代码规范统一