gen dao
命令是 CLI
中最频繁使用、也是框架设计的工程规范能否准确落地的关键命令。该命令用于生成 dao
数据访问对象、 do
数据转化模型及 entity
实例数据模型 Go
代码文件。由于该命令的参数、选项较多,我们推荐使用配置文件来管理生成规则。
关于框架项目工程规范介绍请查看 代码分层设计 章节。
使用方式
大部分场景下,进入项目根目录执行 gf gen dao
即可。以下为命令行帮助信息。
$ gf gen dao -h
USAGE
gf gen dao [OPTION]
OPTION
-p, --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 ','
-x, --tablesEx generate models excluding given tables, multiple table names separated with ','
-g, --group specifying the configuration group name of database for generated ORM instance,
it's not necessary and the default value is "default"
-f, --prefix add prefix for all table of specified link/database tables
-r, --removePrefix remove specified prefix of the table, multiple prefix separated with ','
-j, --jsonCase generated json tag case for model struct, cases are as follows:
| Case | Example |
|---------------- |--------------------|
| Camel | AnyKindOfString |
| CamelLower | anyKindOfString | default
| Snake | any_kind_of_string |
| SnakeScreaming | ANY_KIND_OF_STRING |
| SnakeFirstUpper | rgb_code_md5 |
| Kebab | any-kind-of-string |
| KebabScreaming | ANY-KIND-OF-STRING |
-i, --importPrefix custom import prefix for generated go files
-d, --daoPath directory path for storing generated dao files under path
-o, --doPath directory path for storing generated do files under path
-e, --entityPath directory path for storing generated entity files under path
-t1, --tplDaoIndexPath template file path for dao index file
-t2, --tplDaoInternalPath template file path for dao internal file
-t3, --tplDaoDoPath template file path for dao do file
-t4, --tplDaoEntityPath template file path for dao entity file
-s, --stdTime use time.Time from stdlib instead of gtime.Time for generated time/date fields of tables
-w, --withTime add created time for auto produced go files
-n, --gJsonSupport use gJsonSupport to use *gjson.Json instead of string for generated json fields of
tables
-v, --overwriteDao overwrite all dao files both inside/outside internal folder
-c, --descriptionTag add comment to description tag for each field
-k, --noJsonTag no json tag will be added for each field
-m, --noModelComment no model comment will be added for each field
-a, --clear delete all generated go files that do not exist in database
-h, --help more information about this command
EXAMPLE
gf gen dao
gf gen dao -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
gf gen dao -p ./model -g user-center -t user,user_detail,user_login
gf gen dao -r user_
CONFIGURATION SUPPORT
Options are also supported by configuration file.
It's suggested using configuration file instead of command line arguments making producing.
The configuration node name is "gfcli.gen.dao", which also supports multiple databases, for example(config.yaml):
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "order,products"
jsonCase: "CamelLower"
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
path: "./my-app"
prefix: "primary_"
tables: "user, userDetail"