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 ...
随机推荐
- int的最大最小值补码原码转换
原码 正数的二进制表示即为原码(正数的原码.反码.补码均一致) 补码 负数的补码为符号位不变,其余为取反,然后加1 补码的设计目的 (原因:带符号的数加减失效) 1.使符号位能够参与加减运算 2.将减 ...
- Binary Protocol
A. Binary Protocol 这道题要唯一注意的一点就是数字0的表示--0个"1"来表达,所以字符串"100"所表示的数字就是100 附代码: // C ...
- ERROR: virtualenvwrapper could not find virtualenv in your path
环境: Ubuntu 18.04 Python3 使用pip3安装virtualenv和virtualenvwrapper两个包,ubuntu18.04中,用户使用pip安装的包在~/.local/下 ...
- python-无联网情况下安装skt-learn
请从上到下安装 numpy importlib pytz python-dateutil pandas scipy pasty statemodels backports.functools_lru_ ...
- 「HEOI2016/TJOI2016」 排序
题目链接 戳我 \(Solution\) 这道题在线的做法不会,所以这里就只讲离线的做法. 因为直接排序的话复杂度显然不对.但是如果数列为\(01\)串的话就可以让复杂度变成对的了 那么\(01\)串 ...
- Maven :Failed to execute goal on projectt ...: Could not resolve dependencies for project ...
Maven 项目运行 clean install 之前,先要运行父项目的 clean install, 否则可能出现 Failed to execute goal on project ...: C ...
- git忽略提交:.gitignore
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交. Git 忽略文件提交的方法 有三种方法可以实现忽略Gi ...
- Qt加载本地字体 .ttc或.ttf
版权声明:支持原创,转载请说明~ https://blog.csdn.net/luoyayun361/article/details/54934437 //设置本地字体,黑体简 int fontId ...
- Zabbix - 实现对磁盘动态监控
回到目录 前言 zabbix一直是小规模互联网公司服务器性能监控首选,首先是免费,其次,有专门的公司和社区开发维护,使其稳定性和功能都在不断地增强和完善.zabbix拥有详细的UI界面和分组策 ...
- LoaderDialog自定义加载框的实现
package com.loaderman.loadingdialogdemo; import android.app.Dialog; import android.content.Context; ...