mybatis批量更新update-设置多个字段值allowMultiQueries=true
mybatis由于简单易用性得到大家的认可和使用
但是在批量更新操作中,网上介绍的貌似不全,正好今天做个记录,大家一起进步
在实际项目开发过程中,常有这样的需求:根据ids更新表的某一个字段值,这时的sql语句是:
- public interface IStaffDao {
- void batchUpdate(@Param("list") List<Long> list);
- }
- <select id="getStaffsByIds" resultMap="staff_Mapper">
- update staff set status = 0 where id in
- <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
- #{item}
- </foreach>
- ORDER BY id
- </select>
还有一种情况:根据ids更新表的多个值,并且每个id对应的值也不一样,这时上述语句已经满足不了需求,需要另一种批量更新sql语句
- public interface IStaffDao {
- void batchUpdate(@Param("list") List<Staff> list);
- }
- <update id="batchUpdate" parameterType="java.util.List" >
- <foreach collection="list" item="item" index="index" separator=";">
- UPDATE staff set count = #{item.count} , code = #{item.code} , invalid_time = #{item.time} WHERE id = #{item.id}
- </foreach>
- </update>
由于这种批量更新是一次执行多个update语句,所以mybatis需要额外的配置:
在spring.datasource.url后加上allowMultiQueries=true
如:jdbc:mysql://10.10.20.36:3306/test?allowMultiQueries=true
否则,在执行sql语句时,会报下面的错误
- [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 staff
- SET status = 1, send_email_code='abc@abc.abc';<span style="font-family: Helvetica, Tahoma, Arial, sans-serif;">update sta<span style="font-size:10px;">ff SET status = 2,</span> send_email_code='test@qq.com' </span>' 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
mybatis批量更新update-设置多个字段值allowMultiQueries=true的更多相关文章
- mybatis批量更新update-设置多个字段值 报错 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
mybatis批量更新update-设置多个字段值 2016年08月01日 12:49:26 姚一号 阅读数:29539 标签: mysql mybatis批量更新批量更新allowMultiQuer ...
- 第五章 mybatis批量更新update
一.所有的指定id的模型类的同一个字段进行批量更新 实际上: update t set fileld='xx' where id in (id1,id2,...,idn) 代码: <update ...
- mybatis批量更新 UPDATE mysql
oracle和mysql数据库的批量update在mybatis中配置不太一样: oracle数据库: <update id="batchUpdate" parameterT ...
- mybatis执行批量更新update
Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...
- Mybatis批量更新<转>
Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插入还有批量删除的帖子.但是批量更新却没有详细的解决方案. 实现目标 这里主要讲的是1张table中.根 ...
- Mybatis批量更新详解
转:http://www.cnblogs.com/winkey4986/p/3915151.html Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插 ...
- MyBatis批量更新
逐条更新 这种方式显然是最简单,也最不容易出错的,即便出错也只是影响到当条出错的数据,而且可以对每条数据都比较可控. 代码 updateBatch(List<MyData> datas){ ...
- mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样
Mybatis批量更新数据 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批 ...
- mybatis批量更新报错 org.mybatis.spring.MyBatisSystemException
具体报错信息: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Bin ...
随机推荐
- 【Python网络】网络协议篇
OSI七层协议 互联网协议按照功能不同分为OSI七层 或 TCP/IP五层 或 TCP/IP四层 每层运行常见物理设备 TCP/IP五层模型讲解 每层都运行特定的协议,越往上越靠近用户,越往下越靠近硬 ...
- Program Transformation Semantics (程序转换语义学)
本文是Inside The C++ Object Model Chapter 2 部分的读书笔记.讨论编译器调用拷贝构造函数时的策略(如何优化以提高效率),侯捷称之为"程序转化的语义学&qu ...
- Ubuntu安装jdk10
一:去官网下载jdk,和jre 因为jdk10之后jdk和jre是分开的 jdk下载 jre下载 二:解压缩,并放到指定目录 # 创建目录 sudo mkdir /usr/lib/java ...
- numpy中np.max() 和 np.maximum() 的区别
np.max(a, axis=None, out=None, keepdims=False) # 接收一个参数a # 取a 在 axis方向上的最大值 np.maximum(x, y) # 接收两个参 ...
- JavaWeb-SpringSecurity实现需求-判断请求是否以html结尾
系列博文 项目已上传至guthub 传送门 JavaWeb-SpringSecurity初认识 传送门 JavaWeb-SpringSecurity在数据库中查询登陆用户 传送门 JavaWeb-Sp ...
- Vue_(组件通讯)非父子关系组件通信
Vue单项数据流 传送门 Vue中不同的组件,即使不存在父子关系也可以相互通信,我们称为非父子关系通信 我们需要借助一个空Vue实例,在不同的组件中,使用相同的Vue实例来发送/监听事件,达到数据通信 ...
- HDU 5894 hannnnah_j’s Biological Test ——(组合数)
思路来自于:http://blog.csdn.net/lzedo/article/details/52585170. 不过并不需要卢卡斯定理,直接组合数就可以了. 代码如下: #include < ...
- Linux sssd 进程 ldap 客户端配置
Linux sssd 进程 ldap 客户端配置 标签(空格分隔): ldap authconfig authconfig命令解析:authconfig 面对多计算机的身份管理以及账户信息同步, 其解 ...
- Retrofit 使用简介
一,简介 Retrofit 是目前使用广泛的 Http Client 框架,它适用于 Android 和 Java. 但需要注意的是,Retrofit 本身并不是一个网络请求框架,而是一个网络请求框架 ...
- EBS 修改系统颜色
1)修改 配置文件: Java 色彩设计,选择相应的颜色 2)清理高速缓存 注:如果不清理缓存,则要等15分钟后才显示变成新设定的颜色