Mybatis3中对oracle的批量插入

  Mybatis批量插入的资料,很多都是mysql的,insert into ... values (),(),...酱紫。oracle中不支持这种语法,oracle中的批量插入是酱紫insert into selcect ... union all select ...。在这里http://my.oschina.net/u/232879/blog/118245有说明
mysql
<insert id="addRoleModule" parameterType="java.util.List">
    INSERT INTO T_P_ROLE_MODULE (ROLE_ID, MODULE_ID)
    VALUES <foreach collection="list" item="item" index="index"  
    separator=",">  
    ( #{item.roleId}, #{item.moduleId})  
    </foreach>  
</insert>

oracle
<insert id="addRoleModule" parameterType="java.util.List">
    INSERT INTO T_P_ROLE_MODULE (ROLE_ID, MODULE_ID)
    <foreach collection="list" item="item" index="index" separator=" UNION ALL ">  
    SELECT #{item.roleId}, #{item.moduleId} FROM DUAL
    </foreach>  
</insert>
      mybatis的foreach说明如下
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
      测试这种方式可行,但现在想插入同时指定序列,想当然这样改了下
    <foreach collection="list" item="item" index="index" separator=" UNION ALL ">  
          SELECT SEQ_XXX.NEXTVAL , #{item.roleId}, #{item.moduleId} FROM DUAL
    </foreach>
报ORA-02287: sequence number not allowed here错误。ORA-02287错误网上最多的例子就是序列不能跟group by一起使用,解决方法就是把group by语句包起来,外层再select序列就可以了。目前这个错误显示不是group by的问题,也有神似大仙的人物建议使用oracle的insert all,可这个是将数据插入多张表的语句,与当前问题不属于同一范畴。在一个wiki里看到ORA-02287错误的详细描述 http://www.orafaq.com/wiki/ORA-02287

The following are the cases where you can't use a sequence:

For a SELECT Statement:

  • In a WHERE clause
  • In a GROUP BY or ORDER BY clause
  • In a DISTINCT clause
  • Along with a UNION or INTERSECT or MINUS
  • In a sub-query

Other areas:

  • A sub-query of Update or Delete
  • In a View or snapshot
  • In a DEFAULT or CHECK Condition of a table definition
  • Within a single SQL statement that uses CURRVAL or NEXTVAL, all referenced LONG columns, updated tables, and locked tables must be located on the same database.
其中讲到union中也不用使用序列。UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果. UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果。 UNION ALL的用法和union一样,只不过union含有distinct的功能,它会把两张表了重复的记录去掉,而union all不会,所以从效率上,union all 会高一点。
      这里就是union中不能使用序列的问题,将select序列语句再往里包一层也是不行的,union会嵌套向里层检查。往里包不行,就往外包,写成这样可行
select SEQ_XXX.NEXTVAL, a.* from
(select 1,'66' from dual union
select 2,'77' from dual) a
      于是foreach的配置变成这个样子就可以了
    <foreach collection="list" item="item" index="index" separator="union all" 
            open="select SEQ_XXX.NEXTVAL, a.* from (" close=") a">
      select #{item.code,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR} from dual
    </foreach>
 
 

使用mybatis将数据批量插入到oracle中,在映射文件中怎么写啊?

1.mybaits版本,3.1.0
2.


      
      
  1. <!-- 批量插入 -->
  2. <insert id="batchInsert" parameterType="java.util.List">
  3. insert into b_agent_info
  4. (id,hkgs,userName,custName,office,remark) values
  5. <foreach collection="list" item="item" index="index"
  6. separator=",">
  7. (#{item.id},#{item.hkgs},#{item.userName},#{item.custName},#{item.office},#{item.remark})
  8. </foreach>
  9. </insert>
3

 

<resultMap type="Preson" id="getPreson">
  <id column="presonId" property="presonId"/>
  <result column="presonName" property="presonName"/>
  <result column="presonTel" property="presonTel"/>
  <result column="presonEmail" property="presonEmail"/>
  <result column="presonAge" property="presonAge"/>
</resultMap>

<!-- 批量插入 -->
 <insert id="inserts" parameterType="java.util.List">
 
  <!-- 这个在下面循环的时候貌似没有被循环
                     ??????????????
                -->
  <selectKey keyProperty="id" resultType="int" order="BEFORE">
   select seq_preson_id.nextval from dual
  </selectKey>
 
  insert into preson
  <foreach collection="list" item="item" index="index" 
             separator="union"> 
             select #{id},#{item.presonName},#{item.presonTel},#{item.presonEmail},#{item.presonAge} from dual 
                </foreach> 
 
 </insert>

 

 

<!-- 批量插入 -->
 <insert id="inserts" parameterType="java.util.List">
 
            <!-- 在此处做了修改 -->
     insert into PRESON
     select SEQ_PRESON_ID.NEXTVAL,A.* from(
  <foreach collection="list" item="item" index="index"
   separator="UNION">
   SELECT
   #{item.presonName},
   #{item.presonTel},
   #{item.presonEmail},
   #{item.presonAge}
   from dual
             </foreach>
  ) A

 </insert>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值