首页 技术 正文
技术 2022年11月21日
0 收藏 847 点赞 2,332 浏览 3264 个字

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’    ENDWHERE 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--------------------------------------------------------------------------------------------------------------------------------

mybatis执行批量更新update 的方法oracle小记

标签: mybatisjava批量更新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> 

 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845