台州网站专业制作,物联网的网络架构,环保厅网站建设的必要性,织梦wordpressgorm的自动化工具gen
官方
https://gorm.io/zh_CN/gen/假设数据库结构如 这里使用gen-tool
安装
go install gorm.io/gen/tools/gentoollatest用法
gentool -hUsage of gentool:-c string配置文件名、默认值 “”、命令行选项的优先级高于配置文件。 -db string指定Driver…gorm的自动化工具gen
官方
https://gorm.io/zh_CN/gen/假设数据库结构如 这里使用gen-tool
安装
go install gorm.io/gen/tools/gentoollatest用法
gentool -hUsage of gentool:-c string配置文件名、默认值 “”、命令行选项的优先级高于配置文件。 -db string指定Driver默认值“mysql”refererhttps://gorm.io/docs/connecting_to_the_database.html-dsn string用于连接数据库的DSN reference: https://gorm.io/docs/connecting_to_the_database.html-fieldNullable当字段允许空时用指针生成-fieldWithIndexTag生成带有gorm index 标签的字段-fieldWithTypeTag生成带有gorm type标签的字段-modelPkgName string生成模型代码包名称。-outFile stringGenrated 查询代码文件名称默认值gen.go-outPath string指定输出目录(默认 “./dao/query”)-tables string指定要生成的表名称默认所有表。-onlyModel指生成Models不生成对应的query-withUnitTest生成单元测试默认值 false, 选项: false / true-fieldSignabledetect integer fields unsigned type, adjust generated data typeExample
gentool -dsn user:pwdtcp(localhost:3306)/database?charsetutf8mb4parseTimeTruelocLocal -tables orders,doctorgentool -c ./gen.tool # 配置文件像下面version: 0.1
database:# consult[https://gorm.io/docs/connecting_to_the_database.html]dsn : username:passwordtcp(address:port)/db?charsetutf8mb4parseTimetruelocLocal# 选择mysql或者其他引擎比方sqlserverdb : mysql# 指定要生成的table,流控则全部tables : user# 指定输出目录outPath : ./dao/query# 输出的代码默认gen.gooutFile : # 是否生成单元测试withUnitTest : false# generated model codes package name# 生成的model的代码的包名modelPkgName : # 使用指针当字段是空的fieldNullable : false# 生成的字段带有gorm tagfieldWithIndexTag : false# 生成的字段时候带有gorm type 标签fieldWithTypeTag : falseubuntu将gobin加入到PATH的做法
个人来说gentool没有被加入到PATH中这边手动把GOPATH加入到PATH中,我用的是
zsh,所以把环境变量加入到~/.zshrc中参考下面的命令 echo export PATH$PATH:~/go/bin | tee -a ~/.zshrc现在gentool可以在任意地方被调用了 实例
在项目根目录新疆gentool文件里面写入内容
version: 0.1
database:# consult[https://gorm.io/docs/connecting_to_the_database.html]dsn : root:roottcp(127.0.0.1:3306)/school?charsetutf8mb4parseTimetruelocLocal# 选择mysql或者其他引擎比方sqlserverdb : mysql# 指定要生成的table,流控则全部# 指定输出目录outPath : ./dao/query# 输出的代码默认gen.gooutFile : # 是否生成单元测试withUnitTest : false# generated model codes package name# 生成的model的代码的包名modelPkgName : models# 使用指针当字段是空的fieldNullable : false# 生成的字段带有gorm tagfieldWithIndexTag : false# 生成的字段时候带有gorm type 标签fieldWithTypeTag : false然后使用gentool指定-c
结果如在dao包下生成了对应的models和query