TORM-数据库支持


TORM支持多种主流数据库,提供统一的API接口,让您可以轻松在不同数据库间切换。

🗄️ 支持的数据库

数据库 驱动 状态 版本要求
MySQL mysql ✅ 完全支持 5.7+ / 8.0+
PostgreSQL postgres ✅ 完全支持 11+
SQLite sqlite ✅ 完全支持 3.8+
MongoDB mongodb ✅ 完全支持 4.4+
SQL Server sqlserver 🚧 基础支持 2017+

🔧 MySQL

配置示例

go 复制代码
config := &db.Config{
    Driver:   "mysql",
    Host:     "localhost",
    Port:     3306,
    Database: "myapp",
    Username: "root",
    Password: "password",
    Charset:  "utf8mb4",
    
    Options: map[string]string{
        "parseTime": "true",
        "loc":       "Local",
    },
}

特性支持

  • ✅ 完整的SQL支持
  • ✅ 事务处理
  • ✅ 连接池
  • ✅ JSON字段
  • ✅ 全文索引
  • ✅ 外键约束

🐘 PostgreSQL

配置示例

go 复制代码
config := &db.Config{
    Driver:   "postgres",
    Host:     "localhost",
    Port:     5432,
    Database: "myapp",
    Username: "postgres",
    Password: "password",
    SSLMode:  "disable",
}

特性支持

  • ✅ 完整的SQL支持
  • ✅ JSONB支持
  • ✅ 数组类型
  • ✅ 高级索引
  • ✅ 窗口函数

📁 SQLite

配置示例

go 复制代码
config := &db.Config{
    Driver:   "sqlite",
    Database: "app.db",
    
    Options: map[string]string{
        "foreign_keys": "on",
    },
}

特性支持

  • ✅ 轻量级部署
  • ✅ 零配置
  • ✅ 嵌入式应用
  • ⚠️ 并发限制

🍃 MongoDB

配置示例

go 复制代码
config := &db.Config{
    Driver:   "mongodb",
    Host:     "localhost",
    Port:     27017,
    Database: "myapp",
}

特性支持

  • ✅ 文档存储
  • ✅ 聚合管道
  • ✅ 索引支持
  • ✅ 事务支持(副本集)

🔄 数据库切换

TORM的设计允许您轻松在不同数据库间切换:

go 复制代码
// 开发环境 - SQLite
devConfig := &db.Config{
    Driver:   "sqlite",
    Database: "dev.db",
}

// 生产环境 - MySQL
prodConfig := &db.Config{
    Driver:   "mysql",
    Host:     "prod-db.example.com",
    Database: "myapp",
    Username: "app_user",
    Password: "secure_password",
}
相关推荐
岁忧7 小时前
(LeetCode 面试经典 150 题) 104. 二叉树的最大深度 (深度优先搜索dfs)
java·c++·leetcode·面试·go·深度优先
Code季风15 小时前
GORM 一对多关联(Has Many)详解:从基础概念到实战应用
数据库·go·orm
DemonAvenger15 小时前
边缘计算场景下Go网络编程:优势、实践与踩坑经验
网络协议·架构·go
程序员爱钓鱼16 小时前
Go语言实战案例:使用模板渲染HTML页面
后端·google·go
程序员爱钓鱼16 小时前
Go语言实战案例:构建简单留言板(使用文件存储)
后端·google·go
用户89535603282201 天前
Gin 框架核心架构解析
后端·go
Code季风1 天前
深入理解 Gin 框架的路由机制:从基础使用到核心原理
ide·后端·macos·go·web·xcode·gin
程序员爱钓鱼2 天前
Go语言实战案例:Cookie与Session基础
前端·后端·go
程序员爱钓鱼2 天前
Go语言实战案例:用户注册与登录(无数据库)
后端·google·go