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 ...
随机推荐
- React组件(组件属性this.state和this.props,css样式修饰组件)
目录: 1.创建组件的第一种方式 function2.将组件抽离为单独的jsx文件3.省略.jsx后缀, 配置webpack设置根目录4.创建组件的第二种方式--使用class关键字创建组件5.组件私 ...
- Monkey使用详情
https://blog.csdn.net/zhangmeng1314/article/details/82699316 比如使用 adb shell input keyevent <keyco ...
- java上传视频文件
需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...
- java+上传大视频文件断点续传
上周遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误. 查看了系统的配置文件没有看到文件大小限制, web.xml中s ...
- 28.Python list列表详解
在实际开发中,经常需要将一些(不只一个)数据暂储起来,以便将来使用.说到这里,一些读者可能知道或听说过数组,它就可以把多个数据挨个存储到一起,通过数组下标可以访问数组中的各个元素.但使用数组存储数据有 ...
- Marked
哈夫曼树 2-sat问题 线性代数基础 矩阵和行列式基础 可并堆1 可并堆2 概率与期望概念 Kruskcl重构树1 Kruskcl重构树2 匈牙利算法 带权并查集 C++参考手册 尺取法 AC自动机 ...
- SRS之TS封装PAT和PMT
1. SrsTsContext::encode_pat_pmt 在该函数中,将 PAT 和 PMT 封装到 TS Packet 中,并将这两个 TS packet 写入到 ts 文件中. /* the ...
- P4138 [JOISC2014]挂饰
P4138 [JOISC2014]挂饰 ◦ N个装在手机上的挂饰.挂饰附有可以挂其他挂件的挂钩.每个挂件要么直接挂在手机上,要么挂在其他挂件的挂钩上.直接挂在手机上的挂件最多有1个. ...
- Android通过JNI实现守护进程与卸载后跳转指定网页
JNI进程守护 c代码部分如下:JNIEXPORT void JNICALL Java_com_sharetimes_qude_jni_JNIDaemon_daemon(JNIEnv * env, j ...
- CSS鼠标效果手型效果
Example:CSS鼠标手型效果 <a href="#" style="cursor:hand">CSS鼠标手型效果</a> Exam ...