我們都知道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:
1
2
3
4
5
6
7
8
9
10
11
12
|
<insert id= "insertauthor" usegeneratedkeys= "true" keyproperty= "id" > insert into author (username,password,email,bio) values (#{username},#{password},#{email},#{bio}) </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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<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> 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層代碼
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- 批量新增 --> <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實現層代碼
1
2
3
4
5
|
public list<shopresource> batchinsertcallid(list<shopresource> shopresourcelist) { this .getsqlsession().insert(getstatement(sql_batch_insert_call_id), shopresourcelist); return shopresourcelist; // 重點介紹 } |
補充:mybatis 插入的同時獲取主鍵id
有時候進行一些多步操作的時候就需要得到最新插入一條記錄的id號,那么如何在插入的同時返回id號
mapper代碼:
1
2
3
4
5
6
7
8
9
10
11
12
|
<insert id= "insertfeeds" parametertype= "com.yj.pojo.feeds" usegeneratedkeys= "true" keyproperty= "id" > insert into feeds (id, title, content, pic, video, auther_id, comments, favours, likes, cover_select, views, set_time, kind) values (#{id,jdbctype=integer}, #{title,jdbctype=varchar}, #{content,jdbctype=varchar}, #{pic,jdbctype=varchar}, #{video,jdbctype=varchar}, #{autherid,jdbctype=varchar}, #{comments,jdbctype=integer}, #{favours,jdbctype=integer}, #{likes,jdbctype=integer}, #{coverselect,jdbctype=integer}, #{views,jdbctype=integer}, #{settime,jdbctype=varchar}, #{kind,jdbctype=varchar}) </insert> |
service層:
當設置了usegeneratedkeys="true" keyproperty="id"后,它會在你插入數據庫的同時,將這個對象的id值改為最新的那個id,然后我們只需要取出他就可以了
最終效果:
數據庫
輸出
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/jiangeeq/article/details/55047116