Mybatis中批量插入并返回主键笔记

1.mapper中的代码

int insertBatchUserReturnId(List<User> users);

也可以在形参前面加上@Param("xxxx")

xml中的代码,collection必须填list类型

<insert id="insertBatchUserReturnId" keyProperty="userId" useGeneratedKeys="true">
	insert into message (user_id, user_name, user_type, user_passwd, user_phone,user_pic,user_address)
	values
	<foreach collection="list" item="item" open="(" close=")" separator=",">
		#{item.userId,jdbcType=INTEGER}, #{item.userName,jdbcType=VARCHAR}, #{item.userType,jdbcType=TINYINT}, 
		#{item.userPasswd,jdbcType=VARCHAR}, #{item.userPhone,jdbcType=VARCHAR},
		#{item.userPic,jdbcType=VARCHAR},#{item.userAddress,jdbcType=VARCHAR}
	</foreach>
</insert>

执行完这条语句之后,原来的users就会自动带上主键userId。

猜你喜欢

转载自blog.csdn.net/qq_35341771/article/details/83140799