一、解決方案:
產生此現象的原因是因爲mybatis配置文件中的這樣一個配置
<!-- 配置一個可以執行批量的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
其中,可以看到executorType配置的值是BATCH,改爲SIMPLE即可正確返回數據庫受影響的行數
二、貿然在項目中修改會不會產生問題
- Mybatis內置的ExecutorType有3種,默認的是simple,該模式下它爲每個語句的執行創建一個新的預處理語句,單條提交sql;而batch模式重複使用已經預處理的語句
- 在測試中使用simple模式提交10000條數據,時間爲18248 毫秒,batch模式爲5023 ,性能提高70%