本文由go語言教學專欄跟大家介紹關於xorm自動產生mode的方法 ,希望對需要的朋友有幫助!
xorm自動產生model檔案
luwei@luweideMacBook-Pro-2 go-simple-task % pwd/Users/myself/Golang/common_project/src/go-simple-task luwei@luweideMacBook-Pro-2 go-simple-task % ls Dockerfile go-simple-task go.mod go.sum logic logs main.go model models overall serv templates tool luwei@luweideMacBook-Pro-2 go-simple-task %
如果沒有go- sql-driver/mysql就先安裝
go get -u github.com/go-sql-driver/mysql
安裝xorm,在cmd命令列下
go get github.com/go-xorm/xorm
再安裝xorm的cmd命令工具
go get github.com/go-xorm/cmd/xorm
安裝cmd 報錯處理
luwei@luweideMacBook-Pro-2 go-simple-task % go get github.com/go-xorm/cmd/xorm # github.com/go-xorm/cmd/xorm ../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/dump.go:45:26: cannot use "github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/core".LogLevel) as type "xorm.io/core".LogLevel in argument to engine.Logger().SetLevel ../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/reverse.go:246:34: cannot use tables (type []*"xorm.io/core".Table) as type []*"github.com/go-xorm/core".Table in argument to langTmpl.GenImports ../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/reverse.go:254:18: cannot use table (type *"xorm.io/core".Table) as type *"github.com/go-xorm/core".Table in append ../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/reverse.go:291:26: cannot use table (type *"xorm.io/core".Table) as type *"github.com/go-xorm/core".Table in slice literal ../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/shell.go:60:26: cannot use "github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/core".LogLevel) as type "xorm.io/core".LogLevel in argument to engine.Logger().SetLevel ../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/source.go:45:26: cannot use "github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/core".LogLevel) as type "xorm.io/core".LogLevel in argument to engine.Logger().SetLevel luwei@luweideMacBook-Pro-2 go-simple-task %
luwei@luweideMacBook-Pro-2 go-simple-task % cd ../luwei@luweideMacBook-Pro-2 src % go get github.com/go-xorm/cmd/xorm luwei@luweideMacBook-Pro-2 src %
lang=go genJson=1prefix=
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}}
程式會在目前目錄下產生models資料夾,並在models資料夾中產生go檔
xorm reverse mysql root:root@/fox?charset=utf8 templates/goxorm
luwei@luweideMacBook-Pro-2 go-simple-task % xorm reverse mysql root:123456@/bubble?charset=utf8 templates/goxorm zsh: no matches found: root:123456@/bubble?charset=utf8
vim ~/.zshrc
setopt no_nomatch
luwei@luweideMacBook-Pro-2 go-simple-task % cat ~/.zshrc source ~/.bash_profileexport GOPROXY=https://goproxy.io,direct setopt no_nomatch
source ~/.zshrc
luwei@luweideMacBook-Pro-2 go-simple-task % xorm reverse mysql root:123456@/bubble?charset=utf8 templates/goxorm luwei@luweideMacBook-Pro-2 go-simple-task %
以上是詳解xorm怎麼自動產生model的詳細內容。更多資訊請關注PHP中文網其他相關文章!