Go 語言的持久層框架:gorp

jopen 13年前發布 | 29K 次閱讀 持久層框架 Google Go/Golang開發

gorp 是一個Go 語言的持久層框架,當前支持MySQL、PostgresSQL和SQLite。但gorp提供一個方言接口,可以實現不同數據庫廠商自己的方言Dialect 。

功能特性:

  • Bind struct fields to table columns via API or tag
  • 支持事務
  • 從結構正向工程數據庫db schema(適合于單元測試)
  • Pre/post insert/update/delete hooks
  • 為結構struct自動生成insert/update/delete語句
  • Automatic binding of auto increment PKs back to struct after insert
  • Delete by primary key(s)
  • Select by primary key(s)
  • 可選SQL日記跟蹤
  • Bind arbitrary SQL queries to a struct
  • Optional optimistic locking using a version column (for update/deletes)
  • </ul>

    // connect to db using standard Go database/sql API
    // use whatever database/sql driver you wish
    db, err := sql.Open("mysql", "myuser:mypassword@localhost:3306/dbname")
    
    // construct a gorp DbMap
    dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
    
    // register the structs you wish to use with gorp
    // you can also use the shorter dbmap.AddTable() if you 
    // don't want to override the table name
    //
    // SetKeys(true) means we have a auto increment primary key, which
    // will get automatically bound to your struct post-insert
    //
    t1 := dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "Id")
    t2 := dbmap.AddTableWithName(Person{}, "person_test").SetKeys(true, "Id")

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

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