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]

測試例子可參考

關注下,博文第一時間推送。
有技術問題的都可以加我公眾號提問。掃一掃,升職加薪就是你。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM