mybatis批量更新update-设置多个字段值

2016年08月01日 12:49:26 姚一号 阅读数:29539 标签: mysql mybatis批量更新批量更新allowMultiQuerie更多

个人分类: mybatis
 
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mingliangniwo/article/details/52084648

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-设置多个字段值 报错 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的更多相关文章

  1. MySql 执行语句错误 Err] 1064 - 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

    关于用Power Designer 生成sql文件出现 错误  [Err] 1064 - You have an error in your SQL syntax; check the manual ...

  2. 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 &#39;

    mysql中如果字段使用了关键字,在插入和更新时会提示 You have an error in your SQL syntax; check the manual that corresponds ...

  3. 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 &#39;varchar(255), sort integer not null

    you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...

  4. ERROR: 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 &#39;type=InnoDB&#39; at line 7

    问题: 使用hibernate4.1.1,数据库使用mysql5.1.30,使用hibernate自动生成数据库表时,hibernate方言使用org.hibernate.dialect.MySQLI ...

  5. 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数据库时打印出来的异常: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...

  6. 1064 - 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 &#39;groups)VALUES(&#39;1&#39;,&#39;hh&#39;,&#39;hh@163.com&#39;,&#39;Boss&#39;)&#39; at line 1

    mysql8.0版本 在已存在的表里插入一条数据 insert INTO api_user(id,username,email,groups)VALUES('1','hh','hh@163.com', ...

  7. C# Mysql 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 ????

    有几年没用过MySql数据了,今天在使用C#访问MySql数据库时出现了一个小插曲. 错误提示: You have an error in your SQL syntax; check the man ...

  8. 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 &#39;like &#39;%逸%&#39;&#39; at line 1

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  9. 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 &#39;group t1,customer t2

    ### SQL: select t1.gid,t1.gname,t1.gvalue,t1.gtype, t1.gaddress,t1.gmembers, t1.gcode,t1.gphone, t2. ...

随机推荐

  1. 初见SpringMVC

    1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...

  2. 【JAVA】JAVA 反射

    在Java反射机制中,需要掌握的知识有:         (1)掌握反射机制的概述.         (2)能够使用Class类并结合java.lang.reflect包取得一个类的完整结构.     ...

  3. 常用移动web开发框架研究分析

    纯粹的总结一下移动web开发框架,移动web开发框架有jQuery Mobile .Sencha Touch等等,他们都来源于web开发,是成熟的框架,jQuery Mobile出自于jQuery家族 ...

  4. 【转载/修改】ScrollLayout代码修正,追加模仿viewpager滚动速度

    组件作用为类似ViewPager但直接插视图的横向滚动容器. 修改自:http://blog.csdn.net/yaoyeyzq/article/details/7571940 在该组件基础上修正了滚 ...

  5. PIGCMS提示“你的程序为盗版,非法授权,请联系QQ7530782或者8441010”的修复方法

    最近群里又有人发出来微信平台盗版源码这个问题求解决,其实我本人是一直支持正版的,大家有条件的还是购买正好为好,既然有人问我就顺便解决了下,其实很简单,再换个接口就好了,查看了一下是在\PigCms\L ...

  6. KinSlideshow参数设置说明

    [引用来自:http://blog.sina.com.cn/s/blog_4f4f4a5901014zn1.html] Jquery.KinSlideshow参数设置说明: 附:所有参数列表   in ...

  7. JavaScript高级程序设计15.pdf

    组合继承的问题是会调用2次超类型构造函数 寄生组合式继承 即通过借用构造函数来继承属性,通过原型链的形式来继承方法,思路:不必为了指定子类型的原型而调用超类型的原型,我们所需要的无非是超类型原型的一个 ...

  8. js获取元素的滚动高度,和距离顶部的高度

    jq: 获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度 $(doc ...

  9. 使用Redux DevTools浏览器插件调试redux

    与redux的Devtools模块不同,该工具主要依赖浏览器插件完成.模式也比Devtools简单点. step1 下载插件 Chrome地址(360极速模式也可以用): https://chrome ...

  10. Linux下载软件

    .yum yum install tree yum install telnet 直接安装与更新 .查询软件是否装上 rpm -qa tree telnet .查询软件包的内容 rpm -ql tre ...