mybatis oracle批量插入数据

 

方式一

<insert id="addBatch" parameterType="java.util.List">  
    BEGIN  
    <foreach collection="list" item="item" index="index" separator="">  
        insert into test  
        (userid,username createdate)  
        VALUES  
        (  
        #{item.userId,jdbcType=INTEGER},
        #{item.username,jdbcType=VARCHAR},#{item.createDate,jdbcType=DATE});  
    </foreach>  
    COMMIT;  
    END;  
</insert>  

 

方式二

(适用oracle。去掉foreach中的open="(" close=")" 适用于mysql和oracle)

<insert id="addBatch"  parameterType="java.util.List">  
    INSERT INTO test (userid,username createdate 
    )  
    <foreach open="("  close=")" collection="list" item="item" index="index" separator="union all" >  
        select #{item.userId,jdbcType=INTEGER},#{item.username,jdbcType=VARCHAR},#{item.createDate,jdbcType=DATE}   
        from dual  
    </foreach>  
</insert> 

 

 

方式三

使用mybatisplus自带的批量插入方法

IService.java
  /**
* 插入(批量) * * @param entityList 实体对象集合 * @param batchSize 插入批次数量 */ boolean saveBatch(Collection<T> entityList, int batchSize);

 

 

 

posted @ 2021-03-01 17:59  harara  阅读(2943)  评论(0编辑  收藏  举报