好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

ibatis结合oracle批量插入三种方法的测评

第一种

?

1

2

3

4

5

6

7

8

9

10

< 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命令未正确结束

第二种

?

1

2

3

4

5

6

7

8

9

<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

第三种方式

?

1

2

3

4

5

6

7

8

9

10

<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/moneyshi/article/details/22807239

查看更多关于ibatis结合oracle批量插入三种方法的测评的详细内容...

  阅读:14次