xorm 也能夠參考官方文檔 readme.md https://github.com/go-xorm/cmd 和 http://xorm.io/docs/mysql
go get github.com/go-xorm/cmd/xorm
安裝驅動版本,選擇本身須要用的git
go get github.com/go-sql-driver/mysql //Mysql go get github.com/ziutek/mymysql/godrv //MyMysql go get github.com/lib/pq //Postgres go get github.com/mattn/go-sqlite3 //SQLite go get github.com/denisenkom/go-mssqldb //MSSQL
xorm help reverse
xorm reverse命令參數以下所示github
D:\MyConfiguration\xxx>xorm help reverse usage: 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請填寫完整路徑。
參考案例:MSSQLsql
database | command |
---|---|
sqlite | xorm reverse sqite3 test.db templates/goxorm C:\temp |
mysql | xorm reverse mysql "root:123456@(127.0.0.1:3306)/test?charset=utf8" templates/goxorm C:\temp |
mymysql | xorm reverse mymysql xorm_test2/root/ templates/goxorm C:\temp |
postgres | xorm reverse postgres "user=postgres password=123456 dbname=test host=127.0.0.1 port=5432 sslmode=disable" templates/goxorm C:\temp |
mssql | xorm reverse mssql "server=127.0.0.1;user id=testid;password=testpwd;database=testdb" templates/goxorm C:\temp |
ps:command 最後的 C:\temp 是指定 generatedPath(代碼生成的目錄)方便找到生成的代碼,此參數非必填。
能夠在項目裏建立一個bat批處理文件直接生成實體代碼。ide
案例:MSSQL
方式一:指定 tmplPath(生成代碼的模板文件)絕對路徑post
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\xorm xorm reverse mssql "server=127.0.0.1;user id=sa;password=123456;database=shifenzheng" templates\goxorm C:\temp
如圖:
生成後的代碼:
code
案例:POSTGRESQLregexp
cd %GOPATH%/Src\github.com\go-xorm\cmd\xorm xorm reverse postgres "user=postgres password=123456 dbname=test host=127.0.0.1 port=5432 sslmode=disable" templates\goxorm C:\temp
若是生成報錯 Unknown colType: USER-DEFINED ,是由於表中有列包含了自定義數據類型,xorm官方暫時還不支持(如 ltree、postgis等)
https://github.com/go-xorm/xorm/issues/667 相似的issues仍是挺多的orm