mybatis由于简单易用性得到大家的认可和使用

但是在批量更新操作中,网上介绍的貌似不全,正好今天做个记录,大家一起进步

在实际项目开发过程中,常有这样的需求:根据ids更新表的某一个字段值,这时的sql语句是:

  1.  
    public interface IStaffDao {
  2.  
    void batchUpdate(@Param("list") List<Long> list);
  3.  
    }
  1.  
    <select id="getStaffsByIds" resultMap="staff_Mapper">
  2.  
    update staff set status = 0 where id in
  3.  
    <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
  4.  
    #{item}
  5.  
    </foreach>
  6.  
    ORDER BY id
  7.  
    </select>

还有一种情况:根据ids更新表的多个值,并且每个id对应的值也不一样,这时上述语句已经满足不了需求,需要另一种批量更新sql语句

  1.  
    public interface IStaffDao {
  2.  
    void batchUpdate(@Param("list") List<Staff> list);
  3.  
    }
  1.  
    <update id="batchUpdate" parameterType="java.util.List" >
  2.  
    <foreach collection="list" item="item" index="index" separator=";">
  3.  
    UPDATE staff set count = #{item.count} , code = #{item.code} , invalid_time = #{item.time} WHERE id = #{item.id}
  4.  
    </foreach>
  5.  
    </update>

由于这种批量更新是一次执行多个update语句,所以mybatis需要额外的配置:

在spring.datasource.url后加上allowMultiQueries=true
如:jdbc:mysql://10.10.20.36:3306/test?allowMultiQueries=true

否则,在执行sql语句时,会报下面的错误

    1.  
      [org.apache.ibatis.session.defaults.DefaultSqlSession@76a2f910]
    2.  
      org.springframework.jdbc.BadSqlGrammarException:
    3.  
      ### 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
    4.  
      SET send_status = 1, send_email_code='abc@abc.abc'' at line 6
    5.  
      ### The error may involve com.hhsoft.sectionservice.model.persistence.EmailMapper.updateEmailTasks-Inline
    6.  
      ### The error occurred while setting parameters
    7.  
      ### 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
    8.  
      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
    9.  
      ; 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
    10.  
      SET send_status = 1, send_email_code='abc@abc.abc'' at line

mybatis批量更新update-设置多个字段值allowMultiQueries=true的更多相关文章

  1. 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 ...

  2. 第五章 mybatis批量更新update

    一.所有的指定id的模型类的同一个字段进行批量更新 实际上: update t set fileld='xx' where id in (id1,id2,...,idn) 代码: <update ...

  3. mybatis批量更新 UPDATE mysql

    oracle和mysql数据库的批量update在mybatis中配置不太一样: oracle数据库: <update id="batchUpdate" parameterT ...

  4. mybatis执行批量更新update

    Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...

  5. Mybatis批量更新<转>

    Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插入还有批量删除的帖子.但是批量更新却没有详细的解决方案. 实现目标 这里主要讲的是1张table中.根 ...

  6. Mybatis批量更新详解

    转:http://www.cnblogs.com/winkey4986/p/3915151.html Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插 ...

  7. MyBatis批量更新

    逐条更新 这种方式显然是最简单,也最不容易出错的,即便出错也只是影响到当条出错的数据,而且可以对每条数据都比较可控. 代码 updateBatch(List<MyData> datas){ ...

  8. mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样

    Mybatis批量更新数据 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批 ...

  9. mybatis批量更新报错 org.mybatis.spring.MyBatisSystemException

    具体报错信息: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Bin ...

随机推荐

  1. [Python之路] 实现简易HTTP服务器与MINI WEB框架(利用WSGI实现服务器与框架解耦)

    本文描述如果简单实现自定义Web服务器与自定义简易框架,并且不断进行版本迭代,从而清晰的展现服务器与Web框架之间是如何结合.如何配合工作的.以及WSGI是什么. 本文帖的代码有点多,但基本每次迭代修 ...

  2. 棋盘问题 ( POJ -1321 )(简单DFS)

    转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82684942作者:Mercury_Lc 题目链接 题解:dfs入门,就是每个点都搜 ...

  3. jQuery文档操作之克隆操作

    语法: $(selector).clone(); 解释:克隆匹配的DOM元素 $("button").click(function(event) { //1.clone():克隆匹 ...

  4. Centos 安装字体库 以及解决confluence 旧文档数据的乱码

    首先,第一步我们需要执行以下的命令来安装字体管理工具:  yum install -y fontconfig mkfontscale 然后我们到(Windows系统)“c:/windows/fonts ...

  5. HNOI2015菜肴制作

    一开始,没想出来,先topsort判环,把impossible拿到手,然后划分联通块,对每个联通块跑一遍topsort,觉得可对了,然后被大样例教育明白了,知道自己的策略错在哪了. 接着在纸上疯狂手模 ...

  6. y7000笔记本 darknet-yolo安装与测试(Ubuntu16.04+Cuda9.0+Cudnn7.1)

    https://zhuanlan.zhihu.com/p/41096599 1.先查看是否安装有以下组件,若有先考虑彻底删除再安装(安装严格按照下面顺序进行) 查看nvidia 版本 nvidia-s ...

  7. linux vmware 安装步骤

    一.下载vmware软件 二.下载centos镜像文件 三.安装步骤 以上相当于于硬件设备已经准备ok,接下来安装软件

  8. 快速查找 js 插件

    我们是否为一个插件找半天找不到而烦恼 BootCDN 现在不用了,我们可以在 https://www.bootcdn.cn/ 中查找我们想要的任何插件,然后点进去,一直到点进去文件,我们便可以得到 这 ...

  9. CentOS 7下使用Apache2部署Django项目,解决文件名中含有中文报错的问题

    系统版本: CentOS 7.3Apache 2.4 Django 1.11 问题描述 Django项目涉及上传操作,上传文件名称含有中文,若使用runserver启动服务,没有问题!若将Django ...

  10. Javascript和JQuery获取浏览器窗口各种尺寸

    原生JS 窗口尺寸: console.log('window.innerWidth = ' + window.innerWidth + '---window.innerHeight = ' + win ...