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. 处理大并发之五 使用libevent利器bufferevent

    转自:http://blog.csdn.net/feitianxuxue/article/details/9386843 处理大并发之五 使用libevent利器bufferevent 首先来翻译一段 ...

  2. [转]libevent简介和使用

    libevent是一个基于事件触发的网络库,memcached底层也是使用libevent库. 总体来说,libevent有下面一些特点和优势:* 事件驱动,高性能:* 轻量级,专注于网络: * 跨平 ...

  3. excel 导入数值变成科学记数的解决办法.

    string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=Yes;IM ...

  4. Displaying Bitmaps Efficiently 显示图片相关

    http://developer.android.com/training/displaying-bitmaps/index.html .手机内存资源有限 .Bitmap占用的内存大 .App有时需要 ...

  5. 兵家必争之地——关于O2O商业模式的一点遐想

    先来说说什么是O2O(online to offline)商业模式.创新工场CEO李开复在提及O2O模式时指出,“你如果不知道O2O至少知道团购,但团购只是冰山一角,只是第一步”.O2O营销模式又称离 ...

  6. dispatch_set_target_queue 说明

    参照:http://blog.csdn.net/growinggiant/article/details/41077221 http://codingobjc.com/blog/2013/05/07/ ...

  7. 【学】React的学习之旅6-组件的嵌套2

    <input type=text placeholder='aaa'>, placeholder属性是定义文本框在没有输入值之前显示的一段灰色提示 ()=>{}箭头函数在ECMA6里 ...

  8. PLSQL Developer图形化窗口创建数据库全过程

    1.用系统管理员登陆,我这里用户名是system,密码是manager2.首先建立表空间(tablespaces),点击file->new->sql window   create tab ...

  9. 转 strace

    转自http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Li ...

  10. 对话框AlertDialog.Builder使用方法

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...