第一种
< insert id =" insert_table " parameterClass ="java.util.List" >
<![CDATA[
insert into sj_test( col1 , col2 , col3 ) values
]]>
< iterate conjunction ="," >
<![CDATA[
(#test[]. col1 #, # test []. col2 #, # test []. col3 #)
]]>
</ iterate >
</ insert >
这种方式是网上最常见的,但是也是问题最大的, 今天把我彻底纠结了,弄了几个小时,最后发现, Oracle不支持 一个insert多个values的方式, 不知道网友们被坑到了没,好像MySQL支持这种 方式 所报的错误:ORA-00933:SQL命令未正确结束
第二种
<insert id="insert_table " parameterClass="java.util.List">
insert all
<iterate conjunction="">
into sj_test( col1 , col2 , col3 ) values
(#test[]. col1 #, # test []. col2 #, # test []. col3 #)
</iterate>
<!--必须要加上 -->
select * from dual
</insert>
这种方式,Oracle支持,其他的数据库就不知道支不支持,但是这种方式有个局限性,就是你插入的表的列数* 你插入的行数 <1000 才有效
如:
我今天需要插入的表有13列字段,总共需要插入246行,在执行的时候,他就报:ORA-24335 cannot support more than 1000 columns
第三种方式
<insert id="insert_table" parameterClass="java.util.List">
insert into sj_test( col1 , col2 , col3 ) values select
col1 , col2 , col3
from (
<iterate conjunction=" union all ">
select
#test[].col1# as col1 , #test []. col2# as col2, # test[].col3# as col3 from dual
</iterate>
)
</insert>
这种方式 是先将List里面的值,循环拼接成一个查询虚拟表,然后再通过查询虚拟表,获取每一行的数据 插入到你需要插入的表里面去. 这样的话有个需要注意的地方,就是你拼接的SQL语句的长度有没有超过Oracle的最大长度,不过Oracle的最大长度是64KB,你的SQL语句应该不会写这么长吧?
https://blog.csdn.net/u012184337/article/details/52318768
批量修改
<update id="updateInvertExp" parameterType="com.lvic.prsp.dao.dto.CRM_InverttexperienceDto">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
update PRSP_CRM_INVERT_EXPERIENCE
<trim prefix="SET" suffixOverrides=",">
<if test="item.invertname != null and item.invertname !='' ">
INVERTNAME = #{item.invertname,jdbcType=VARCHAR},
</if>
<if test="item.inverttime != null and item.inverttime !='' ">
INVERTTIME = #{item.inverttime,jdbcType=VARCHAR},
</if>
<if test="item.invertdes != null and item.invertdes !='' ">
INVERTDES = #{item.invertdes,jdbcType=VARCHAR}
</if>
</trim>
,UPDATETIMESTAMP=sysdate
where invertid = #{item.invertid,jdbcType=VARCHAR}
</foreach>
</update>
批量新增
<insert id="insertLpCust" parameterType="com.lvic.prsp.dao.dto.CRM_Lpcust">
INSERT INTO
PRSP_CRM_LPCUST
(liid,
custno,
lpname,
lpdes,
delete_flg,
inserttimestamp)
select
PRSP_CRM_LP_ID.nextval,
TT1.*
from (
<foreach collection="list" item="item" index="index" separator="UNION ALL">
select
#{item.custno,dbcType=VARCHAR},
#{item.lpname,dbcType=VARCHAR},
#{item.lpdes,dbcType=VARCHAR},
0,
sysdate from dual
</foreach>
) TT1
</insert>