方式一

<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);

 

 

 

相关文章:

  • 2022-01-03
  • 2022-01-22
  • 2021-06-08
  • 2022-12-23
  • 2021-12-01
猜你喜欢
  • 2021-12-02
  • 2021-07-20
  • 2021-05-26
  • 2021-11-17
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案