Go语言的ORM框架:gorm
jopen
11年前
程序员友好的GoLang ORM, 具有高易用性。
支持 CURD, 链式查询, 内嵌struct, 各种回调 callback 支持
支持Rails类似的 Update, Updates, FirstOrInit, FirstOrCreate 等功能
并且有自动的 CreatedAt, UpdatedAt, 软删除等等功能
- CURD
- Chainable API
- Embedded structs support
- Before/After Create/Save/Update/Delete Callbacks
- Update, Updates Like Rails's update_attribute, update_attributes
- FirstOrInit, FirstOrCreate Like Rails's first_or_initialize, first_or_create
- Order/Select/Limit/Offset Support
- Automatically CreatedAt, UpdatedAt
- Soft Delete
- Create/Drop table from struct
- Dynamically set table name when search, create, update, delete...
- Prevent SQL Injection
- Goroutines friendly
- Database Pool
- Convention Over Configuration
import "github.com/jinzhu/gorm" import _ "github.com/lib/pq" // import _ "github.com/go-sql-driver/mysql" // import _ "github.com/mattn/go-sqlite3" db, err := Open("postgres", "user=gorm dbname=gorm sslmode=disable") // db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") // db, err = Open("sqlite3", "/tmp/gorm.db") // Set the maximum idle database connections db.SetPool(100) // Gorm is goroutines friendly, so you can create a global variable to keep the connection and use it everywhere like this var DB gorm.DB func init() { DB, err = gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable") if err != nil { panic(fmt.Sprintf("Got error when connect database, the error is '%v'", err)) } }