Mybatis 对oracle进行批量操作

来源:互联网 发布:怎么看自己淘宝店铺号 编辑:程序博客网 时间:2024/05/17 01:48

最近需要用到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>


一个更简明例子:http://www.cnblogs.com/xxyfhjl/p/5659524.html

原创粉丝点击