当前位置: 代码网 > it编程>编程语言>Java > MyBatis批量更新(update foreach)报错问题

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

2024年08月19日 Java 我要评论
mybatis批量更新报错解决在使用mybatis执行批量更新(update foreach)数据的时候报错如下:org.springframework.jdbc.badsqlgrammarexcep

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遍,发现也没问题,而且到navicat中执行也没说sql报错,

批量更新sql如下:

 <!--批量更新报表 -->
 <update id="updatebatchuser" parametertype="java.util.list">
  <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 即可

jdbc:mysql://xx:3306/test?servertimezone=asia/shanghai&characterencoding=utf8&usessl=false&rewritebatchedstatements=true&allowmultiqueries=true

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com