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

update table set column='...' where id in (1,2,3)l
这样的sql就可以了。Mybatis中这样写就行
<update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
    UPDATE STUDENT SET name = #{name} WHERE id IN
    <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</update>
      但是这样的需求很少,一般是有个集合,每个元素中的值是不一样的,然后需要一次性更新。一般的处理方式是使用for循环。这样的效率较低,当数据量大时,期望有种一次性插入的操作。如果使用的是mysql,有
insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo) on duplicate key update
replace into table (aa,bb,cc) values(xxx,xxx,xxx),(ooo,ooo,ooo),(ccc,ccc,ccc) 
两种方式可以处理。
      当前数据库是oracle,可以使用case when来拼成一长串sql处理
UPDATE mytable
    SET myfield = CASE id
        WHEN 1 THEN 'value'
        WHEN 2 THEN 'value'
        WHEN 3 THEN 'value'
    END
WHERE id IN (1,2,3)
实际上这种方式对于mysql也有效。
      最开始的时候,想着写一系列并列的更新语句就可以了
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";"
  open="" close="">
  update REGION_CODE set
    CODE=#{item.Code,jdbcType=VARCHAR},
    NAME=#{item.Name,jdbcType=VARCHAR}
    where ID = #{item.id,jdbcType=DECIMAL}
</foreach>
</update>
这样直接报错,因为Mybatis映射文件中的sql语句不允许 ; 符号。按照可行的case when处理方式,Mybatis映射文件书写方式如下:
<update id="updateBatch" parameterType="java.util.List">
  update REGION_CODE set
    CODE=
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
      when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR}
  </foreach>
  ,NAME=
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
      when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR}
  </foreach>
  where ID in
  <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
      #{item.id,jdbcType=DECIMAL}
  </foreach>
</update>
      至此,批量更新功能完成。
 
--------------------------------------------------------------------------------------------------------------------------------
 
原mybatis执行批量更新batch update 的方法(oracle,mysql)

oracle数据库:

<update id="batchUpdate"  parameterType="java.util.List">

	   <foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";">
update test
<set>
test=${item.test}+1
</set>
where id = ${item.id}
</foreach> </update>

mysql数据库:

mysql数据库采用一下写法即可执行,但是数据库连接必须配置:&allowMultiQueries=true

例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&allowMultiQueries=true

<update id="batchUpdate"  parameterType="java.util.List">

	      <foreach collection="list" item="item" index="index" open="" close="" separator=";">
update test
<set>
test=${item.test}+1
</set>
where id = ${item.id}
</foreach> </update> ------------------------------------------------------------------------------------------------------------------------------ https://my.oschina.net/zouqun/blog/405424 --------------------------------------------------------------------------------------------------------------------------------
2015-03-02 18:10 3993人阅读 评论(0) 收藏 举报

版权声明:本文为博主原创文章,未经博主允许不得转载。

<update id="updateLicaiAllList" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
   update tmi_licai_all t 
   set 
   t.est_amount=#{item.estAmount,jdbcType=NUMERIC}
   where t.licai_id = #{item.licaiId,jdbcType=NUMERIC}
   </foreach>
</update>
 
 

mybatis执行批量更新update的更多相关文章

  1. mybatis执行批量更新batch update 的方法

    1.数据库连接必须配置:&allowMultiQueries=true 我的配置如下:jdbc:mysql://10.20.13.16:3306/CALENDAR?useUnicode=tru ...

  2. mybatis执行批量更新数据

    1.业务需求:同时执行多记录批量操作 2.实现方法:     1)mapping: 2) dao 层 3)Service 层(注意要使用Transactional,否则可能会导致数据紊乱) 4)Con ...

  3. 170829、mybatis使用oracle和mybatis中批量更新

    一.mybatis执行批量更新batch update 的方法(mysql数据库) 1.数据库连接必须配置:&allowMultiQueries=true(切记一定要加上这个属性,否则会有问题 ...

  4. 如何 ︰ 执行批量更新和插入使用.NET 提供程序在 C#.NET OpenXML

    https://support.microsoft.com/zh-cn/kb/315968 如何 ︰ 执行批量更新和插入使用.NET 提供程序在 C#.NET OpenXML Email Prin ...

  5. mybatis 的批量更新操作sql

    转: mybatis 的批量更新操作sql 2018年07月23日 10:38:19 海力布 阅读数:1689   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  6. 【mybatis】mybatis进行批量更新,报错: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

    使用mybatis进行批量更新操作: 报错如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an erro ...

  7. Myabtis中批量更新update多字段

    在mybatis中批量更新多个字段 推荐使用如下操作: 方式1:在Dao层接口中: void updateBatch(@Param("list")List<Student&g ...

  8. Mysql 批量更新update的表与表之间操作

    Mysql 批量更新update的表与表之间操作 一.方法一 使用User2表数据更新User表: update User as a ,User2 as b set a.role_id=b.set_v ...

  9. mysql批量update更新,mybatis中批量更新操作

    在日常开发中,有时候会遇到批量更新操作,这时候最普通的写法就是循环遍历,然后一条一条地进行update操作.但是不管是在服务端进行遍历,还是在sql代码中进行遍历,都很耗费资源,而且性能比较差,容易造 ...

随机推荐

  1. python virtualenv 安装运行saltstack

    需求产生场景:      1.python的virtualenv虚拟环境非常的好用.      2.saltstack作为运维自动化的一个重要组件也挺好用的. 但是:      1.saltsatck ...

  2. 使用curl命令获取文件下载速度

    使用curl可以下载网络内容,那如何获取curl下载时的下载速度呢,使用下面的命令即可: # curl -Lo /dev/null -skw "%{speed_download}\n&quo ...

  3. ruby发送邮件方法

    #encoding:utf-8require 'mail'def send_email sum,fail,case_path,name,receive smtp = { :address => ...

  4. SQLite中的时间日期函数(转)

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...

  5. 如何使用一个对象而非数组元素为ng-options初始化

    a,是引用,而b是一个和a内容相同的另一个对象, 因此不能通过b直接赋值.如果要这样用,就用 track by xxx.id  ,它的作用是通过id(唯一的)去ng-options做一次检索匹配

  6. Xxtea加解密

    转自:http://www.cnblogs.com/luminji/p/3406407.html 很有意思的一件事情,当我想要找 Xxtea 加解密算法的时候,发现了前同事(likui318)的代码, ...

  7. Xcode 自动升级到8.21后坑-Abort trap: 6

    pod install or pod update show this message:Generating Pods project Abort trap: 6solve method: udo g ...

  8. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  9. css不常用重要属性

    超出省略号:display:block;white-space:norwrap;overflow:hidden;text-overflow:ellipsis; white-space:norwrap/ ...

  10. 利用nodejs搭建服务器,测试AJAX

    最近学习了AJAX一直没有进行过测试,前今天了解了Noejs搭建本地服务器下就尝试了一下.通过AJAX请求的方式获取HTTP服务器返回数据的代码 首先创建一个serve.js的文件.并写入以下代码. ...