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 '

    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 '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 'type=InnoDB' 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 'groups)VALUES('1','hh','hh@163.com','Boss')' 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 'like '%逸%'' 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 'group t1,customer t2

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

随机推荐

  1. 5.Python文件操作之增删改查

    需求一:取文件的前几行: f = open("yesterday","r",encoding="utf-8") for i in range ...

  2. happyxiaofan的程序员书单

    转自   happyxiaofan 读书的看法 从15年7月至今,研究生期间读了不少书,读书让我学到了很多,也是提升技术能力的一个重要手段.可能很多人嫌读书太花时间,曾经的我一度也是这么认为的,觉得一 ...

  3. ZooKeeper自定义数据日志目录

    安装版本:zookeeper-3.4.10 问题描述: ZooKeeper在启动时会将zookeeper.out输出到当前目录,不仅不友好,有时候可能会因为目录权限问题引发一些不必要的麻烦. 脚本分析 ...

  4. 扩展saltstack组件

    一.扩展Grains 我们知道,grains可以帮助收集minion端的静态信息.在更为复杂的业务环境中,可以使用自定义的grains帮助收集真实需要的静态或者业务信息; grains流程:  mas ...

  5. js对象和jquery对象互相转换

    javascript对象转成jquery对象 jquery对象转成javascript对象

  6. python 2.0 与 python 3.0 区别

    区别一:           python 2.0 : 源码不规范,重复代码很多 python 3.0 : 源码精简,美观.优雅 区别二: PY2 : 有整型int.长整型long. py3:只有整型 ...

  7. Matlab练习——rpy2tr函数与自己实现的ZYX欧拉角的结果不同的问题

    问题背景:在根据<机器人导论>这本书的Z-Y-X欧拉角原理用Matlab实现旋转矩阵求解时,发现与直接调用机器人工具箱中的rpy2tr()函数得出的结果并不相同. 首先:先检查自己写的函数 ...

  8. &符号 (弃用引用传参了,不要用!!)

    写法一 $age = function grow($age) { $age += ; return $age; } echo grow($age) echo $age 写法二 $age = funct ...

  9. tomcat架构分析(valve机制)

    关于tomcat的内部逻辑单元的存储空间已经在相关容器类的blog里阐述了.在每个容器对象里面都有一个pipeline及valve模块. 它们是容器类必须具有的模块.在容器对象生成时自动产生.Pipe ...

  10. kotlin函数api

    原 Kotlin学习(4)Lambda 2017年09月26日 21:00:03 gwt0425 阅读数:551   记住Lambda的本质,还是一个对象.和JS,Python等不同的是,Kotlin ...