Xorm 常用工具

Xorm 提供了一组数据库操作命令行工具。


1. 源码安装

Xorm 安装命令行工具的命令:

go get xorm.io/cmd/xorm


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


2. 命令列表

Xorm 有如下可用的命令:

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

1) 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


xorm reverse sqite3 test.db templates/goxorm

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

xorm reverse mymysql xorm_test2/root/ templates/goxorm

xorm reverse postgres "dbname=xorm_test sslmode=disable" templates/goxorm

will generated go files in ./model directory

2) Template and Config

Now, xorm tool supports go and c++ two languages and have go, goxorm, c++ three of default templates. In template directory, we can put a config file to control how to generating.


lang must be go or c++ now.
genJson can be 1 or 0, if 1 then the struct will have json tag.

3) 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.

4) 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.

5) Source

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

6) Driver

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

Xorm 常见问题:如何使用Like?答:engine.Where("column like ?", "%"+char+"%").Find怎么同时使用xorm的tag和json的tag?答:使用空格type User s ...