Xorm reverse是go語言golang數據庫轉換為代碼的命令行工具,能夠根據數據庫自動反向生成go代碼,根據數據表結構創建對應的 struct 模型,非常方便ORM的使用,下面用最簡潔的步驟介紹xorm reverse工具安裝與使用。go語言練習冊
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"`
}