关于程序员:工具Xorm-reverse

3次阅读

共计 1024 个字符,预计需要花费 3 分钟才能阅读完成。

参考资料

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: reverse
name: testdb
source:
  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 models

import ("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"`
}
正文完
 0