我們都知道Mybatis在插入單條數據的時候有兩種方式返回自增主鍵:
1、對於支持生成自增主鍵的數據庫:增加 useGenerateKeys和keyProperty ,<insert>標簽屬性。
2、不支持生成自增主鍵的數據庫:使用<selectKey>。
但是怎么對批量插入數據返回自增主鍵的解決方式網上看到的還是比較少,至少百度的結果比較少。
Mybatis官網資料提供如下:
First, if your database supports auto-generated key fields (e.g. MySQL and SQL Server), then you can simply set useGeneratedKeys="true" and set the keyProperty to the target property and you're done. For example, if the Authortable above had used an auto-generated column type for the id, the statement would be modified as follows:
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id"> insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio}) </insert>
If your database also supports multi-row insert, you can pass a list or an array of Authors and retrieve the auto-generated keys.
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id"> insert into Author (username, password, email, bio) values <foreach item="item" collection="list" separator=","> (#{item.username}, #{item.password}, #{item.email}, #{item.bio}) </foreach> </insert>
從官網資料可以看出Mybatis是支持批量插入時返回自增主鍵的。
但是在本地測試的時候使用上述方式確實不能返回自增id,而且還報錯(不認識keyProperty中指定的Id屬性),然后在網上找相關資料。終於在Stackoverflow上面找到了一些信息。
解決辦法:
1、升級Mybatis版本到3.3.1。官方在這個版本中加入了批量新增返回主鍵id的功能
2、在Dao中不能使用@param注解。
3、Mapper.xml中使用list變量(parameterType="java.util.List")接受Dao中的參數集合。
下面是具體代碼過程,可供參考
mapper.xml層代碼
<!-- 批量新增 --> <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" > INSERT INTO <include refid="t_shop_resource" /> (relation_id, summary_id, relation_type) VALUES <foreach collection="list" index="index" item="shopResource" separator=","> ( #{shopResource.relationId}, #{shopResource.summaryId}, #{shopResource.relationType} ) </foreach> </insert>
dao實現層代碼
public List<ShopResource> batchinsertCallId(List<ShopResource> shopResourceList) { this.getSqlSession().insert(getStatement(SQL_BATCH_INSERT_CALL_ID), shopResourceList); return shopResourceList;// 重點介紹 }
為什么最后返回的參數不是挑用mybatis后的insert的返回值呢,細心的話可以發現,如果使用debug模式觀察,會看到調用mybatis后insert的返回值是[],也就是空集合元素,在mybatis3.3.1中,雖然加入了批量新增返回主鍵id的功能,但是它是這樣運行的,在需要新增插入新元素集合對象時,它會需要參數對象,當執行完插入操作后,給之前的參數對象設置id值,也就是改變了需要插入對象集合中的元素的屬性id值, 所以接收返回時,返回方法形參參數即可,同樣的地址引用改變了內容,返回后的集合也是改變后的集合。
---------------------
原文:https://blog.csdn.net/zhaoraolin/article/details/78946340