当使用事务处理时,需要创建 Session 对象。在进行事务处理时,可以混用 ORM 方法和 RAW 方法,如下代码所示:
func MyTransactionOps() error { session := engine.NewSession() defer session.Close() // add Begin() before any action if err := session.Begin(); err != nil { return err } user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()} if _, err := session.Insert(&user1); err != nil { return err } user2 := Userinfo{Username: "yyy"} if _, err = session.Where("id = ?", 2).Update(&user2); err != nil { return err } if _, err = session.Exec("delete from userinfo where username = ?", user2.Username); err != nil { return err } // add Commit() after all actions return session.Commit() }
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8