...
Code Block | ||
---|---|---|
| ||
$ gf gen -h
USAGE
gf gen TYPE [OPTION]
TYPE
dao generate dao and model files.
model generate model files, note that these generated model files are different from model files
of command "gf gen dao".
DESCRIPTION
The "gen" command is designed for multiple generating purposes.
It's currently supporting generating go files for ORM models.
Please use "gf gen dao -h" or "gf gen model -h" for specified type help. |
1、dao
代码生成(推荐)
dao
命令用于生成dao
数据访问对象文件,以及model
数据结构定义文件。推荐使用配置文件来管理生成规则。
Code Block | ||
---|---|---|
| ||
$ gf gen dao -h USAGE gf gen dao [OPTION] OPTION -/--path directory path for generated files. -l, --link database configuration, please refer to: https://goframe.org/database/gdb/config the same as the ORM configuration of GoFrame. -t, --tables generate models only for given tables, multiple table names separated with ',' -g, --group specifying the configuration group name for database, it's not necessary and the default value is "default" -c, --config used to specify the configuration file for database, it's commonly not necessary. If "-l" is not passed, it will search "./config.toml" and "./config/config.toml" in current working directory in default. -p, --prefix add prefix for all table of specified link/database tables. -r, --remove-prefixremovePrefix remove specified prefix of the table, multiple prefix separated with ',' -m, --mod module name for generated golang file imports. -j, --jsonCase generated "json" tag case for model struct, cases are as follows(default Snake): | Case | Example | |---------------- |--------------------| | Camel | AnyKindOfString | | CamelLower | anyKindOfString | | Snake | any_kind_of_string | | SnakeScreaming | ANY_KIND_OF_STRING | | SnakeFirstUpper | rgb_code_md5 | | Kebab | any-kind-of-string | | KebabScreaming | ANY-KIND-OF-STRING | CONFIGURATION SUPPORT Options are also supported by configuration file. The configuration node name is "gf.gen", which also supports multiple databases, for example: [gfcli] [[gfcli.gen.dao]] link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test" tables = "order,products" jsonCase = "CamelLower" [[gfcli.gen.dao]] link = "mysql:root:12345678@tcp(127.0.0.1:3306)/primary" path = "./my-app" prefix = "primary_" tables = "user, userDetail" EXAMPLES gf gen dao gf gen dao gf gen dao -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test" gf gen dao -path ./model -c config.yaml -g user-center -t user,user_detail,user_login gf gen dao -r user_ gf gen model gf gen model gf gen model -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test" gf gen model -path ./model -c config.yaml -g user-center -t user,user_detail,user_login gf gen model -r user_ DESCRIPTION The "gen" command is designed for multiple generating purposes. It's currently supporting generating go files for ORM models. |
1、dao
代码生成(推荐)
...
配置示例:
Code Block | ||
---|---|---|
| ||
[gfcli] [[gfcli.gen.dao]] link = "mysql:root:12345678@tcp(127.0.0.1:3306)/order" group = "order" prefix = "order_" tables = "" [[gfcli.gen.dao]] link = "mysql:root:12345678@tcp(127.0.0.1:3306)/user" group = "user" prefix = "user_" tables = "user,userDetail,userScore" |
...
名称 | 必须 | 默认值 | 含义 | 示例 |
---|---|---|---|---|
gfcli.gen.dao | 是 | dao 代码生成配置项,可以有多个配置项构成数组,支持多个数据库生成。 | - | |
link | 是 | 分为两部分,第一部分表示你连接的数据库类型mysql , postgresql 等, 第二部分就是连接数据库的dsn 信息。具体请参考 ORM使用配置 章节。 |
| |
mod | 否 | 用于生成go 文件的import 计算,默认情况下会自动读取当前项目根目录下的go.mod 获取。 | github.com/gogf/gf-demos | |
group | 否 | default | 在数据库配置中的数据库分组名称。 |
|
tables | 否 | 指定当前数据库中需要执行代码生成的数据表。如果为空,表示数据库的所有表都会生成。 | user, userDetail | |
path | 否 | ./app | 生成dao 和model 文件的存储目录地址 | ./app |
prefix | 否 | 生成数据库对象及文件的前缀,以便区分不同数据库或者不同数据库中的相同表名,防止数据表同名覆盖。 |
| |
remove-prefix removePrefix | 否 | 删除数据表的指定前缀名称。 | gf_ | |
jsonTag | 否 | Snake | 指定 | CamelLower |
使用方式:进入项目根目录执行 gf gen dao
即可。
...
数据表名.go
自定义文件,开发者可以自由定义填充的代码文件,仅会生成一次,每一次模型生成不会覆盖。数据表名_entity.go
表结构文件,根据数据表结构生成的结构体定义文件,包含字段注释。数据表在外部变更后,可使用gen
命令重复生成更新该文件。数据表名_model.go
表模型文件,为数据表提供了许多便捷的CURD
操作方法,并可直接查询返回该表的结构体对象。数据表在外部变更后,可使用gen
命令重复生成更新该文件。
Code Block | ||
---|---|---|
| ||
$ gf gen model -h
USAGE
gf gen model [OPTION]
OPTION
-/--path directory path for generated files.
-l, --link database configuration, the same as the ORM configuration of GoFrame.
-t, --tables generate models only for given tables, multiple table names separated with ','
-g, --group specifying the configuration group name for database,
it's not necessary and the default value is "default"
-c, --config used to specify the configuration file for database, it's commonly not necessary.
If "-l" is not passed, it will search "./config.toml" and "./config/config.toml"
in current working directory in default.
-p, --prefix add prefix for all table of specified link/database tables.
-r, --removePrefix remove specified prefix of the table, multiple prefix separated with ','
-m, --mod module name for generated golang file imports.
CONFIGURATION SUPPORT
Options are also supported by configuration file. The configuration node name is "gf.gen", which also supports
multiple databases, for example:
[gfcli]
[[gfcli.gen.model]]
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables = "order,products"
[[gfcli.gen.model]]
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
path = "./my-app"
prefix = "primary_"
tables = "user, userDetail"
EXAMPLES
gf gen model
gf gen model -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
gf gen model -path ./model -c config.yaml -g user-center -t user,user_detail,user_login
gf gen model -r user_
DESCRIPTION
The "gen" command is designed for multiple generating purposes.
It's currently supporting generating go files for ORM models. |
3、注意事项
gen dao/model
命令生成数据访问相关代码默认支持的数据库类型为:MySQL/MariaDB
、PostgreSQL
、SQLServer
。
...