Mybatis批量增加,删除,更新Oracle
2014-02-26 20:42
357 查看
最近需要用到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>
<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>
相关文章推荐
- Mybatis批量增加,删除,更新Oracle
- Mybatis批量增加,删除,更新Oracle
- Mybatis批量增加,删除,更新Oracle
- Mybatis批量增加,删除,更新Oracle
- Mybatis批量增加,删除,更新Oracle
- Mysql和Oracle 用Mybatis批量增加,删除,更新
- Mybatis批量增加,删除,更新Oracle
- Mybatis批量增加、批量更新、批量删除和查询
- oracle和mysql在创建表、更新表(增加、修改、删除字段)、删除表的区别与联系
- oracle增加,修改,删除某列
- oracle使用sql语句增加字段示例(sql删除字段语句)
- 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常
- Oracle 增加修改删除字段
- oracle游标的更新与删除数据
- oracle删除表字段和oracle表增加字段
- Oracle的列操作(增加列,修改列,删除列),包括操作多列
- Oracle 增加修改删除字段与添加注释
- Oracle 增加修改删除字段
- Oracle 增加修改删除字段与添加注释
- Mongodb基本数据类型、常用命令之增加、更新、删除