参考资料

Xorm reverse工具装置与应用 依据数据库主动生成go代码
golang xorm reverse 主动生成数据库实体文件

Xorm reverse是go语言golang数据库转换为代码的命令行工具,可能依据数据库主动反向生成go代码,依据数据表构造创立对应的 struct 模型,十分不便ORM的应用,上面用最简洁的步骤介绍xorm reverse工具装置与应用。

1.装置Reverse

go get xorm.io/reverse

2.查看gopath

go env|grep -i 'gopath'

3.进入gopath目录的

cd /root/go/bin          #进入到gopath目录下的bin文件夹

4.ls命令查看一下,如果装置胜利,会呈现reverse文件

用vi custom.yml文件,文件用来配置连贯数据库的信息。保留

kind: reversename: testdbsource:  database: mysql  conn_str: 'root:123456@tcp(192.168.1.11:3306)/testdb?parseTime=true'targets:- type: codes  language: golang  output_dir: ./testoutput

5.运行reverse工具

./reverse -f custom.yml

6. 运行胜利。进入testoutput文件夹,主动生成了models.go文件

vi一下看看吧

package modelsimport (        "time")type Ok struct {        Id   int `xorm:"not null pk autoincr index INT"`        Ncee int `xorm:"not null index INT"`}type Place struct {        Country string `xorm:"TEXT"`        City    string `xorm:"TEXT"`        Telcode int    `xorm:"INT"`}type Good struct {        Id            int     `xorm:"not null pk autoincr INT"`        CRate       string    `xorm:"not null DECIMAL(10)"`        ZRate       string    `xorm:"not null DECIMAL(10)"`        ARate       string    `xorm:"not null JSON"`        AllData     string    `xorm:"JSON"`        Timestamp     time.Time `xorm:"not null TIMESTAMP"`}