Go 语言的持久层框架:gorp

jopen 12年前

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)

// 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.open-open.com/lib/view/home/1346943904537