一、先安装好需要的东西

xorm 也可以参考官方文档 readme.md https://github.com/go-xorm/cmdhttp://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

二、配置%GOPATH%/Bin的环境变量

三、测试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请填写完整路径。

参考案例:MSSQL

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批处理文件直接生成实体代码。

案例: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

如图:



生成后的代码:

案例:POSTGRESQL

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还是挺多的

golang xorm reverse 自动生成数据库实体文件的更多相关文章

  1. Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类

    Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类 转载至:https://blog.csdn.net/fishinhouse/article/details ...

  2. 转 Django根据现有数据库,自动生成models模型文件

    Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...

  3. python学习-- Django根据现有数据库,自动生成models模型文件

    Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...

  4. MVC自动生成数据库【Code-FIrst方式】

    一般我们写好实体之后,配置好数据上下文对象,还有在配置文件中改好连接字符串之后. 还不能生成数据库,自动生成数据库,有两步关键步骤:   1.   Enable Migrations   2. Upd ...

  5. 基于MVC4+EasyUI的Web开发框架经验总结(14)--自动生成图标样式文件和图标的选择操作

    在很多Web系统中,一般都可能提供一些图标的选择,方便配置按钮,菜单等界面元素的图标,从而是Web系统界面看起来更加美观和协调.但是在系统中一般内置的图标样式相对比较有限,而且硬编码写到样式表里面,这 ...

  6. T4教程2 T4模版引擎之生成数据库实体类

    T4模版引擎之生成数据库实体类   在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇.T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗).而这其中 ...

  7. [转]T4模版引擎之生成数据库实体类

    本文转自:http://www.cnblogs.com/lzrabbit/archive/2012/07/18/2597953.html 在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实 ...

  8. T4模版引擎之生成数据库实体类

    在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇.T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗).而这其中我们使用的最普遍的是根据数据库生成实 ...

  9. 基于数据库的自动化生成工具,自动生成JavaBean、自动生成数据库文档等(v4.1.2版)

            目录:            第1版:http://blog.csdn.net/vipbooks/article/details/51912143            第2版:htt ...

随机推荐

  1. [译]The Python Tutorial#10. Brief Tour of the Standard Library

    [译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...

  2. git初次建立远程仓库问题

    git "Could not read from remote repository.Please make sure you have the correct access rights. ...

  3. 大数模板Java

    import java.util.*; import java.math.BigInteger; public class Main{ public static void main(String a ...

  4. 菜鸟学Linux - Hard Link与Symbolic Link

    在学习Hard Link与Symbolic Link之前,需要大概了解一下inode与data block.在Linux的文件系统中,一个文件对应一个inode与若干个data block.inode ...

  5. HashMap的实现原理和底层数据结构

    看了下Java里面有HashMap.Hashtable.HashSet三种hash集合的实现源码,这里总结下,理解错误的地方还望指正 HashMap和Hashtable的区别 HashSet和Hash ...

  6. 创建OpenStack的存储云

    OPENSTACK内部 OpenStack是一个开源的云平台项目,是由NASA发起,Rackspace在2010作为一个项目进行主导.源代码是由OpenStack基金会管理并在准许Apache许可下发 ...

  7. leetcode 【 Container With Most Water 】python 实现

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  8. leetcode 【 Merge Sorted Array 】python 实现

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  9. csu-2018年11月月赛Round2-div1题解

    csu-2018年11月月赛Round2-div1题解 A(2191):Wells的积木游戏 Description Wells有一堆N个积木,标号1~N,每个标号只出现一次 由于Wells是手残党, ...

  10. vue tradingView(二)

    tradingView 一些配置问题 tradingView 一些配置问题 javascript Demo_Hu 4月17日提问 · 4月17日更新 9 关注 1 收藏,993 浏览 问题对人有帮助, ...