准备用go语言去操作数据库 找了半天就找到了xorm听说还不错 用起来就是大坑 官方手册看的蒙圈 xorm官网
首先下载reverse可执行文件
https://gitea.com/xorm/reverse/releases
选择对应的系统版本 下载完成了放到项目目录下面
然后就是创建配置文件 和reverse同一目录下 创建一个custom.yml
这里坑来了我是mysql 官网没写mysql怎么连接 在就是用那个极简版本的配置无法生成
kind: reverse
name: mydb
source:
database: mysql
conn_str: "user:password@tcp(MysqlIP:MysqlPort)/dbname?charset=utf8"
targets:
- type: codes
include_tables: # tables included, you can use **
- "*"
exclude_tables: # tables excluded, you can use **
- c
table_mapper: snake # how table name map to class or struct name
column_mapper: snake # how column name map to class or struct field name
table_prefix: "" # table prefix
multiple_files: true # generate multiple files or one
language: golang
template: | # template for code file, it has higher perior than 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}}
}
func (m *{{TableMapper .Name}}) TableName() string {
return "{{$table.Name}}"
}
{{end}}
template_path: ./template/goxorm.tmpl # template path for code file, it has higher perior than template field on language
output_dir: ./models # code output directory
然后运行就是
reverse--windows-amd64.exe -f custom.yml
就会自动创建好了
Comments | NOTHING