Home > Backend Development > Golang > Detailed explanation of how xorm automatically generates models

Detailed explanation of how xorm automatically generates models

藏色散人
Release: 2021-11-01 16:11:36
forward
2647 people have browsed it

This article is written by the go language tutorial column to introduce to you the method of automatically generating mode by xorm. I hope it will be helpful to friends in need!

xorm automatically generates model files

Enter the project root directory

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 %
Copy after login

If there is no go- sql-driver/mysql first install

go get -u github.com/go-sql-driver/mysql
Copy after login

Install xorm, under the cmd command line

go get github.com/go-xorm/xorm
Copy after login

then install the xorm cmd command tool

go get github.com/go-xorm/cmd/xorm
Copy after login

Installation cmd error handling

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 %
Copy after login

Jump out of the project directory to execute, then jump back when finished

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 %
Copy after login

Create in the project directory templates/goxorm folder

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

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

    xorm reverse mysql root:root@/fox?charset=utf8 templates/goxorm
    Copy after login

Execution error

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
Copy after login
  • Solution
    vim ~/.zshrc
    Copy after login
  • Add to ~/.zshrc:
    setopt no_nomatch
    Copy after login
  • Check
    luwei@luweideMacBook-Pro-2 go-simple-task % cat ~/.zshrc                                              
    source ~/.bash_profileexport GOPROXY=https://goproxy.io,direct
    setopt no_nomatch
    Copy after login
  • Finally execute
    source ~/.zshrc
    Copy after login

##Execute again
luwei@luweideMacBook-Pro-2 go-simple-task % xorm reverse mysql root:123456@/bubble?charset=utf8 templates/goxorm
luwei@luweideMacBook-Pro-2 go-simple-task %
Copy after login

Detailed explanation of how xorm automatically generates models

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

The above is the detailed content of Detailed explanation of how xorm automatically generates models. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template