一、先安裝好需要的東西
xorm
go get github.com/go-xorm/cmd/xorm
安裝驅動版本,選擇自己需要用的
go get github.com/go-sql-driver/mysql //Mysqlgo get github.com/ziutek/mymysql/godrv //MyMysqlgo get github.com/lib/pq //Postgresgo get github.com/mattn/go-sqlite3 //SQLitego get github.com/denisenkom/go-mssqldb //MSSQL
二、配置%GOPATH%/Bin的環境變數
三、測試xorm cmd是否安裝成功
xorm help reverse
xorm reverse命令參數如下所示
D:\MyConfiguration\xxx>xorm help reverseusage: xorm reverse [-s] driverName datasourceName tmplPath [generatedPath] [tableFilterReg]according database's tables and columns to generate codes for Go, C++ and etc. -s Generated one go file for every table driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres datasourceName Database connection uri, for detail infomation please visit driver's project page tmplPath Template dir for generated. the default templates dir hasprovide 1 template generatedPath This parameter is optional, if blank, the default value is models, then will generated all codes in models dir tableFilterReg Table name filter regexp
注意的是下面指令中的templates/goxorm 指的是%GOPATH%/Src\github.com\go-xorm\cmd\xorm\templates\goxorm
如果是cd切換到了%GOPATH%/Src\github.com\go-xorm\cmd\xorm的目錄,就可以使用下面的指令,否則tmplPath請填寫完整路徑。
參考案例:MSSQL
sqlite: xorm reverse sqite3 test.db templates/goxormmysql: xorm reverse mysql root:@/xorm_test?charset=utf8 templates/goxormmymysql: xorm reverse mymysql xorm_test2/root/ templates/goxormpostgres: xorm reverse postgres "dbname=xorm_test sslmode=disable" templates/goxormmssql: xorm reverse mssql "server=127.0.0.1;user id=testid;password=testpwd;database=testdb" templates/goxorm
案例:MSSQL
方式一:建議指定generatedPath(如下例C:\temp) ,這樣好找產生後的代碼。
xorm reverse mssql "server=127.0.0.1;user id=sa;password=123456;database=shifenzheng" %GOPATH%\Src\github.com\go-xorm\cmd\xorm\templates\goxorm C:\temp
方式二:cd先切換到xorm/cmd/xorm檔案夾下再執行
cd %GOPATH%/Src\github.com\go-xorm\cmd\xormxorm reverse mssql "server=127.0.0.1;user id=sa;password=123456;database=shifenzheng" templates\goxorm C:\temp
產生後的代碼: