mybatis 批量插入返回多个主键,低版本有bug

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]

测试例子可参考

关注下,博文第一时间推送。
有技术问题的都可以加我公众号提问。扫一扫,升职加薪就是你。

原文地址:https://www.cnblogs.com/tonyY/p/12125316.html