第一種
<insert id="insertBatch">
INSERT ALL
<foreach collection="list" item="user" separator=" "close="SELECT * FROM dual" index="index">
INTO LY_TEST (id, name, age) VALUES (#{user.id}, #{user.name}, #{user.age})
</foreach>
</insert>
第二種
<insert id="insertBatch">
INSERT INTO LY_TEST (id, name, age )
<foreach collection ="list" item="user" index="index" separator ="union all">
(SELECT #{user.id,jdbcType=INTEGER},
#{user.name,jdbcType=VARCHAR},
#{user.age,jdbcType=INTEGER}
from dual)
</foreach>
</insert>
第三種
<insert id="insertBatch" parameterType="list">
<foreach collection ="list" item="user" separator =";" open="begin" close = ";end;">
INSERT INTO LY_TEST (id, name, age ) VALUES (#{user.id}, #{user.name}, #{user.age})
</foreach>
</insert>
第四種
INSERT ALL
<foreach collection="list" item="item" separator="">
INTO LY_SJS_WDZM_YXLXD_SXLB_ZX_YZ (SXLBID, YZLXID, ZXCLID, ZT, BBH)
VALUES
(
#{item.thingsTypeId, jdbcType=VARCHAR},
#{item.thingsYzId, jdbcType=VARCHAR},
#{item.thingsClId, jdbcType=VARCHAR},
#{item.status, jdbcType=VARCHAR},
#{item.version, jdbcType=NUMERIC}
)
</foreach>
SELECT 1 FROM DUAL
補充:
批次刪除:
<delete id="xxx" parameterType="list">
delete from LY_TEST
where id in
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</delete>
批次更新:
<update id="batchSaveDraft">
<foreach collection="list" item="item" separator=";" open="begin" close=";end;">
UPDATE LY_SJS SET SFSC = '2',CZSJ = SYSDATE,
NETID = #{item.userId},
WHERE SFSC = '3'
AND ID = #{item.id}
</foreach>
</update>