下载
https://gitea.com/xorm/reverse
编译打包
go build -o reverse main.go
放入gopath目录下;
验证:
reverse -v
reverse version 0.1+dev
新建 custom.yml 内容如下:
kind: reverse
name: mydb
source:
database: mysql
conn_str: 'root:123456@tcp(192.168.0.201:3306)/test?charset=utf8'
targets:
- type: codes
language: golang
table_prefix: "" # 表前缀
multiple_files: true # 是否生成多个文件
template: | # 生成模板,如果这里定义了,优先级比 template_path 高
package models
{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
{{range .Imports}}"{{.}}"{{end}}
)
{{end}}
{{range .Tables}}
type {{TableMapper .Name}} struct {
{{$table := .}}
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{ColumnMapper $col.Name}} {{Type $col}} `{{Tag $table $col}}`
{{end}}
}
{{end}}
output_dir: ./models # 代码生成目录
执行命令:
reverse -f .\custom.yml
本地models目录下生成结构OK;
pg.yml
kind: reverse
name: mydb
source:
database: postgres
conn_str: 'postgres://postgres:123456@192.168.0.201:5432/test_db?sslmode=disable'
targets:
- type: codes
# include_tables:
# - tb_user
# - tb_order
# exclude_tables:
# - place
# - person
language: golang
table_prefix: "" # 表前缀
multiple_files: true # 是否生成多个文件
template: | # 生成模板,如果这里定义了,优先级比 template_path 高
package models
{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
{{range .Imports}}"{{.}}"{{end}}
)
{{end}}
{{range .Tables}}
type {{TableMapper .Name}} struct {
{{$table := .}}
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{ColumnMapper $col.Name}} {{Type $col}} `{{Tag $table $col}}`
{{end}}
}
{{end}}
output_dir: ./models5 # 代码生成目录
网友评论