开发者

MyBatis批量更新(update foreach)报错问题

目录
  • MyBATis批量更新报错解决
  • 解决方案
  • 总结

MyBatis批量更新报错解决

在使用mybatis执行批量更新(update foreach)数据的时候

报错如下:

org.springframework.jdbc.BadSqlGrammarException:

### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '......

### The error may involve .... 

### The error occurred while setting parameters ### SQL:

刚开始一直以为是自己SQL写错了,但是检查N遍,发ocUAc现也没问题,而且到Navicat中执行也没说SQL报错,

批量更新SQL如下:

 <!--批量更新报表 -->
 <update id="updateBatphpchUser" parameterType="Java.util.List"&pythongt;
  <foreach collection="userList" item="item" index="index" separator=";">
   update sys_user
   <set>
    <if test="item.userName != null and item.userName!= ''">user_name = #{item.userName}, </if>
    <if test="item.userNo != null">user_no = #{item.userNo }, </if>
    ...
    updated_time = now()
   </set>
   where id = #{item.id} 
  </foreach>
 </update>

解决方案

MySQL的批量更新是要我们主动去设置的

使用mybatis进行批量插入与更新时

必须在配置连接url时加上 &allowMultiQueries=true 即可

MyBatis批量更新(update foreach)报错问题

jdbc:mysql://xx:3306/test?serverTimezone=Asia/Shangphphai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true&allowMultiQueries=true

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多编程客栈支持编程客栈(www.devze.com)。

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜