0
点赞
收藏
分享

微信扫一扫

MyBatis设置主键回填

TiaNa_na 2022-08-07 阅读 83


单条插入

useGeneratedKeys设置为true,keyProperty为要回填的属性名

<insert id="insertBook" useGeneratedKeys="true" keyProperty="id">
insert into t_book (b_name,author) values (#{name},#{author});
</insert>

批量插入

int batchInsert(@Param("list") List<CarSourceFollow> carSourceFollowList);

注意:foreach标签中collection的名字必须为list,不然不会回填成功

<insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyProperty="id">
insert into car_source_follow (car_source_id, user_id, follow_type, follow_progress, content, img_link) values
<foreach collection="list" item="item" separator=",">
(#{item.carSourceId}, #{item.userId}, #{item.followType}, #{item.followProgress}, #{item.content}, #{item.imgLink})
</foreach>
</insert>



举报

相关推荐

0 条评论