<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                補充:xorm高級部分 === ### 事務及回滾 - 創建session對象 `sess := Engine.NewSession()` - 啟動事任 `sess.Begin()` - 通過session操作數據庫 `sess.Update()` - 發生錯誤進行回滾 `sess.RollBack()` - 提交事務 `sess.Commit()` 為什么要用到事務?保證數據的一致性 原子性 隔離性 持久性 我們就吧那個轉賬模塊修改一下吧 ``` func TransferAccounts(form,to string,balance float64) error { session := Engine.NewSession() // 獲得session defer session.Close() // session會消耗資源 所以要關閉 err := session.Begin() //開啟事務 if err != nil { return errors.New("session begin error") } account := &Account{Name: form} _, e := Engine.Get(account) if e != nil { return e } if account.Balance > balance{ account.Balance -= balance } account2 := &Account{Name:to} b, e := Engine.Get(account2) if b != true || e!= nil{ return errors.New("Error") } account2.Balance += balance _, e = Engine.Where("name = ?",account.Name).Update(account) if e!=nil{ session.Rollback() //回滾 return e } _, e = Engine.Where("name = ?",account2.Name).Update(account2) if e!=nil{ session.Rollback() //回滾 } return session.Commit() //提交 } ``` ### 這節課程的大塊頭講完了,就是小部分了 #### 統計 - 統計某個表中服務條件的所有記錄條數 `Engine.Count(new(Account))` - 配合鏈式操作 `Engine.Where("id > 10").Count(new(Account))` #### 迭代查詢 - 迭代查詢某個表中符合條件的所有記錄 ``` Engine.Iterate(new(Account),func(idx int,bean interface{}) { fmt.Printf("%d,%#v\n",idx,bean.(*Account)) }) ``` - 使用Rows對象 `rows,err := Engine.Rows(new(Account))` ``` func row() { rows, _ := Engine.Rows(new(Account)) defer rows.Close() account := new(Account) for rows.Next() { rows.Scan(account) } } ``` #### 常用查詢方法 - 當只有結構中的某個字段的值對你有價值時: `Engine.Clos("name").Get(..)` - 忽略指定字段 `Engine.Omit("name").Find(...)` - 分頁 `Engine.Limit(3,2).Find(...)` #### 日志記錄 - 開啟日志 `Engine.ShowSQL = true` - 將日志保存到文件 ``` f,_ := os.Create("sql.log") Engine.Logger = xorm.NewSimpleLogger(f) ``` #### LRU緩存 - 開啟默認LRU緩存 ``` cacher := xormNewLRUCacher(xorm.NewMemoryStore(),1000) Engine.SetDefaultCacher(cacher) ``` #### 事件鉤子 這個去看官方文檔吧.... 本次課程代碼: [https://github.com/dollarkillerx/GolangWebCourseware/tree/xorm](https://github.com/dollarkillerx/GolangWebCourseware/tree/xorm)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看