xorm 工具使用

xorm 工具使用

Posted by 锐玩道 on May 21, 2021

如果❤️我的文章有帮助,欢迎点赞、关注。这是对我继续技术创作最大的鼓励。更多往期文章在我的个人博客

xorm 工具

xorm 是一组数据库操作命令行工具。

二进制安装

如果你安装了 got,你可以输入如下命令安装:

got go-xorm/cmd/xorm

或者你可以从 gobuild 下载后解压到可执行路径。

源码安装

go get github.com/go-xorm/cmd/xorm

同时你需要安装如下依赖:

** 对于 sqlite3 的支持,你需要自己进行编译 go build -tags sqlite3 因为 sqlite3 需要 cgo 的支持。

命令列表

有如下可用的命令:

  • reverse 反转一个数据库结构,生成代码
  • shell 通用的数据库操作客户端,可对数据库结构和数据操作
  • dump Dump 数据库中所有结构和数据到标准输出
  • source 从标注输入中执行 SQL 文件
  • driver 列出所有支持的数据库驱动

reverse

Reverse command is a tool to convert your database struct to all kinds languages of structs or classes. After you installed the tool, you can type

xorm help reverse to get help

mysql 驱动安装

go get -u github.com/go-sql-driver/mysql

反转数据库结构,生成代码

在项目目录下建立templates/goxorm文件夹 这个文件下建立config和struct.go.tpl文件. 模板内容可以根据你自己的需要修改 config内容如下

lang=go
genJson=1
prefix=

struct.go.tpl内容如下


package 



import (
    ""
)



type  struct {

 	 

}


最后执行命令

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

Shell

Shell command provides a tool to operate database. For example, you can create table, alter table, insert data, delete data and etc.

xorm shell sqlite3 test.db will connect to the sqlite3 database and you can type help to list all the shell commands.

Dump

Dump command provides a tool to dump all database structs and data as SQL to your standard output.

xorm dump sqlite3 test.db could dump sqlite3 database test.db to standard output. If you want to save to file, just
type xorm dump sqlite3 test.db > test.sql.

Source

xorm source sqlite3 test.db < test.sql will execute sql file on the test.db.

Driver

List all supported drivers since default build will not include sqlite3.

LICENSE

BSD License
http://creativecommons.org/licenses/BSD/

xorm 文档: https://gobook.io/read/gitea.com/xorm/manual-zh-CN/