Xorm automatically generates model

Time:2021-11-25

Xorm automatically generates model files

Enter the project root directory

[email protected] go-simple-task % pwd
/Users/myself/Golang/common_project/src/go-simple-task
[email protected] go-simple-task % ls
Dockerfile      go-simple-task  go.mod          go.sum          logic           logs            main.go         model           models          overall         serv            templates       tool
[email protected] go-simple-task % 
  • If you do not have go SQL driver / MySQL, install it first
    go get -u github.com/go-sql-driver/mysql
  • Install xorm on the CMD command line
    go get github.com/go-xorm/xorm
  • Then install the CMD command tool of xorm
    go get github.com/go-xorm/cmd/xorm
  • Install CMD error handling
    [email protected] go-simple-task % go get github.com/go-xorm/cmd/xorm
    # github.com/go-xorm/cmd/xorm
    ../../pkg/mod/github.com/go-xorm/cmd/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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
    [email protected] go-simple-task % 

    Jump out of the project directory for execution, and then jump back

    [email protected] go-simple-task % cd ../
    [email protected] src % go get github.com/go-xorm/cmd/xorm
    [email protected] src % 

    Create the templates / goxorm folder in the project directory

  • Create config and struct.go.tpl files under this file
  • The template content can be modified according to your own needs
  • Config contents are as follows
    lang=go
    genJson=1
    prefix=
  • The content of struct.go.tpl is as follows
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}}
  • Finally execute the command

    The program will generate the models folder in the current directory and generate the go file in the models folder

    xorm reverse mysql root:[email protected]/fox?charset=utf8 templates/goxorm

Execution error

[email protected] go-simple-task % xorm reverse mysql root:[email protected]/bubble?charset=utf8 templates/goxorm
zsh: no matches found: root:[email protected]/bubble?charset=utf8
  • Solution
    vim ~/.zshrc
  • Add in ~ /. Zshrc:
    setopt no_nomatch
  • Check it
    [email protected] go-simple-task % cat ~/.zshrc                                              
    source ~/.bash_profile
    export GOPROXY=https://goproxy.io,direct
    setopt no_nomatch
  • Final execution
    source ~/.zshrc

Execute again

[email protected] go-simple-task % xorm reverse mysql root:[email protected]/bubble?charset=utf8 templates/goxorm
[email protected] go-simple-task % 

Xorm automatically generates model

The rest is to assign the content in models to the model corresponding to model

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Vue、Three. JS implementation panorama

1、 First, we need to create a Vue project This paper mainly records the process of building panorama in detail, so building Vue project is not described too much. 2、 Install three js npm install three –save npm install three-trackballcontrols –save npm install three-orbit-controls –save npm i three-obj-mtl-loader –save npm i three-fbx-loader –save npm i […]