dao命令用于生成dao数据访问对象文件,以及model数据结构定义文件。推荐使用配置文件来管理生成规则。

使用方式

进入项目根目录执行 gf gen dao 即可。以下为命令行帮助信息。

  1. $ gf gen dao -h USAGE
  2. gf gen dao [OPTION]
  3. OPTION
  4. -/--path directory path for generated files.
  5. -l, --link database configuration, the same as the ORM configuration of GoFrame.
  6. -t, --tables generate models only for given tables, multiple table names separated with ','
  7. -g, --group specifying the configuration group name of database for generated ORM instance,
  8. it's not necessary and the default value is "default"
  9. -p, --prefix add prefix for all table of specified link/database tables.
  10. -r, --removePrefix remove specified prefix of the table, multiple prefix separated with ','
  11. -m, --mod module name for generated golang file imports.
  12. -j, --jsonCase generated json tag case for model struct, cases are as follows:
  13. | Case | Example |
  14. |---------------- |--------------------|
  15. | Camel | AnyKindOfString |
  16. | CamelLower | anyKindOfString | default
  17. | Snake | any_kind_of_string |
  18. | SnakeScreaming | ANY_KIND_OF_STRING |
  19. | SnakeFirstUpper | rgb_code_md5 |
  20. | Kebab | any-kind-of-string |
  21. | KebabScreaming | ANY-KIND-OF-STRING |
  22. -/--tplDaoIndex template content for Dao index files generating.
  23. -/--tplDaoInternal template content for Dao internal files generating.
  24. -/--tplModelIndex template content for Model index files generating.
  25. -/--tplModelInternal template content for Model internal files generating.
  26. CONFIGURATION SUPPORT
  27. Options are also supported by configuration file.
  28. It's suggested using configuration file instead of command line arguments making producing.
  29. The configuration node name is "gf.gen.dao", which also supports multiple databases, for example:
  30. [gfcli]
  31. [[gfcli.gen.dao]]
  32. link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  33. tables = "order,products"
  34. jsonCase = "CamelLower"
  35. [[gfcli.gen.dao]]
  36. link = "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
  37. path = "./my-app"
  38. prefix = "primary_"
  39. tables = "user, userDetail"
  40. EXAMPLES
  41. gf gen dao
  42. gf gen dao -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  43. gf gen dao -path ./model -c config.yaml -g user-center -t user,user_detail,user_login
  44. gf gen dao -r user_

配置示例

  1. [gfcli]
  2. [[gfcli.gen.dao]]
  3. link = "mysql:root:12345678@tcp(127.0.0.1:3306)/order"
  4. group = "order"
  5. prefix = "order_"
  6. tables = ""
  7. [[gfcli.gen.dao]]
  8. link = "mysql:root:12345678@tcp(127.0.0.1:3306)/user"
  9. group = "user"
  10. prefix = "user_"
  11. tables = "user,userDetail,userScore"

参数说明

名称 必须 默认值 含义 示例
gfcli.gen.dao

| dao代码生成配置项,可以有多个配置项构成数组,支持多个数据库生成。 | - | | link | 是 |

| 分为两部分,第一部分表示你连接的数据库类型mysql, postgresql等, 第二部分就是连接数据库的dsn信息。具体请参考 ORM使用配置
章节。 | mysql:root:12345678@tcp(127.0.0.1:3306)/user | | mod | 否 |

| 用于生成go文件的import计算,默认情况下会自动读取当前项目根目录下的go.mod获取。 | github.com/gogf/gf-demos | | group | 否 | default | 在数据库配置中的数据库分组名称。 | default
order
user | | tables | 否 |

| 指定当前数据库中需要执行代码生成的数据表。如果为空,表示数据库的所有表都会生成。 | user, userDetail | | path | 否 | ./app | 生成dao和model文件的存储目录地址 | ./app | | prefix | 否 |

| 生成数据库对象及文件的前缀,以便区分不同数据库或者不同数据库中的相同表名,防止数据表同名覆盖。 | order
user
| | removePrefix | 否 |

| 删除数据表的指定前缀名称。 | gf_ | | jsonCase | 否 | CamelLower | 指定model中生成的数据实体对象中json标签名称规则,参数不区分大小写。参数可选为:Camel、CamelLower、Snake、SnakeScreaming、SnakeFirstUpper、Kebab、KebabScreaming。具体介绍请参考命名行帮助示例。

| CamelLower |

生成的代码结构示例:

image2020-12-24_17-39-44.png
其中:

  1. dao/internal 以及model/internal 下面的文件由工具生成,多次生成会被覆盖,因此不要手动修改。采用internal包名的目的是仅作为dao或model的内部包引用,不对外开放。
  2. dao 目录下的文件 可以做一些数据库的定制化操作,通过工具多次生成不会覆盖,但是更多建议用户在自己的service中实现。
  3. model目录下的文件,可以做自定义的一些数据结构定义,通过工具多次生成不会覆盖。