首页 > 技术文章 > mybatis支持oracle批量插入

xxyfhjl 2016-07-11 10:53 原文

问题:mysql使用mybatis批量插入时,通过foreach标签,将每条记录按照逗号","连接即可。

但是,oracle不支持。

 

oracle支持如下写法:

 

<insert id="insertStudents">
        INSERT INTO Student
            (
                id, 
                name, 
                age, 
                sex
            )
        <foreach collection="stuList" item="item" index="index" separator="union all" > 
              (
                  select  
                      #{item.id,jdbcType=VARCHAR},
                    #{item.name,jdbcType=VARCHAR},
                    #{item.age,jdbcType=VARCHAR},
                    #{item.sex,jdbcType=VARCHAR}
                from dual
               )
        </foreach>
       </insert>

 

其中dao的写法如下:

public void insertStudents(@Param("stuList") List<Student> stuList);

 

 

 

知识点:

oracle给字段起有空格的别名:select count(*)  as "my sum" from student;   使用双引号""。

 

推荐阅读