mybatis批量update的时候失败,解决办法

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 工作小总结
  • 时间:2019-01-17 10:29
  • 3972人已阅读
简介 mybatis批量更新:形式一:更新的时候带有判断条件的:<!-- 添加数据权限后,数据补充。批量更新入库 --><update id="batechUpdateInfoForOaAuth" parameterType="list">     &

🔔🔔好消息!好消息!🔔🔔

 如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号代升级ChatGPT4

有需要的朋友👉:微信号 kaigejava2022

mybatis批量更新:

形式一:

更新的时候带有判断条件的:

<!-- 添加数据权限后,数据补充。批量更新入库 -->
<update id="batechUpdateInfoForOaAuth" parameterType="list">
      <foreach collection="aList"   index="index" item="item" open="" close="" separator=";">
          update a表
          <set>
              <if test="item.deptCode !=null">
                  dept_code = #{item.deptCode,jdbcType=VARCHAR},
              </if>

              <if test="item.deptName !=null">
                  dept_name = #{item.deptName,jdbcType=VARCHAR},
              </if>


              <if test="item.productLineCode !=null">
                  product_line_code = #{item.productLineCode,jdbcType=VARCHAR},
              </if>

              <if test="item.productLineName !=null">
                  product_line_name = #{item.productLineName,jdbcType=VARCHAR},
              </if>

              <if test="item.subjectCode !=null">
                  subject_code = #{item.subjectCode,jdbcType=VARCHAR},
              </if>

              <if test="item.subjectName !=null">
                  subject_name = #{item.subjectName,jdbcType=VARCHAR},
              </if>

              <if test="item.jobDeptCode !=null">
                  job_dept_code = #{item.jobDeptCode,jdbcType=VARCHAR},
              </if>

              <if test="item.jobDeptName !=null">
                  job_dept_name = #{item.jobDeptName,jdbcType=VARCHAR},
              </if>

              <if test="item.jobManagerName !=null">
                  job_manager_name = #{item.jobManagerName,jdbcType=VARCHAR},
              </if>

              <if test="item.financeType !=null">
                  finance_type = #{item.financeType},
              </if>
          </set>
          where id = #{item.id}
      </foreach>
</update>

25b30813a065c18391f6c6e65938f4d0.png


形式二:直接更新

<!--添加数据权限后,结算数据补充。批量更新入库-->
<update id="batechUpdateSalarWayInfoForOaAuth" parameterType="java.util.List">
    <foreach collection="bWayList" item="item" index="index"  open="" close="" separator=";">
        update b表
        <set>
            position_type = ${item.positionType}
        </set>
          where id = ${item.id}
    </foreach>
</update>

2500e7a3868924c788da19c2308acda2.png

如果直接这样写,有可能会出现问题的。因为:

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true" />  

数据库连接需要添加:allowMultiQueries=true

有可能是下面这种情况:

7ec7733e33d68a6656179b42cedd2e66.png

TopTop