Golang Xorm reverse automatically generate database entity files

Source: Internet
Author: User
Tags sqlite


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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.