Mybatis中批量插入並返回主鍵筆記
阿新 • • 發佈:2019-01-03
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。