单表事务


  1. session := torm.GetDbInstance("default", "writer").GetSession() //指定实例
  2. defer session.Close() //需注意手动关闭session
  3. session.Begin() //开启事务
  4. dao := NewDbtestDao(session) //传入session
  5. param := torm.CastToParamIn([]int{1})
  6. data, err := dao.Get(param)
  7. row := data[0]
  8. row.Param1 = 100
  9. row.Param2 = "update1"
  10. dao.Update(&row)
  11. param = torm.CastToParamIn([]int{2})
  12. data, err = dao.Get(param)
  13. row = data[0]
  14. dao.Delete(&row)
  15. e := session.Commit() //不commit 会自动rollback
  16. if e != nil {
  17. session.Rollback()
  18. }