亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis批量插入,返回主鍵ID不成功,巨坑記錄

 更新時間:2023年12月08日 15:04:05   作者:程序員Forlan  
這篇文章主要介紹了Mybatis批量插入,返回主鍵ID不成功,巨坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

一、場景說明

批量插入,返回主鍵ID報錯

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter ‘id’ not found. Available parameters are [entitys, param1]

二、代碼

dao.java

int insertBatch(@Param("entitys") List<ForlanDTO> entities);

dao.xml

    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        insert into forlan_batch_insert(name,age)
        values
        <foreach collection="entitys" item="entity" index="index" separator=",">
            (#{entity.name}, #{entity.age})
        </foreach>
    </insert>

mybatis版本號為:3.4.2

<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.4.2</version>
</dependency>

三、解決方案

1、換mybatis版本

調(diào)整版本號為3.5.2

<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.5.2</version>
</dependency>

換了版本號后,就正常了,其它版本大家可以測試下,具體從什么版本后修復(fù)的,這個暫時沒查到

2、調(diào)整代碼

dao.java

int insertBatch(@Param("list") List<ForlanDTO> entities);

dao.xml

    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        insert into forlan_batch_insert(name,age)
        values
        <foreach collection="list" item="entity" index="index" separator=",">
            (#{entity.name}, #{entity.age})
        </foreach>
    </insert>

關(guān)鍵點:foreach里的collection必須是list,不然就會報錯

四、拓展說明

關(guān)于返回主鍵ID,需要在insert標(biāo)簽中添加,useGeneratedKeys=“true” keyProperty=“id”,useGeneratedKeys和keyProperty必須配合使用,keyProperty的字段就是返回的主鍵ID

mybatis3.3.1及以上的版本,才支持批量插入返回主鍵ID

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論