首页 技术文章 后端开发 详解xorm怎么自动生成model
正文 评论

详解xorm怎么自动生成model

本文由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 %

在项目目录下建立 templates/goxorm 文件夹

  • 这个文件下建立config和struct.go.tpl文件.
  • 模板内容可以根据你自己的需要修改
  • config内容如下
    lang=go
    genJson=1prefix=
  • 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}}
  • 最后执行命令

    程序会在当前目录下生成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
  • 在~/.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

剩下就是把models里的内容赋值到model 对应的model里面了

以上就是详解xorm怎么自动生成model的详细内容,更多请关注主题君其它相关文章!

-=||=-收藏赞 (0)
更多主题
单栏极简文艺WordPress博客主题:Diaspora主题
¥ 0 关注:20,031
详情
简约黑白WordPress个人博客主题:Personal主题
¥ 0 关注:19,990
详情
v1.0.0
小程序收录免费WordPress主题:XCX主题
¥ 0 关注:19,608
详情
v1.0.0
一款简约单栏的免费WordPress博客主题:itheme主题
¥ 0 关注:18,113
详情
功能丰富切轻量的WordPress虚拟资源主题:Rizhuti主题
¥ 599 关注:18,000
详情
大气优雅免费WordPress博客主题:HONEY主题
¥ 0 关注:14,853
详情
回复
暂无评论

不要再留垃圾评论了,主题君整理资源不容易,留几句鼓励的话吧。