<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          發(fā)現一個很奇怪的現象,mybaits的insert方法一直返回"-2147482646"

          共 2533字,需瀏覽 6分鐘

           ·

          2021-03-25 12:13

          來源 | https://www.cnblogs.com/wyq178/p/8652443.html

          前幾天在做項目demo的時候,發(fā)現有一個很奇怪的現象,就是MyBatis發(fā)現更新和插入返回值一直為"-2147482646",無論怎么改,這個值一直不變...

          實在摸不著頭腦,百度和谷歌了一下,有這樣的說法原來:是由defaultExecutorType設置引起的,如果設置為BATCH,更新返回值就會丟失。

          If the BATCH executor is in use, the update counts are being lost.

          操作

          也就是說在spring的配置文件中,只要把constructor-arg中的executorType改為SIMPLE(以前的值是BATCH就會引發(fā)這個問題)或者直接注釋掉就可以了

          <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
              <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
              <!-- 執(zhí)行類型 -->
              <constructor-arg name="executorType" value="SIMPLE"></constructor-arg>
          </bean>

          這么一改果然好使,但是為啥會返回這個奇怪的數字呢?背后有啥原因?

          本篇就來研究一下為什么會返回這個數字,打開我們的idea,又到了跟蹤源碼的時候了,走起。

          1、打開配置文件中的變量,看到SqlSessionTemplate這個類有個構造參數類型ExecutorType了,也就是我們配置的值,當它為BATCH的時候又會怎么樣呢?

          2、ExecutorType的參數,打開這個類發(fā)現它是個枚舉類型,也就是說它僅僅只有3種配置的值,SIMPLE、RESUME、BATCH,在配置文件中我們也只能配這三種其中的一個

          3、程序執(zhí)行,可見最終操作的是SqlSession的insert方法,我們就來打開這個方法的源碼:

          Reader config = Resources.getResourceAsReader("conf.xml"); 
          SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(config); 
          SqlSession sqlSession = sessionFactory.openSession(); 
          String sql = "com.mlxs.mybatis.test.userMapper.insertUser";

          int res= sqlSession.insert(sql, "1");//  

          4、SqlSession.insert()方法:

          public interface SqlSession extends Closeable {

            int insert(String var1);

            int insert(String var1, Object var2);

          }

          SqlSession是一個接口,里面有insert方法,我們再來看一下這個接口的實現類:

          一般在源碼中,如果沒有特殊配置肯定是采用的Default的設置,我們就來看看DefaultSqlSession的源碼:

          再找this.insert方法:

          可以看到,它調用的是this.update方法,再來找一個update方法:

          看到本質上它調用的是executor的update方法,我們再點進去看update方法:

          可以看到它是一個接口,那么肯定要找實現類了:

          這里就可以看出端倪了,如果沒有配置緩存的話,肯定用的是BaseExecutor,那么我們在配置文件中配置的是Batch,毋庸置疑它肯定是選擇BatchExecutor這個類中的update方法了:

          千呼萬喚始出來,這個奇怪的數字原來在這里出來的,其中可以看到它是固定返回的,沒有任何判斷邏輯...這樣就證明了一點,只要你的insert方法在配置文件中配置的是batch,那么它肯定返回這個值!

          5、當配置為simple返回的是什么?

          根據上面的經驗,這里我們直接看SimpleExecutor方法就行了:

          接著再看StatementHandler就知道了:

          我找到了SimpleStatementHandler的update方法,可以看到它返回是statement.getUpdateCount返回的數字,也就是實際影響的行數,那么就是正常的數值。

          總結

          本篇博客記錄了一次源碼追蹤的過程,從而解釋了為什么配置BATCH返回的是這個數字的原因,在實際的編程中,可能會遇到很多奇怪的問題,這時候就要敢于翻源碼,答案一定在源碼中,才能從根本上知道產生問題的原因。

          后臺回復 學習資料 領取學習視頻


          如有收獲,點個在看,誠摯感謝

          瀏覽 55
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产女人18毛片水真多18 | 资优生的性爱大对决 | 丁香五月婷婷情 | 91粉嫩| 九九九精品影视 |