1 Star 0 Fork 25

Hellotrik / gfstudy

forked from goflyfox / gfstudy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
19.GoFrame工具链之代码生成.md 2.60 KB
一键复制 编辑 原始数据 按行查看 历史
zcool321@sina.com 提交于 2020-04-29 01:42 . add gf tool chain

GoFrame工具链之代码生成

代码生成gen之model生成

现在gen命令主要是生成model

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

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

ARGUMENT
    PATH  the destination for storing generated files, not necessary, default is "./app/model"

OPTION
    -l, --link    database configuration, please refer to: https://goframe.org/database/gdb/config
    -t, --table   generate models only for given tables, multiple table names separated with ','
    -g, --group   used with "-c" option, 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  remove specified prefix of the table, multiple prefix separated with ','


EXAMPLES
    gf gen model
    gf gen model -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
    gf gen model ./model -l "mssql:sqlserver://sa:12345678@127.0.0.1:1433?database=test"
    gf gen model ./model -c config.yaml -g user-center -t user,user_detail,user_login
    gf gen model -p user_,p_

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

PS D:\17.gfcli> gf gen model ./model -c config/config.toml -p sys_ -t sys_user
2020-04-26 23:35:31.682 [DEBU] [ 51 ms] SHOW FULL COLUMNS FROM `sys_user`
generated: ./model\user\user.go
generated: ./model\user\user_entity.go
generated: ./model\user\user_model.go
done!

D:\17.gfcli> tree /f .\model
卷 Data 的文件夹 PATH 列表
卷序列号为 DA91-D877
D:\17.GFCLI\MODEL
└─user
        user.go
        user_entity.go
        user_model.go
1
https://gitee.com/hellotrik/gfstudy.git
git@gitee.com:hellotrik/gfstudy.git
hellotrik
gfstudy
gfstudy
master

搜索帮助