Mybatis实现Oracle批量插入

J2EE 码拜 9年前 (2015-05-10) 2252次浏览 0个评论

之前用的SqlSessionDaoSupport,但是网上搜了下,发现getSqlSession()不能设置ExecutorType,所以又自己重写了一个类,为SqlSession设置了ExecutorType,Mapper里使用

<insert id="insertBatch" >
    insert into student ( <include refid="Base_Column_List" /> ) 
    values 
    <foreach collection="list" item="item" index="index" separator=",">
        (null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})
    </foreach>
</insert>

会报错,ORA-00933: SQL 命令未正确结束,ORACLE应该不支持这种写法吧,然后改成这样

<insert id="insertBatch" >
    <foreach collection="list" item="item" index="index" separator=",">
    insert into student ( <include refid="Base_Column_List" /> ) 
    values 
        (null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})
    </foreach>
</insert>

还是报错,谁遇到过这种情况,帮忙看下,多谢了,另外问下mybaits这种批量插入的方式如果用SPRING管理事物,中途插入异常会回滚吗?

30分
为啥不在代码里,通过preparedStatement实现批量操作呢
已经实现http://blog.csdn.net/sanyuesan0000/article/details/19998727,不过不支持SPRING的事物管理http://blog.csdn.net/m13321169565/article/details/7729422,在mybaits中通过preparedStatement实现批量操作会不会很麻烦,需要获取preparedStatement对象,1楼的大牛,你做批处理的时候怎么管理事物的?用mybatis自身事物管理吗?
<insert id=”insertBatch” >
    <foreach collection=”list” item=”item” index=”index”
separator=” UNION ALL “>
    insert into student ( <include refid=”Base_Column_List” /> ) 
    values 
      select   null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId} from dual
    </foreach>
</insert>

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Mybatis实现Oracle批量插入
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!