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

  • 相关阅读:
    微信端video去除最顶层播放
    MVC错误页面相关说明
    sublime使用
    linux或Mac下手动回滚代码
    用Python操作git命令
    利用pyinstaller打包加密Python项目
    进程、线程和协程的结合使用
    模块导入失败问题
    递归调用解压zip包或rar包
    随机验证码&发红包
  • 原文地址:https://www.cnblogs.com/wangjintao-0623/p/10577489.html
Copyright © 2020-2023  润新知