The gen dao command is the most frequently used command in the CLI and is crucial for the accurate implementation of the framework’s engineering specifications. This command is used to generate dao data access objects, do data transformation models, and entity instance data model Go code files. Due to the numerous parameters and options for this command, it is recommended to manage generation rules using configuration files.

Dao/Do/Entity Generating - 图1tip

For an introduction to the framework’s project engineering specifications, please see the section Code Layering.

Usage

In most scenarios, execute gf gen dao in the project’s root directory. Below is the command line help information.

  1. $ gf gen dao -h
  2. USAGE
  3. gf gen dao [OPTION]
  4. OPTION
  5. -p, --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. -x, --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. -f, --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. -rf, --removeFieldPrefix remove specified prefix of the field, multiple prefix separated with ','
  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. -i, --importPrefix custom import prefix for generated go files
  25. -d, --daoPath directory path for storing generated dao files under path
  26. -o, --doPath directory path for storing generated do files under path
  27. -e, --entityPath directory path for storing generated entity files under path
  28. -t1, --tplDaoIndexPath template file path for dao index file
  29. -t2, --tplDaoInternalPath template file path for dao internal file
  30. -t3, --tplDaoDoPath template file path for dao do file
  31. -t4, --tplDaoEntityPath template file path for dao entity file
  32. -s, --stdTime use time.Time from stdlib instead of gtime.Time for generated time/date fields of tables
  33. -w, --withTime add created time for auto produced go files
  34. -n, --gJsonSupport use gJsonSupport to use *gjson.Json instead of string for generated json fields of
  35. tables
  36. -v, --overwriteDao overwrite all dao files both inside/outside internal folder
  37. -c, --descriptionTag add comment to description tag for each field
  38. -k, --noJsonTag no json tag will be added for each field
  39. -m, --noModelComment no model comment will be added for each field
  40. -a, --clear delete all generated go files that do not exist in database
  41. -y, --typeMapping custom local type mapping for generated struct attributes relevant to fields of table
  42. -fm, --fieldMapping custom local type mapping for generated struct attributes relevant to specific fields of
  43. table
  44. -/--genItems
  45. -h, --help more information about this command
  46. EXAMPLE
  47. gf gen dao
  48. gf gen dao -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  49. gf gen dao -p ./model -g user-center -t user,user_detail,user_login
  50. gf gen dao -r user_
  51. CONFIGURATION SUPPORT
  52. Options are also supported by configuration file.
  53. It's suggested using configuration file instead of command line arguments making producing.
  54. The configuration node name is "gfcli.gen.dao", which also supports multiple databases, for example(config.yaml):
  55. gfcli:
  56. gen:
  57. dao:
  58. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  59. tables: "order,products"
  60. jsonCase: "CamelLower"
  61. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
  62. path: "./my-app"
  63. prefix: "primary_"
  64. tables: "user, userDetail"
  65. typeMapping:
  66. decimal:
  67. type: decimal.Decimal
  68. import: github.com/shopspring/decimal
  69. numeric:
  70. type: string
  71. fieldMapping:
  72. table_name.field_name:
  73. type: decimal.Decimal
  74. import: github.com/shopspring/decimal

Dao/Do/Entity Generating - 图2tip

If using the framework-recommended project engineering scaffold and the system has the make tool installed, the shortcut command make dao can also be used.

Configuration Example

File configuration example:

hack/config.yaml

  1. gfcli:
  2. gen:
  3. dao:
  4. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  5. tables: "order,products"
  6. jsonCase: "CamelLower"
  7. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
  8. path: "./my-app"
  9. prefix: "primary_"
  10. tables: "user, userDetail"
  11. # sqlite requires manual compilation of gf with sqlite driver, modify the import path file (gf\cmd\gf\internal\cmd\cmd_gen_dao.go) to uncomment for sqlite driver, gcc is required for sqlite driver
  12. - link: "sqlite:./file.db"

Parameter Description

NameDefault ValueMeaningExample
gfcli.gen.daodao code generation configuration items, can have multiple configuration items to form an array, supporting multiple database generation. Different databases can set different generation rules, for example, they can be generated to different locations or files.-
linkRequired Parameter. It consists of two parts, the first part represents your connected database type mysql, postgresql, etc., while the second part is the dsn information for the connection. For specific details, please refer to the section ORM - Configuration.-
pathinternalThe storage directory address for generated dao and model files../app
groupdefaultDatabase group name in the database configuration. Only one name can be configured. The group name in the database configuration file is often not modified once determined.default
order
user
prefixPrefixes for the generated database objects and files to distinguish between different databases or the same table names in different databases, to prevent data table name overwriting.order
user
removePrefixRemoves specified prefix names from data tables. Multiple prefixes are separated by commas.gf
removeFieldPrefixRemoves specified prefix names from field names. Multiple prefixes are separated by commas.f
tablesSpecifies which tables in the current database need code generation. If empty, all tables in the database will be generated.user, user_detail
tablesExTables Excluding, specifies which tables in the current database are excluded from code generation.product, order
jsonCaseCamelLowerSpecifies the naming rules for json tags in generated data entity objects in model, the parameter is case-insensitive. Optional parameters: Camel, CamelLower, Snake, SnakeScreaming, SnakeFirstUpper, Kebab, KebabScreaming. For a detailed introduction, please refer to the command line help example.Snake
stdTimefalseWhen a data table field type is a time type, the generated property type uses the standard library time.Time instead of the framework’s gtime.Time type.true
withTimefalseAdds generation time comments to each automatically generated code file
gJsonSupportfalseWhen a data table field type is JSON, the generated property type uses gjson.Json type.true
overwriteDaofalseWhether to regenerate and overwrite files outside the dao/internal directory for each dao code generation. Note that files outside the dao/internal directory may have been custom extended by the developer, and overwriting may introduce risk.true
importPrefixAutomatically detected via go.modUsed to specify the import path prefix for generated Go files. Especially necessary when the gen dao command is not used at the project’s root directory or when intending to generate code files to a custom directory.github.com/gogf/gf
descriptionTagfalseSpecifies whether to add a description tag with the schema for each data model struct field, where the content is the corresponding data table field comment.true
noJsonTagfalseThe generated data model does not include json tags for fields
noModelCommentfalseSpecifies whether to disable automatic generation of comments for model struct fields, where the content is derived from the data table field comments.true
clearfalseAutomatically deletes local dao/do/entity code files that do not correspond to any data tables in the database. Use this parameter with caution!
daoPathdaoDirectory for storing generated DAO files
doPathmodel/doDirectory for storing generated DO files
entityPathmodel/entityDirectory for storing generated Entity files
tplDaoIndexPathCustom DAO Index code generation template file path, please refer to the source code for use
tplDaoInternalPathCustom DAO Internal code generation template file path, please refer to the source code for use
tplDaoDoPathCustom DO code generation template file path, please refer to the source code for use
tplDaoEntityPathCustom Entity code generation template file path, please refer to the source code for use
typeMappingSupported from version v2.5. Used to customize the mapping of data table fields types to corresponding types in generated Go files.
fieldMappingSupported from version v2.8. Used to customize the mapping of specific data table fields to corresponding field types in generated Go files.

Parameter: typeMapping

The typeMapping parameter supports configuring the database field types corresponding to Go data types, and the default value is:

  1. decimal:
  2. type: float64
  3. money:
  4. type: float64
  5. numeric:
  6. type: float64
  7. smallmoney:
  8. type: float64

This configuration supports importing third-party packages via the import configuration item, for example:

  1. decimal:
  2. type: decimal.Decimal
  3. import: github.com/shopspring/decimal

Parameter: fieldMapping

The fieldMapping parameter provides fine-grained field type mapping configuration, supporting the configuration of specific database fields’ generated Go data types. Apart from a different configuration name, the configuration content is the same as typeMapping. Example configuration:

  1. paid_orders.amount:
  2. type: decimal.Decimal
  3. import: github.com/shopspring/decimal

In the example, paid_orders is the table name, amount is the field name, type represents the data type name in the generated Go code, and import represents third-party packages that need to be imported in the generated code.

Usage Example

Repository address: https://github.com/gogf/focus-single

Dao/Do/Entity Generating - 图3

  1. Files in the following 3 directories are generated by the dao command:
PathDescriptionDetailed Introduction
/internal/daoData Access ObjectAccesses the underlying data source through the object-oriented way, based on the ORM component. Often used in combination with entity and do. Files in this directory can be extended and modified by developers.
/internal/model/doData Transformation ModelThe data transformation model is used for converting business models to data models, maintained by tools, and should not be modified by users. The tool will overwrite this directory each time code files are generated. For an introduction to do files, please refer to:
- Data and Business Models
- Pain Points and Improvements In Business Project
- Utilizing Pointer Properties and Do Objects for Flexible Modification Interfaces
/internal/model/entityData ModelThe data model is maintained by tools and should not be modified by users. The tool will overwrite this directory each time code files are generated.
  1. Models in the model directory are divided into two categories: Data Models and Business Models.

Data Models: Automatically generated by the CLI tool in the model/entity directory. The database tables will all be generated in this directory, and the files in this directory correspond to data models. Data models are data structures that correspond one-to-one with database tables; developers usually do not need to modify them and should not, as data models are automatically updated by the CLI tool when there are changes to the database table structures. Data models are generated and maintained uniformly by the CLI tool.

Business Models: Business models are data structures related to the business, defined as needed, such as service input and output data structure definitions and some internal data structure definitions. Business models are defined and maintained by developers according to business needs, defined in the model directory.

  1. Files in the dao folder are named after database table names, with one file and its corresponding DAO object for each database table. Operations on database tables are implemented through DAO objects and related methods. The dao operation adopts a standardized design, requiring the ctx parameter to be passed, and the generated code must be operated by creating objects through the Ctx or Transaction method to chain operation on database tables.

Dao/Do/Entity Generating - 图4

Notes

Databases Requiring Manual Compilation

The gen dao command involves data access-related code generation and supports several commonly used database types by default. If Oracle database support is needed, developers must modify the source code files and manually compile and install the CLI tool locally, as the driver for these databases require CGO support and cannot be precompiled for general use.

Dao/Do/Entity Generating - 图5

About bool Type Corresponding to Database Table Fields

Since most database types do not have bool type table field types, we recommend using bit(1) to represent a field’s bool type instead of tinyint(1) or int(1). Because the tinyint(1)/int(1) field type represents a range of -127~127, it is usually used as a status field type. While the bit(1) type range is 0/1, which can accurately represent the bool type’s two values false/true.

For example, table fields:

Dao/Do/Entity Generating - 图6

Generated properties:

Dao/Do/Entity Generating - 图7