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

使用方式

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

  1. $ gf gen dao -h
  2. USAGE
  3. gf gen dao [OPTION]
  4. OPTION
  5. -/--path directory path for generated files.
  6. -l, --link database configuration, the same as the ORM configuration of GoFrame.
  7. -t, --tables generate models only for given tables, multiple table names separated with ','
  8. -e, --tablesEx generate models excluding given tables, multiple table names separated with ','
  9. -g, --group specifying the configuration group name of database for generated ORM instance,
  10. it's not necessary and the default value is "default"
  11. -p, --prefix add prefix for all table of specified link/database tables.
  12. -r, --removePrefix remove specified prefix of the table, multiple prefix separated with ','
  13. -m, --mod module name for generated golang file imports.
  14. -j, --jsonCase generated json tag case for model struct, cases are as follows:
  15. | Case | Example |
  16. |---------------- |--------------------|
  17. | Camel | AnyKindOfString |
  18. | CamelLower | anyKindOfString | default
  19. | Snake | any_kind_of_string |
  20. | SnakeScreaming | ANY_KIND_OF_STRING |
  21. | SnakeFirstUpper | rgb_code_md5 |
  22. | Kebab | any-kind-of-string |
  23. | KebabScreaming | ANY-KIND-OF-STRING |
  24. -/--stdTime use time.Time from stdlib instead of gtime.Time for generated time/date fields of tables.
  25. -/--gJsonSupport use gJsonSupport to use *gjson.Json instead of string for generated json fields of tables.
  26. -/--modelFile custom file name for storing generated model content.
  27. -/--tplDaoIndex template content for Dao index files generating.
  28. -/--tplDaoInternal template content for Dao internal files generating.
  29. -/--tplModelIndex template content for Model index files generating.
  30. -/--tplModelStruct template content for Model internal files generating.
  31. CONFIGURATION SUPPORT
  32. Options are also supported by configuration file.
  33. It's suggested using configuration file instead of command line arguments making producing.
  34. The configuration node name is "gf.gen.dao", which also supports multiple databases, for example:
  35. [gfcli]
  36. [[gfcli.gen.dao]]
  37. link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  38. tables = "order,products"
  39. jsonCase = "CamelLower"
  40. [[gfcli.gen.dao]]
  41. link = "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
  42. path = "./my-app"
  43. prefix = "primary_"
  44. tables = "user, userDetail"
  45. EXAMPLES
  46. gf gen dao
  47. gf gen dao -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  48. gf gen dao -path ./model -c config.yaml -g user-center -t user,user_detail,user_login
  49. 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
group default 在数据库配置中的数据库分组名称。只能配置一个名称。 defaultorderuser
tables 指定当前数据库中需要执行代码生成的数据表。如果为空,表示数据库的所有表都会生成。 user, userDetail