Mybatis批量插入返回多個主鍵
熱心的托尼老師就幫忙寫了個例子測試。測試了兩個Mybatis版本,低版本的不支持,大家注意了。
<insert id="insertBatchInterest" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
insert into xxxxx ( `uid`)
values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.uid,jdbcType=INTEGER} )
</foreach>
</insert>
執行java 代碼
@Test
public void test() {
List<CouponInterestPO> batchList = new ArrayList<>();
CouponInterestPO po = new CouponInterestPO();
po.setUid(111);
batchList.add(po);
po = new CouponInterestPO();
po.setUid(222);
batchList.add(po);
couponInterestDao.insertBatchInterest(batchList);
for (CouponInterestPO c : batchList) {
LOG.info("返回主鍵====>"+c.getId());
}
測試是成功
mybatis
的3.4.1測試成功的,測試結果如上圖
mybatis
的3.27的版本是不支持的會報錯,報
org.apache.ibatis.binding.BindingException: Parameter 'id' not found.
Available parameters are [list]
關注下,博文第一時間推送。
有技術問題的都可以加我公眾號提問。掃一掃,升職加薪就是你。