First of all, install the necessary things
Xorm
go get github.com/go-xorm/cmd/xorm
Install the driver version and choose the one you need
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
Second, the configuration%gopath%/bin environment variable
Third, test xorm cmd is installed successfully
xorm help reverse
The xorm reverse command parameters are as follows
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
Note that the Templates/goxorm in the following directive refers to the%gopath%/src\github.com\go-xorm\cmd\xorm\templates\goxorm
If the CD switch to the%gopath%/src\github.com\go-xorm\cmd\xorm directory, you can use the following instructions, otherwise tmplpath please fill in the full path.
Reference case: MSSQL
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
mssql: xorm reverse mssql "server=127.0.0.1;user id=testid;password=testpwd;database=testdb" templates/goxorm
Case: MSSQL
Method One: It is recommended to specify Generatedpath (example C:\temp below) so that the generated code is easy to find.
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
Mode two: CD first switch to Xorm/cmd/xorm folder and then execute
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
Post-generated code:
Golang xorm reverse automatically generate database entity files