zoukankan      html  css  js  c++  java
  • Oracle 和 mysql 的批量操作Sql语句 的区别

    正确的oracle批量新增的sql是:

    方法 1:

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

    方法 2 :

    <insert id="insertBatch" parameterType="java.util.List">
    BEGIN
    <foreach collection="list" item="item" index="index" separator=";" >
    INSERT INTO TABLE.STUDENT (ID,AGE,NAME,STU_ID) VALUES
    ( DEMO.SEQ_EID.NEXTVAL,#{item.age},#{item.name},#{item.stuId} )
    </foreach>
    ;END ;
    </insert>

    方法 3 : 

    <insert id="addList" parameterType="java.util.List" useGeneratedKeys="false">
    INSERT ALL
    <foreach item="item" index="index" collection="list">
    INTO T_APPLAUD
    (
    ID,
    USER_ID,
    BUSINESS_TYPE,
    PRODUCT_ID,
    CREATE_TIME
    ) VALUES
    (
    #{item.id, jdbcType=NUMERIC},
    #{item.userId, jdbcType=VARCHAR},
    #{item.businessType, jdbcType=VARCHAR},
    #{item.productId, jdbcType=VARCHAR},
    #{item.createdTime, jdbcType=NUMERIC}
    )
    </foreach>
    SELECT 1 FROM DUAL
    </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>

  • 相关阅读:
    关于extern对变量的使用
    MediaPipe Android Archive
    MediaPipe框架结构
    bazel构建C++工程
    Ubuntu安装ss(终端+浏览器)
    Ubuntu下MediaPipe的环境配置
    Ubuntu使用frp进行内网穿透
    Ubuntu中ssh-server的安装与开机自启动
    Ubuntu系统挂载新硬盘的方法
    Ubuntu16.04下安装TensorFlow
  • 原文地址:https://www.cnblogs.com/wangjintao-0623/p/10577489.html
Copyright © 2011-2022 走看看