mybatis配置对sql的批量插入


可分为xml配置和注解配置。

 

1、使用xml配置:

  (1)xml配置单项插入,在外部执行循环。

 

  (2)xml配置循环插入,外部传入LIst类型的参数(我使用的)。

 

code:

  (1)xml配置:

 1 <!--定义批量插入-->
 2   <insert id="insertBatch" parameterType="java.util.List" >
 3 
 4     insert into scores (scores_id, scores_tcId, scores_stuId,
 5     scores_res, scores_intime)
 6     values
 7     <foreach collection="list" item="item" index="index" separator=",">
 8       (#{item.scoresId,jdbcType=INTEGER},
 9        #{item.scoresTcid,jdbcType=VARCHAR},
10        #{item.scoresStuid,jdbcType=VARCHAR},
11        #{item.scoresRes,jdbcType=DOUBLE},
12        #{item.scoresIntime,jdbcType=TIMESTAMP})
13     </foreach>
14   </insert>

  (2)mapper接口:

 

    /**
     * 自定义批量插入
     * @param list
     * @return
     */
    int insertBatch(@Param("list") List<Scores> list);

 

其他的类型:略

参考博客:https://blog.csdn.net/qq_38011415/article/details/84928791

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM