`
zhaojian0910
  • 浏览: 47164 次
社区版块
存档分类
最新评论

Mybatis 批量更新 set 多个字段你的时候报错 mysql

阅读更多

日志如下:

2015-10-15 10:18:00,070 DEBUG  - JDBC Connection [jdbc:mysql://localhost:3306/section?useUnicode=true&characterEncoding=UTF-8, UserName=root@localhost, MySQL-AB JDBC Driver] will not be managed by Spring

2015-10-15 10:18:00,070 DEBUG  - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@76a2f910] was not registered for synchronization because synchronization is not active

2015-10-15 10:18:00,073 DEBUG  - ==>  Executing: update b_email_msg_remind SET send_status = ?, send_email_code='abc@abc.abc' where 1 = 1 AND email_remind_id = ? and send_status = 0 ; update b_email_msg_remind SET send_status = ?, send_email_code='abc@abc.abc' where 1 = 1 AND email_remind_id = ? and send_status = 0 

2015-10-15 10:18:00,073 DEBUG  - ==> Parameters: 1(Integer), 234746e8-4cab-444c-86ee-ea73c57cb7de(String), 1(Integer), 48d4a578-141e-421c-9eed-0c26de4b8f48(String)

2015-10-15 10:18:00,075 DEBUG  - Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@76a2f910]

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 'update b_email_msg_remind 

SET send_status = 1, send_email_code='abc@abc.abc'' at line 6

### The error may involve com.hhsoft.sectionservice.model.persistence.EmailMapper.updateEmailTasks-Inline

### The error occurred while setting parameters

### 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 b_email_msg_remind 

SET send_status = 1, send_email_code='abc@abc.abc'' at line 6

; 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 b_email_msg_remind 

SET send_status = 1, send_email_code='abc@abc.abc'' at line 6

 

Mapper.xml配置

<update id="updateEmailTasks" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" separator=";" >  
			update b_email_msg_remind 
			SET send_status = #{item.sendStatus}, send_email_code='abc@abc.abc'
			  where email_remind_id = #{item.emailRemindId}  and send_status = 0
	    </foreach> 
	</update>

 

 

原因分析:

目前定位如果SET 只修改一个字段,则正常执行,或者SET 两个字段但是foreach只循环一次,也可以正常执行

 

由于时间较紧,暂时没有解决此问题,而是找了替代方法,并且效率更高

 

 

<update id="updateEmailTasks" parameterType="java.util.List">
			update b_email_msg_remind 
			<trim prefix="set" suffixOverrides=","> 
				<trim prefix="send_status =case" suffix="end,"> 
	                <foreach collection="list" item="item"  index="index"> 
	                         	when email_remind_id = #{item.emailRemindId} then #{item.sendStatus} 
	                </foreach> 
             	</trim>
             	<trim prefix="send_time =case" suffix="end,"> 
	                <foreach collection="list" item="item"  index="index"> 
	                         	when email_remind_id = #{item.emailRemindId} then #{item.sendTime}
	                </foreach> 
             	</trim> 
			 </trim>
	</update>
 这样配置生成的sql 是

 

 

update b_email_msg_remind 
set 
	send_status =case when email_remind_id = ? then ? when email_remind_id = ? then ? end, 
	send_email_code =case when email_remind_id = ? then 'abc@abc.abc' when email_remind_id = ? then 'abc@abc.abc' end 

 

分享到:
评论
1 楼 静夜独窗 2016-08-01  
出现这个错误是因为mysql配量更新需要配置一下,在spring.datasource.url后加上allowMultiQueries=true
如:jdbc:mysql://10.10.20.36:3306/test?allowMultiQueries=true
ok,解决

相关推荐

    Mybatis批量更新报错问题

    在使用Mybatis进行数据库操作时,批量更新是一个常见的需求,特别是在处理大量数据的时候。然而,在实际操作中,可能会遇到批量更新报错的情况。本文将详细分析Mybatis批量更新的报错问题及其解决方法。 首先,报错...

    mybatis学习之路mysql批量新增数据的方法

    除了批量新增数据外,MyBatis还提供了批量更新数据的方法,可以使用foreach循环来实现批量update。具体实现方法如下: ```xml update t_customer set c_name = #{cus.name}, c_sex = #{cus.sex}, c_...

    mybatis执行批量更新batch update 的方法(oracle,mysql两种)

    在MyBatis中,批量更新(batch update)是一种提高...通过以上介绍,你应该了解了如何在MyBatis中针对Oracle和MySQL数据库执行批量更新。正确使用批量更新可以显著提高应用程序的性能,同时确保数据操作的高效和安全。

    MyBatis批量插入(insert)数据操作

    本文将详细介绍如何在MyBatis中实现批量插入,并通过一个具体的示例来说明。批量插入操作通常涉及以下关键步骤: 1. **实体类定义**: 实体类`TrainRecord`包含了业务所需的属性,如`id`, `activityId`, `empId`, ...

    Mybatis 处理 CLOB、BLOB 类型数据

    Mybatis 处理 CLOB、BLOB 类型数据 MyBatis 处理 CLOB、BLOB 类型数据是指在使用 MyBatis 框架时,如何正确地处理大字段类型的数据。CLOB(Character Large OBject)和 BLOB(Binary Large OBject)都是大字段类型...

    Mybatis批量插入更新xml方式和注解方式的方法实例

    Mybatis批量插入更新xml方式和注解方式的方法实例 Mybatis是一款流行的持久层框架,它提供了多种方式来实现批量插入和更新操作。今天,我们将介绍Mybatis批量插入更新xml方式和注解方式的方法实例。 Mybatis批量...

    批量更新两种方法实践

    例如,可以编写一个包含多个UPDATE语句的脚本,或者使用IN子句一次性更新多条满足特定条件的数据。在MySQL中,可以构造这样的语句: ```sql UPDATE 表名 SET 字段名 = 新值 WHERE 条件; ``` 如果你有一系列ID需要...

    mybatis 对clob类型转换

    当你需要将一个字符串或者Reader对象插入到CLOB字段时,MyBatis默认可能无法正确处理。此时,你需要自定义一个TypeHandler来处理这种转换。创建一个实现`org.apache.ibatis.type.TypeHandler`接口的类,重写`...

    struts2+mybatis解决longblob字段问题

    在Java Web开发中,Struts2和MyBatis是两个常用的框架,分别用于处理MVC模式中的动作控制和持久层操作。在某些情况下,我们可能需要存储大量的二进制数据,如图片、视频或者大文本文件,这时MySQL数据库中的`...

    mysql字段加密

    MySQL 字段加密 MySQL 字段加密是将数据库字段类型设置为 varbinary 类型,并将加密盐设置为全局变量,以实现对敏感数据的保护。本文将详细介绍 MySQL 字段加密的实现步骤和相关知识点。 数据库字段类型 在 MySQL...

    myBatis一对一和一对多

    在主类JavaBean中,定义一个List或Set类型的集合属性,用来存储一对多关系中的子对象。 3. **使用示例**: 在查询班级信息时,MyBatis会执行SQL获取班级信息,并通过`&lt;collection&gt;`配置的SQL获取所有学生信息,...

    Mysql中FIND_IN_SET()和IN区别简析

    在MySQL数据库中,`FIND_IN_SET()` 和 `IN` 是两种不同的查询方法,它们在处理数据集时有不同的特性和应用场景。本文将对这两种方法进行详细对比,以帮助理解它们之间的差异。 `FIND_IN_SET()` 函数主要用于在一个...

    总结--Mybatis传递参数的几种方法

    在 Mybatis 中,传递多个参数的一种常用做法是把多个参数都放在 Map 中,然后传递这个 Map 作为参数。例如: ```java public void insertAreaDivInfor(HashMap map); ``` 对应的 XML 代码为: ```xml insert into ...

    springmybatis

    mybatis 用来建立 sessionFactory 用的,里面主要包含了数据库连接相关东西,还有 java 类所对应的别名,比如 &lt;typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/&gt; 这个别名非常重要,你在 具体的类...

    mybatis+mysql的增删改查(含架包)

    在MySQL中创建一个简单的用户表,如`users`,包含`id`、`username`和`password`字段。 3. **配置MyBatis** 创建mybatis-config.xml文件,配置数据库连接信息和Mapper扫描路径。 4. **实体类(Entity)** 创建一...

    mysql中find_in_set函数的基本使用方法

    有一个应用类型表,表中有parentId字段和parentIds字段,前者为父级id,后者为多级父级id,在库中都是varchar类型,parentIds由多个父级id拼接而成由”,”分隔. 组长告知我可以使用该函数,就去进行了了解与使用. 语法 ...

    自动读取mysql数据库字段并自动生成java属性和set和get方法

    在给定的标题"自动读取mysql数据库字段并自动生成java属性和set和get方法"中,我们可以理解这是一个自动化过程,它减少了开发者手动编写Java代码的工作量。该过程通常包括以下几个步骤: 1. **连接数据库**:首先,...

    mysql的timestamp类型字段为'0000-00-00 00:00:00'导致mybatis映射时报错解决方法

    然而,当你在使用MyBatis进行数据映射时,可能会遇到一个特定的问题,即当`TIMESTAMP`字段的值为'0000-00-00 00:00:00'时,MyBatis可能无法正确处理这个特殊值,导致映射异常。这个问题通常出现在尝试插入或更新包含...

    Mybatis查询方式

    User类中的roles字段表示用户可以有多个角色,Role类中的users字段表示角色可以被多个用户拥有。中间表`user_role`用来存储用户ID和角色ID。 #### XML映射文件配置 多对多查询通常涉及多个表和中间表,因此配置较为...

Global site tag (gtag.js) - Google Analytics