分享两个常用的代码生成工具:
gormgen
handlergen
gormgen基于 MySQL 数据表结构进行生成 3 个文件:
生成表的 struct 结构体
生成表的 Markdown 文档
生成表的 CURD 方法
场景在进行业务需求开发时,创建完数据表后,执行代码生成工具,常用的 CURD 操作全部生成完毕,使用的时候只需要 . 后面的方法即可,这样大大提高了业务开发效率。
示例表结构:
CREATE TABLE `user_demo` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', `user_name` varchar(32) NOT NULL DEFAULT '' COMMENT '用户名', `nick_name` varchar(100) NOT NULL DEFAULT '' COMMENT '昵称', `mobile` varchar(20) NOT NULL DEFAULT '' COMMENT '手机号', `is_deleted` tinyint(1) NOT NULL DEFAULT '-1' COMMENT '是否删除 1:是 -1:否', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户Demo表';1、在 config 中设置需要自动生成的表,参数为 cmd.genTables,如果设置为空,表示生成当前数据库中的所有的表,如果设置多张表还可以用 “,” 分割。
[cmd] genTables = 'user_demo'2、在根目录下执行脚本
./scripts/gormgen.sh执行完毕后,会在 /internal/api/repository/db_repo 中生成 user_demo_repo 目录,同时也会生成 3 个文件:
gen_model.go
gen_table.md
gen_user_demo.go
gen_model.go 内容如下:
package user_demo_repo import "time" // 用户Demo表 //go:generate gormgen -structs UserDemo -input . type UserDemo struct { Id int32 // 主键 UserName string // 用户名 NickName string // 昵称 Mobile string // 手机号 IsDeleted int32 // 是否删除 1:是 -1:否 CreatedAt time.Time `gorm:"time"` // 创建时间 UpdatedAt time.Time `gorm:"time"` // 更新时间 }gen_table.md Markdown 内容如下:
gen_user_demo.go 内容如下:
func NewModel() *UserDemo {...} func NewQueryBuilder() *userDemoRepoQueryBuilder {...} func (t *UserDemo) Create(db *gorm.DB) (id int32, err error) {...} func (t *UserDemo) Delete(db *gorm.DB) (err error) {...} func (t *UserDemo) Updates(db *gorm.DB, m map[string]interface{}) (err error) {...} type userDemoRepoQueryBuilder struct {...} func (qb *userDemoRepoQueryBuilder) buildQuery(db *gorm.DB) *gorm.DB {...} func (qb *userDemoRepoQueryBuilder) Count(db *gorm.DB) (int64, error) {...} func (qb *userDemoRepoQueryBuilder) First(db *gorm.DB) (*UserDemo, error) {...} func (qb *userDemoRepoQueryBuilder) QueryOne(db *gorm.DB) (*UserDemo, error) {...} func (qb *userDemoRepoQueryBuilder) QueryAll(db *gorm.DB) ([]*UserDemo, error) {...} func (qb *userDemoRepoQueryBuilder) Limit(limit int) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) Offset(offset int) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) WhereId(p db_repo.Predicate, value int32) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) OrderById(asc bool) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) WhereUserName(p db_repo.Predicate, value string) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) OrderByUserName(asc bool) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) WhereNickName(p db_repo.Predicate, value string) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) OrderByNickName(asc bool) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) WhereMobile(p db_repo.Predicate, value string) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) OrderByMobile(asc bool) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) WhereIsDeleted(p db_repo.Predicate, value int32) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) OrderByIsDeleted(asc bool) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) WhereCreatedAt(p db_repo.Predicate, value time.Time) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) OrderByCreatedAt(asc bool) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) WhereUpdatedAt(p db_repo.Predicate, value time.Time) *userDemoRepoQueryBuilder {...} func (qb *userDemoRepoQueryBuilder) OrderByUpdatedAt(asc bool) *userDemoRepoQueryBuilder {...} 使用这样使用生成后的方法:
// 查询示例: user_demo_repo.NewQueryBuilder(). WhereUserName(db_repo.EqualPredicate, "tom"). OrderById(true). QueryOne(u.db.GetDbR().WithContext(ctx.RequestContext())) handlergen基于定义的 Handler 文件中 type interface 中接口方法,进行生成文件。
场景本次需求的研发负责人通过定义 type interface 的方式,定义出需要开发的方法,执行代码生成工具,每个方法的空实现都会生成在一个单独的文件中,开发人员只需去实现各自方法即可,便于进行分工和代码管理。
示例