Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 24 Next »

最新的CLI工具版本功能会随着GoFrame框架的最新版本编译,引入如果本地的CLI工具自动化生成的代码与项目的GoFrame框架版本出现兼容性问题时,建议升级项目框架版本,或者自定义安装旧版本的CLI工具。旧版本CLI工具安装方式参考仓库首页介绍:https://github.com/gogf/gf-cli

使用方式:

$ 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、

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

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

gen命令用以自动化从数据库直接生成采用了Active Record设计模式的模型文件。该命令将会根据数据表名生成对应的目录,该目录名称即数据表包名。目录下自动生成3个文件:

  1. 数据表名.go 自定义文件,开发者可以自由定义填充的代码文件,仅会生成一次,每一次模型生成不会覆盖。
  2. 数据表名_entity.go 表结构文件,根据数据表结构生成的结构体定义文件,包含字段注释。数据表在外部变更后,可使用gen命令重复生成更新该文件。
  3. 数据表名_model.go 表模型文件,为数据表提供了许多便捷的CURD操作方法,并可直接查询返回该表的结构体对象。数据表在外部变更后,可使用gen命令重复生成更新该文件。

$ gf gen model -h
USAGE
    gf gen model [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.

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.model]]
            link   = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
            tables = "order,products"
        [[gfcli.gen.model]]
            link   = "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
            path   = "./my-app"
            prefix = "primary_"
            tables = "user, userDetail"

EXAMPLES
    gf gen model
    gf gen model -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
    gf gen model -path ./model -c config.yaml -g user-center -t user,user_detail,user_login
    gf gen model -r user_

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

3、注意事项

gen dao/model命令生成数据访问相关代码默认支持的数据库类型为:MySQL/MariaDBPostgreSQLSQLServer

如果需要SQLiteOracle数据库类型支持,需要开发者自己修改源码文件后自行本地手动编译生成CLI工具随后安装,因为这两个数据库的驱动需要CGO支持,无法预编译生成给大家直接使用。




  • No labels