MyBatis批量更新(update foreach)报错问题
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
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot使用MockMvc测试get和post接口的示例代码
Spring Boot MockMvc是一个用于单元测试的模块,它是Spring框架的一部分,专注于简化Web应用程序的测试,MockMvc主要用来模拟一个完整的HTTP请求-响应生命周期,本文给大家介绍了SpringBoot使用MockMvc测试get和post接口,需要的朋友可以参考下2024-06-06
Java/Android 获取网络重定向文件的真实URL的示例代码
本篇文章主要介绍了Java/Android 获取网络重定向文件的真实URL的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-11-11
mybatis模糊查询之bind标签和concat函数用法详解
大家都知道bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中,接下来通过本文给大家介绍了mybatis模糊查询——bind标签和concat函数用法,需要的朋友可以参考下2022-08-08
Spring中的ImportBeanDefinitionRegistrar接口详解
这篇文章主要介绍了Spring中的ImportBeanDefinitionRegistrar接口详解,ImportBeanDefinitionRegistrar接口是也是spring的扩展点之一,它可以支持我们自己写的代码封装成BeanDefinition对象,注册到Spring容器中,功能类似于注解@Service @Component,需要的朋友可以参考下2023-09-09


最新评论