XORM 中文文檔地址:?https://xorm.io/zh/

注意通知?XORM 已經(jīng)從?Github?遷移到?Gitea?! 所以之前包的安裝地址都需要改變
XORM 安裝命令?
go get xorm.io/xorm?
XORM 工具安裝
go get xorm.io/cmd/xorm?
XORM 工具幫助命令
xorm help reverse

到此xorm工具安裝成功
xorm工具命令列表:
reverse?反轉(zhuǎn)一個數(shù)據(jù)庫結(jié)構(gòu),生成代碼
shell?通用的數(shù)據(jù)庫操作客戶端,可對數(shù)據(jù)庫結(jié)構(gòu)和數(shù)據(jù)操作
dump?Dump數(shù)據(jù)庫中所有結(jié)構(gòu)和數(shù)據(jù)到標(biāo)準(zhǔn)輸出
source?從標(biāo)注輸入中執(zhí)行SQL文件
driver?列出所有支持的數(shù)據(jù)庫驅(qū)動
反轉(zhuǎn)
Reverse 命令讓你根據(jù)數(shù)據(jù)庫的表來生成結(jié)構(gòu)體或者類代碼文件。安裝好工具之后,可以通過
xorm help reverse
獲得幫助。
例子:
首先要進(jìn)入到當(dāng)前項目的目錄下,主要是后面的命令最后一個參數(shù)中用到的模版存放在當(dāng)前項目的目錄下
cd $GOPATH/src/github.com/go-xorm/cmd/xorm
sqlite:?xorm reverse sqite3 test.db templates/goxorm
mysql:?xorm reverse mysql root:@/xorm_test?charset=utf8 templates/goxorm
mymysql:?xorm reverse mymysql xorm_test2/root/ templates/goxorm
postgres:?xorm reverse postgres "dbname=xorm_test sslmode=disable" templates/goxorm
之后將會生成代碼 generated go files in?./model?directory
xorm reverse mysql "root:1@tcp(localhost:3306)/demo_go_gin" ./templates/goxorm/ C:/demo-go-gin/models
模版和配置
當(dāng)前,默認(rèn)支持Go,C++ 和 objc 代碼的生成。具體可以查看源碼下的 templates 目錄。在每個模版目錄中,需要放置一個配置文件來控制代碼的生成。如下:
lang=go
genJson=1
lang?目前支持 go, c++ 和 objc。?genJson?可以為0或者1,如果是1則結(jié)構(gòu)會包含json的tag,此項配置目前僅支持Go語言。
Shell
Shell命令提供了一個操作數(shù)據(jù)庫的工具。例如,可以創(chuàng)建表、更改表、插入數(shù)據(jù)、刪除數(shù)據(jù)等。
xorm shell sqlite3 test.db
將連接到sqlite3數(shù)據(jù)庫,您可以鍵入help列出所有shell命令。
Dump
Dump命令提供了一個工具,可以將所有數(shù)據(jù)庫結(jié)構(gòu)和數(shù)據(jù)作為SQL轉(zhuǎn)儲到標(biāo)準(zhǔn)輸出。
xorm dump sqlite3 test.db
可以將sqlite3 database test.db轉(zhuǎn)儲到標(biāo)準(zhǔn)輸出。如果要保存到文件,只需鍵入
xorm dump sqlite3 test.db>test.sql。
Source
xorm source sqlite3 test.db<test.sql
將在test.db上執(zhí)行sql文件。
Driver
列出所有受支持的驅(qū)動程序,因為默認(rèn)生成將不包括sqlite3。