zoukankan      html  css  js  c++  java
  • mybatis,批量新增、修改,删除

    转载自:http://blog.csdn.net/sanyuesan0000/article/details/19998727

    最近需要用到Mybatis批量新增oracle数据库,刚开始在网上找到的方法是都是更新mySQL的,试了一下发现不适合Oracle,后来发现正确的oracle批量新增的sql是:

    <insert id="insertAttractionsBatch" parameterType="java.util.List">
    insert into ATTRACTIONS (
    
    ID, NAME, LONGITUDE, LATITUDE,  UPDATE_TIME
    
    )
    
      <foreach collection="list" item="item" index="index" separator="union all" > 
          (select  
    #{item.id,jdbcType=VARCHAR}, 
    #{item.name,jdbcType=VARCHAR}, 
    #{item.longitude,jdbcType=DECIMAL}, #{item.updateTime,jdbcType=TIMESTAMP}
    
           from dual)
    
        </foreach>
    
    </insert>

    需要重点注意的是sql中没有values,和<foreach>标签中的(selece ..... from dual),MySql中的sql是这样的:

    新增:

    <insert id="insertAttractionsBatch" parameterType="java.util.List">
    insert into ATTRACTIONS (
    
    ID, NAME, LONGITUDE, LATITUDE,  UPDATE_TIME
    
    )  
    
        <foreach collection="list" item="item" index="index" separator="union all" >
    
    #{item.id,jdbcType=VARCHAR}, 
    #{item.name,jdbcType=VARCHAR}, 
    #{item.longitude,jdbcType=DECIMAL}, #{item.updateTime,jdbcType=TIMESTAMP}
    
        </foreach>
    
    </insert>

    oracle更新不能按普通的方式,需要这样:

    <update id="updateAttractionsBatch" parameterType="java.util.List">
        begin  
            <foreach collection="list" item="item" index="index" separator=";" >
                update ATTRACTIONS
                <set>
                <if test="item.id!=null and item.id!=''">
                    id = #{item.id},
                </if>
    
                <if test="item.head!=null and item.head!=''">
                    HEAD = #{item.head},
                </if>
    
                </set>
                where id = #{item.id}
                </foreach>
            ;end;
        </update>

    删除就与MySql一样了如下:

    <delete id="deleteAttractions" parameterType="java.util.List">
    
      delete from ATTRACTIONS
    
      <where>
    
      <foreach collection="list" index="index" item="item" open="(" separator="or" close=")">     
    
      id=#{item.id}
    
        </foreach>
    
      </where>
    
      </delete>


  • 相关阅读:
    第09组 Alpha冲刺(3/4)
    第09组 Alpha冲刺(2/4)
    第09组 Alpha冲刺(1/4)
    数值分析——多项式插值之Lagrange插值
    Deepin下安装搭建latex编写环境
    Deepin下安装Matlab2017a永久版
    数学建模方法-遗传算法(实战篇part 2)
    数学建模方法-遗传算法(实战篇part 1)
    数学建模方法-遗传算法(理论篇)
    Deepin系统下MATLAB中文字体乱码问题解决(加上字体美化)
  • 原文地址:https://www.cnblogs.com/zrui-xyu/p/5684917.html
Copyright © 2011-2022 走看看