mysql批量更新 bad SQL grammar [] 错误

本文探讨了在使用MyBatis进行批量更新操作时遇到的MySQL语法错误问题,详细解释了允许多SQL查询的功能`allowMultiQueries=true`的重要性,并提供了相应的解决方案。文章深入分析了错误产生的原因及解决步骤,帮助开发者避免类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误代码如下:

### The error may involve org.wz.dao.LiaoningComInfoTyMapper.updateBatchSyncPhoneAndEmail-Inline
### The error occurred while setting parameters
### SQL: update liaoning_com_info_ty          SET com_phone = ?          where com_name = ?       ;       update liaoning_com_info_ty          SET com_phone = ?,                              com_email = ?          where com_name = ?
### 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 'update liaoning_com_info_ty
         SET com_phone = '13999620408,13565741070,13' at line 5
; bad SQL grammar []; nested exception is 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 'update liaoning_com_info_ty
         SET com_phone = '13999620408,13565741070,13' at line 5

代码如下:

 <!-- 批量同步手机号码 和邮箱账号 -->
  <update id="updateBatchSyncPhoneAndEmail" parameterType="java.util.List">
     <foreach collection="list" item="item" index="index" open="" close="" separator=";">
    	update liaoning_com_info_ty
        <set> 
	        <if test="item.comPhone != null and item.comPhone !='' " >
        	com_phone = #{item.comPhone},
	        </if>
	        <if test="item.comEmail != null and item.comEmail != '' " >
        	com_email = #{item.comEmail}
        	</if>
        </set>
        where com_name = #{item.comName}
     </foreach>
  </update>

只有一条的时候可以执行,两条以上就会报错。

纠结半天,么有发现问题。最后终于明白了,allowMultiQueries=true  需要这个参数才能执行多条sql,默认是一条。

jdbcUrl=jdbc:mysql://192.168.1.203:3306/shiro?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值