xorm根据数据库生成go model文件的操作

作者:zgcbj 时间:2024-01-16 00:29:22 

你懂的,手工翻译表定义到go结构体是很枯燥的。

so,用xorm搞定。

go get github.com/go-xorm/cmd/xorm

安装以下依赖,用到哪个装哪个。

github.com/go-xorm/xorm

驱动

Mysql: github.com/go-sql-driver/mysql

Postgres: github.com/lib/pq

SQLite: github.com/mattn/go-sqlite3

MSSQL: github.com/denisenkom/go-mssqldb

逆向生成

Reverse 命令可以转换数据库到所有支持的语言的数据结构,安装以后可以用 xorm help reverse查看帮助。

例子:


cd $GOPATH/src/github.com/go-xorm/cmd/xorm
sqlite: xorm reverse sqite3 test.db templates/goxorm
mysql: xorm reverse mysql root: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=test;user id=testid;password=testpwd;database=testdb" templates/goxorm

会在./model目录下生成go的文件

1、一定要在$GOPATH/src/github.com/go-xorm/cmd/xorm目录下运行,因为在这个目录下有templets,在解析数据库结构的时候有用。如果在别的目录下运行,会导致命令不报错,但是无法正常生成对应的结构文件。有空可以给github.com/go-xorm/cmd/xorm提个bug,加上错误提示。

2、执行xorm reverse mysql root:root@127.0.0.1:3306/testdb?charset=utf8 templates/goxorm报错2017/08/16 14:09:18 [Error] reverse.go:176 default addr for network '127.0.0.1:3306' unknown

解决办法:

xorm reverse mysql root:root@tcp(127.0.0.1:3306)/testdb?charset=utf8 templates/goxorm xorm reverse mysql root:root@tcp(127.0.0.1:3306)/testdb?charset=utf8 templates/goxorm

补充:「golang」xorm工具生成postgres的model

golang中的orm框架,一般使用xorm的xorm工具根据数据库表自动生成struct文件

xorm reverse postgres "dbname=queimsi sslmode=disable user=postgres password=123456 host=10.0.2.206 port=5432" /data/workspace/go/src/github.com/go-xorm/cmd/xorm/templates/goxorm

然后就会自动在当前目录下生成表的struct文件

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/lizao2/article/details/80272213

标签:xorm,数据库,go,model
0
投稿

猜你喜欢

  • 用FrontPage200八步快速建站

    2008-09-17 10:52:00
  • Golang并发编程之调度器初始化详解

    2023-07-13 08:47:11
  • python单测框架之pytest常见用法

    2021-05-22 04:13:02
  • C#使用ADO.Net连接数据库与DbProviderFactory实现多数据库访问

    2024-01-24 08:56:29
  • 如何使用python把ppt转换成pdf

    2022-09-20 14:27:14
  • Python3分析处理声音数据的例子

    2021-04-21 03:23:26
  • Vue中Axios的封装与接口管理详解

    2024-04-30 10:28:36
  • python源文件的字符编码知识点详解

    2021-04-30 08:05:04
  • 详解PHP的Sodium加密扩展函数

    2024-03-17 23:53:02
  • .NET Framework SQL Server 数据提供程序连接池

    2024-01-27 05:05:23
  • 基于一致性hash算法(consistent hashing)的使用详解

    2024-01-25 02:30:02
  • Python中字典创建、遍历、添加等实用操作技巧合集

    2021-04-02 08:22:12
  • SQL 多条件查询几种实现方法详细介绍

    2024-01-16 23:12:31
  • vuex 多模块时 模块内部的mutation和action的调用方式

    2024-04-09 10:47:11
  • c#将Excel数据导入到数据库的实现代码

    2024-01-25 21:53:43
  • 小结Python的反射机制

    2023-10-16 22:30:28
  • python人工智能tensorflow常用激活函数Activation Functions

    2023-08-01 12:45:02
  • 从零开始学YII2框架(五)快速生成代码工具 Gii 的使用

    2024-05-11 09:54:56
  • python调用新浪微博API项目实践

    2021-08-17 21:56:53
  • Python Selenium Cookie 绕过验证码实现登录示例代码

    2021-11-12 08:25:24
  • asp之家 网络编程 m.aspxhome.com