欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis批量插入返回插入成功后的主鍵id操作

 更新時間:2021年03月30日 10:13:56   作者:蔣老濕  
這篇文章主要介紹了Mybatis批量插入返回插入成功后的主鍵id操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我們都知道Mybatis在插入單條數(shù)據(jù)的時候有兩種方式返回自增主鍵:

1、對于支持生成自增主鍵的數(shù)據(jù)庫:增加 useGenerateKeys和keyProperty ,<insert>標(biāo)簽屬性。

2、不支持生成自增主鍵的數(shù)據(jù)庫:使用<selectKey>。

但是怎么對批量插入數(shù)據(jù)返回自增主鍵的解決方式網(wǎng)上看到的還是比較少,至少百度的結(jié)果比較少。

Mybatis官網(wǎng)資料提供如下:

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>


 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>


 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>

從官網(wǎng)資料可以看出Mybatis是支持批量插入時返回自增主鍵的。

但是在本地測試的時候使用上述方式確實不能返回自增id,而且還報錯(不認(rèn)識keyProperty中指定的Id屬性),然后在網(wǎng)上找相關(guān)資料。終于在Stackoverflow上面找到了一些信息。

解決辦法:

1、升級Mybatis版本到3.3.1。官方在這個版本中加入了批量新增返回主鍵id的功能

2、在Dao中不能使用@param注解。

3、Mapper.xml中使用list變量(parameterType="java.util.List")接受Dao中的參數(shù)集合。

下面是具體代碼過程,可供參考

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實現(xiàn)層代碼

public List<ShopResource> batchinsertCallId(List<ShopResource> shopResourceList)
 {
 this.getSqlSession().insert(getStatement(SQL_BATCH_INSERT_CALL_ID), shopResourceList);
 return shopResourceList;// 重點介紹
 }
 

補充:MyBatis 插入的同時獲取主鍵id

有時候進行一些多步操作的時候就需要得到最新插入一條記錄的id號,那么如何在插入的同時返回id號

Mapper代碼:

<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層:

當(dāng)設(shè)置了useGeneratedKeys="true" keyProperty="id"后,它會在你插入數(shù)據(jù)庫的同時,將這個對象的id值改為最新的那個id,然后我們只需要取出他就可以了

最終效果:

數(shù)據(jù)庫

輸出

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • 一篇文章帶你搞定SpringBoot不重啟項目實現(xiàn)修改靜態(tài)資源

    一篇文章帶你搞定SpringBoot不重啟項目實現(xiàn)修改靜態(tài)資源

    這篇文章主要介紹了一篇文章帶你搞定SpringBoot不重啟項目實現(xiàn)修改靜態(tài)資源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • MyBatis-Plus updateById不更新null值的方法解決

    MyBatis-Plus updateById不更新null值的方法解決

    用Mybatis-Plus的updateById()來更新數(shù)據(jù)時,無法將字段設(shè)置為null值,更新后數(shù)據(jù)還是原來的值,本文就來詳細(xì)的介紹一下解決方法,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • 使用springboot對外部靜態(tài)資源文件的處理操作

    使用springboot對外部靜態(tài)資源文件的處理操作

    這篇文章主要介紹了使用springboot對外部靜態(tài)資源文件的處理操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • maven 環(huán)境變量的配置詳解

    maven 環(huán)境變量的配置詳解

    這篇文章主要介紹了maven 環(huán)境變量的配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java工具類DateUtils實例詳解

    Java工具類DateUtils實例詳解

    這篇文章主要為大家詳細(xì)介紹了Java工具類DateUtils實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • SpringBoot整合ELK實現(xiàn)日志監(jiān)控

    SpringBoot整合ELK實現(xiàn)日志監(jiān)控

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合ELK實現(xiàn)日志監(jiān)控的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • java使用RSA與AES加密解密的實例代碼詳解

    java使用RSA與AES加密解密的實例代碼詳解

    這篇文章主要介紹了java使用RSA與AES加密解密的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • 利用Spring?boot+LogBack+MDC實現(xiàn)鏈路追蹤

    利用Spring?boot+LogBack+MDC實現(xiàn)鏈路追蹤

    這篇文章主要介紹了利用Spring?boot+LogBack+MDC實現(xiàn)鏈路追蹤,MDC?可以看成是一個與當(dāng)前線程綁定的哈希表,可以往其中添加鍵值對,下文詳細(xì)介紹需要的小伙伴可以參考一下
    2022-04-04
  • Java中的浮點數(shù)分析

    Java中的浮點數(shù)分析

    Java中的浮點數(shù)分析...
    2006-12-12
  • Java使用FTPClient類讀寫FTP

    Java使用FTPClient類讀寫FTP

    這篇文章主要為大家詳細(xì)介紹了Java使用FTPClient類讀寫FTP的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評論