Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagexml
$ gf gen -h
USAGE
    gf gen TYPE [OPTION]

TYPE
    dao     generate dao and model files.
    model   generate model files, note that these generated model files are different from model files
            of command "gf gen dao".

DESCRIPTION
    The "gen" command is designed for multiple generating purposes.
    It's currently supporting generating go files for ORM models.
    Please use "gf gen dao -h" or "gf gen model -h" for specified type help.

1、

...

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

Code Block
languagexml
$ 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 ','
    -g, --group          specifying the configuration group name for database,
                         it's not necessary and the default value is "default"
    -c, --config         used to specify the configuration file for database, it's commonly not necessary.
                         If "-l" is not passed, it will search "./config.toml" and "./config/config.toml"
                         in current working directory in 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(default Snake):
                         | Case            | Example            |
                         |---------------- |--------------------|
                         | Camel           | AnyKindOfString    |
                         | CamelLower      | anyKindOfString    |
                         | Snake           | any_kind_of_string |
                         | SnakeScreaming  | ANY_KIND_OF_STRING |
                         | SnakeFirstUpper | rgb_code_md5       |
                         | Kebab           | any-kind-of-string |
                         | KebabScreaming  | ANY-KIND-OF-STRING |


CONFIGURATION SUPPORT
    Options are also supported by configuration file. The configuration node name is "gf.gen", 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_

DESCRIPTION
    The "gen" command is designed for multiple generating purposes.
    It's currently supporting generating go files for ORM models.

配置示例

Code Block
languagetoml
[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"

参数说明:

...

mysql:root:12345678@tcp(127.0.0.1:3306)/user

...

default

order

user

...

order_

user_

...

指定model中生成的数据实体对象中json标签名称规则,参数不区分大小写。参数可选为:CamelCamelLowerSnakeSnakeScreamingSnakeFirstUpperKebabKebabScreaming。具体介绍请参考命名行帮助示例。

...

使用方式:进入项目根目录执行 gf gen dao 即可。

生成的代码结构示例:

Image Removed

其中:

...

2、model代码生成(不再推荐)

使用方式:进入项目根目录执行 gf gen model 即可。

...