可分為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