dao 命令用于生成 dao 数据访问对象文件,以及 model 数据结构定义文件。推荐使用配置文件来管理生成规则。
使用方式
进入项目根目录执行 gf gen dao
即可。以下为命令行帮助信息。
$ gf gen dao -h
USAGE
gf gen dao [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 ','
-e, --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"
-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.
-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 |
-/--stdTime use time.Time from stdlib instead of gtime.Time for generated time/date fields of tables.
-/--gJsonSupport use gJsonSupport to use *gjson.Json instead of string for generated json fields of tables.
-/--modelFile custom file name for storing generated model content.
-/--tplDaoIndex template content for Dao index files generating.
-/--tplDaoInternal template content for Dao internal files generating.
-/--tplModelIndex template content for Model index files generating.
-/--tplModelStruct template content for Model internal files generating.
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 "gf.gen.dao", 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 -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_
配置示例
[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 使用配置 章节。 |
mysql:root:12345678@tcp(127.0.0.1:3306)/user |
|
group |
否 | default |
在数据库配置中的数据库分组名称。只能配置一个名称。 | default 、order 、user |
tables |
否 | 指定当前数据库中需要执行代码生成的数据表。如果为空,表示数据库的所有表都会生成。 | user, userDetail |
|