mybatis批量更新问题:在解决问题中不断突破自己

记一次mybatis批量更新bug修复过程

版本一

<update id="updateIsDelStatus" parameterType="java.util.List">
  <foreach collection="list" item="item" index="index" separator=";" close="" open="">
    update inp_record set IR_IsDel = -1
    where IR_ID = #{item,jdbcType=INTEGER}
  </foreach>
</update>

版本一在测试时报错,由于在和前端联调中,考虑到单词入参list的size并不是很大(使用in 建议控制在 size <= 10),临时采用版本二进行紧急修复

版本二

<update id="updateIsDelStatus" parameterType="java.util.List">
    update inp_record set IR_IsDel = -1
    where IR_ID in
  <foreach collection="list" item="item" index="index" separator="," close=")" open="(">
    #{item,jdbcType=INTEGER}
  </foreach>
</update>

问题已得到基本解决,但是感觉稀里糊涂的处理事情并不能解决问题,于是进行了深入研究得出Mybatis默认是只执行一条sql的,因此版本一的写法是执行了多条sql,因此会在第一句sql时就报错。

解决办法:

在jdbcurl路径中添加:allowMultiQueries=true

但是还是不建议使用多条sql同时执行,尽量根据实际业务场景来使用单条sql来实现业务逻辑。

猜你喜欢

转载自blog.csdn.net/A___B___C/article/details/95452299