Skip to content
项目

go-gorm/gen has 2,562 GitHub stars and 356 forks. GitHot measured 0 stars in 24 hours and +1 in 7 days. Last checked 2026-11-09 18:48 UTC. Last push 2026-10-21.

这些数字如何统计

Gen: Friendly & Safer GORM powered by Code Generation

实测星标增长

24小时
0
7天
+1
15天
更新延迟
1个月
更新延迟

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

tools/gentool/README.ZH_CN.md

原文

GenTool

将Gen作为二进制的方式进行安装

安装

 go install gorm.io/gen/tools/gentool@latest

使用方式

gentool -h  

Usage of gentool:
  -c string
        is path for gen.yml
  -db string
        input mysql|postgres|sqlite|sqlserver|clickhouse. consult[https://gorm.io/docs/connecting_to_the_database.html] (default "mysql")
  -dsn string
        consult[https://gorm.io/docs/connecting_to_the_database.html]
  -fieldCoverable
        generate with pointer when field has default value
  -fieldNullable
        generate with pointer when field is nullable
  -fieldSignable
        detect integer field's unsigned type, adjust generated data type
  -fieldWithIndexTag
        generate field with gorm index tag
  -fieldWithTypeTag
        generate field with gorm column type tag
  -fieldWithDefaultTag
        generate field with gorm default tag
  -modelPkgName string
        generated model code's package name
  -onlyModel
        only generate models (without query file)
  -outFile string
        query code file name, default: gen.go
  -outPath string
        specify a directory for output (default "./dao/query")
  -tables string
        enter the required data table or leave it blank
  -useAny
        emit "any" instead of "interface{}" in generated code (requires Go 1.18+)
  -withDefaultQuery
        create default query in generated code
  -withGeneric
        generate code with generic
  -withQueryInterface
        generate code with exported interface object
  -withUnitTest
        generate unit test for query code
  -unitTestTemplate string
        custom unit test template file path for query code
  -withoutContext
        generate code without context constrain

c

default "" 可以指定配置文件gen.yml的路径。 用配置文件来代替命令行。 命令行是最高优先级。

db

默认值:mysql

可以输入: mysql、 postgres、 sqlite 、 sqlserve

参考:https://gorm.io/docs/connecting_to_the_database.html

dsn

你可以使用GORM所有的连接。

参考:https://gorm.io/docs/connecting_to_the_database.html

fieldNullable

字段可为空时使用指针生成

fieldCoverable

字段有默认值时使用指针生成

fieldWithIndexTag

使用GROM索引标记生成字段

fieldWithTypeTag

使用gorm列类型标记生成字段

fieldWithDefaultTag

使用gorm默认值标记生成字段

modelPkgName

默认值是数据表名称。

生成的model代码的包名称。

outFile

默认为:gen.go

查询代码文件名。

outPath

默认为:/dao/query

指定输出目录

tables

值为 : 输入所需的数据表或将其留空

eg :

​ --tables="orders" #orders 数据表

​ --tables="orders,users" #orders 数据表和 users数据表

​ --tables="" # 数据库中所有的数据表

基于数据表生成对应的代码。

withUnitTest

值为 : False / True

生成单元测试。

unitTestTemplate

自定义 query 单测模板文件路径

fieldSignable

Value : False / True

基于数据表定义的数据类型,生成对应的数据类型

withDefaultQuery

默认: false

为 true 时,生成 query 类的默认实例变量。

withGeneric

默认: false

为 true 时,生成泛型代码。

withQueryInterface

默认: false

为 true 时,生成 query 类的相应接口。

useAny

默认: false

为 true 时,在生成代码中使用 any 代替 interface{}(要求目标模块使用 Go 1.18+ 编译)。注释和字符串字面量中的 interface{} 文本也会被一并改写。

withoutContext

默认: false

为 true 时,生成的 query 类不接受 context 参数。

使用示例

gentool -dsn "user:pwd@tcp(127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "orders,doctor"

探索相关项目