Go語言的ORM框架:gorm

jopen 11年前發布 | 136K 次閱讀 gorm Google Go/Golang開發

程序員友好的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))
    }
}

項目主頁:http://www.baiduhome.net/lib/view/home/1383613319149

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!