golang xorm reverseデータベースエンティティファイルの自動生成
5754 ワード
一、必要なものを先に取り付ける
xormは公式ドキュメントreadmeを参照することもできる.md https://github.com/go-xorm/cmdおよびhttp://xorm.io/docs/
ドライババージョンをインストールし、必要なものを選択します.
二、xorm cmdのインストールに成功したかどうかをテストする
xorm reverseコマンドパラメータは次のとおりです.
なお、以下のコマンドのtemplates/goxormは%GOPATH%/srcgithubを指す.comgo-xormcmdxormtemplatesgoxorm cdであれば%GOPATH%/srcgithubに切り替わる.comgo-xormcmdxormのディレクトリでは、次のコマンドを使用できます.そうしないと、tmplPathはフルパスを記入してください.
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 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:\tempp
ps:command最後のC:tempはgeneratedPath(コード生成のディレクトリ)を指定して生成されたコードを見つけやすくします.このパラメータは必須ではありません.プロジェクトにbatバッチファイルを作成して直接エンティティコードを生成できます.参考例:MSSQL:方法1:tmplPath(コード生成のテンプレートファイル)絶対パスを指定します.
方式2:cdはxorm/cmd/xormフォルダに切り替えてから実行する
図:Unknown colType:USER-DEFINEDを生成した場合、テーブルにカスタムデータ型が含まれているため、xorm公式はまだサポートされていません(ltree、postgisなど)
xormは公式ドキュメントreadmeを参照することもできる.md https://github.com/go-xorm/cmdおよびhttp://xorm.io/docs/
go get github.com/go-xorm/cmd/xorm
ドライババージョンをインストールし、必要なものを選択します.
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 cmdのインストールに成功したかどうかをテストする
xorm help reverse
xorm reverseコマンドパラメータは次のとおりです.
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%/srcgithubを指す.comgo-xormcmdxormtemplatesgoxorm cdであれば%GOPATH%/srcgithubに切り替わる.comgo-xormcmdxormのディレクトリでは、次のコマンドを使用できます.そうしないと、tmplPathはフルパスを記入してください.
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 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:\tempp
ps:command最後のC:tempはgeneratedPath(コード生成のディレクトリ)を指定して生成されたコードを見つけやすくします.このパラメータは必須ではありません.プロジェクトにbatバッチファイルを作成して直接エンティティコードを生成できます.参考例:MSSQL:方法1:tmplPath(コード生成のテンプレートファイル)絶対パスを指定します.
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
方式2: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
図:Unknown colType:USER-DEFINEDを生成した場合、テーブルにカスタムデータ型が含まれているため、xorm公式はまだサポートされていません(ltree、postgisなど)