golang xorm工具使用 reverse 反轉mysql數據庫結構
風.foxwho
源碼安裝
go get github.com/go-xorm/cmd/xorm
1
mysql 驅動安裝
go get -u github.com/go-sql-driver/mysql
1
編譯驅動
一定要到項目目錄下
go build -tags mysql
1
反轉數據庫結構,生成代碼
在項目目錄下建立templates/goxorm文件夾
這個文件下建立config和struct.go.tpl文件.
模板內容可以根據你自己的需要修改
config內容如下
lang=go
genJson=1
prefix=
1
2
3
struct.go.tpl內容如下
package {{.Models}}
{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
{{range .Imports}}"{{.}}"{{end}}
)
{{end}}
{{range .Tables}}
type {{Mapper .Name}} struct {
{{$table := .}}
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{Mapper $col.Name}} {{Type $col}} {{Tag $table $col}}
{{end}}
}
{{end}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
最后執行命令
xorm reverse mysql root:root@/fox?charset=utf8 templates/goxorm
---------------------