Xorm reverse是go语言golang数据库转换为代码的命令行工具,能够根据数据库自动反向生成go代码,根据数据表结构创建对应的 struct 模型,非常方便ORM的使用,下面用最简洁的步骤介绍xorm reverse工具安装与使用。go语言练习册

1.安装Reverse

go get xorm.io/reverse

2.查看gopath

go env|grep -i 'gopath'

3.进入gopath目录的

cd /root/go/bin #进入到gopath目录下的bin文件夹

4.ls命令查看一下,如果安装成功,会出现reverse文件。用vi custom.yml文件,文件用来配置连接数据库的信息。保存

kind: reverse

name: testdb

source:

database: mysql

conn_str: 'root:123456@tcp(192.168.1.11:3306)/testdb?parseTime=true'

targets:

- type: codes

language: golang

output_dir: ./testoutput

5.运行reverse工具

./reverse -f custom.yml

6.运行成功。进入testoutput文件夹,自动生成了models.go文件。vi一下看看吧

package models

import (

"time"

)

type Ok struct {

Id int `xorm:"not null pk autoincr index INT"`

Ncee int `xorm:"not null index INT"`

}

type Place struct {

Country string `xorm:"TEXT"`

City string `xorm:"TEXT"`

Telcode int `xorm:"INT"`

}

type Good struct {

Id int `xorm:"not null pk autoincr INT"`

CRate string `xorm:"not null DECIMAL(10)"`

ZRate string `xorm:"not null DECIMAL(10)"`

ARate string `xorm:"not null JSON"`

AllData string `xorm:"JSON"`

Timestamp time.Time `xorm:"not null TIMESTAMP"`

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐