正确的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>