golang xorm reverse 自动生成数据库实体文件

一、先安装好需要的东西

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%/Src\github.com\go-xorm\cmd\xorm\templates\goxorm如果是cd切换到了%GOPATH%/Src\github.com\go-xorm\cmd\xorm的目录,就可以使用下面的指令,否则tmplPath请填写完整路径。

databasecommand
sqlitexorm reverse sqite3 test.db templates/goxorm C:\temp
mysqlxorm reverse mysql “root:123456@(127.0.0.1:3306)/test?charset=utf8” templates/goxorm C:\temp
mymysqlxorm reverse postgres “user=postgres password=123456 dbname=test host=127.0.0.1 port=5432 sslmode=disable” templates/goxorm C:\temp
mssqlxorm 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:
方式一:指定 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

方式二: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等)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值