欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Mybatis配置insert時,插入數據失敗

錯誤代碼如下:

繁昌網站建設公司創(chuàng)新互聯(lián),繁昌網站設計制作,有大型網站制作公司豐富經驗。已為繁昌近千家提供企業(yè)網站建設服務。企業(yè)網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的繁昌做網站的公司定做!

@Test
    public void testInsertOne(){
        SqlSession sqlSession = MyBatisUtils.getSession();
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname("sunny");
        userInfo.setPhoneNum("18936896033");
        sqlSession.insert("insertUser", userInfo);
        LOG.log(Level.INFO, "userId:"+userInfo.getId());
        sqlSession.close();
    }

原因是會話沒有被提交而是被回滾了,修改代碼如下:

@Test
    public void testInsertOne(){
        SqlSession sqlSession = MyBatisUtils.getSession();
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname("sunny");
        userInfo.setPhoneNum("18936896033");
        sqlSession.insert("insertUser", userInfo);
        sqlSession.commit(); //注意提交事物
        LOG.log(Level.INFO, "userId:"+userInfo.getId());
        sqlSession.close();
    }

源碼解讀:首先看看openSession的幾種方式:

SqlSession openSession()
SqlSession openSession(boolean autoCommit)
SqlSession openSession(Connection connection)
SqlSession openSession(TransactionIsolationLevel level)
SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level)
SqlSession openSession(ExecutorType execType)
SqlSession openSession(ExecutorType execType, boolean autoCommit)
SqlSession openSession(ExecutorType execType, Connection connection)

從地一個和第二個就可看出區(qū)別:

openSession()會創(chuàng)建一個事物,但是不會自動提交

openSession(true)會創(chuàng)建一個事物,并自動提交

openSession(Connection connection),不使用數據元配置,而是自定義的一個鏈接

openSession(TransactionIsolationLevel level)事物的隔離級別:

(NONE,READ_UNCOMMITTED,READ_COMMITTED,REPEA TABLE_READ,SERIALIZA BLE)

openSession(ExecutorType execType):

  • ExecutorType.SIMPLE: 這個執(zhí)行器類型不做特殊的事情。它為每個語句的執(zhí)行創(chuàng)建一個新的預處理語句。

  • ExecutorType.REUSE: 這個執(zhí)行器類型會復用預處理語句。

  • ExecutorType.BATCH: 這個執(zhí)行器會批量執(zhí)行所有更新語句,如果 SELECT 在它們中間執(zhí)行還會標定它們是 必須的,來保證一個簡單并易于理解的行為。

當前名稱:Mybatis配置insert時,插入數據失敗
網站URL:http://chinadenli.net/article2/jigdoc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設定制網站做網站手機網站建設建站公司網站維護

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網站制作