1. 程式人生 > >Mybatis插入資料後返回主鍵id

Mybatis插入資料後返回主鍵id

有時候使用mybatis插入資料後,需要用到記錄在資料庫中的自增id,可以利用keyProperty來返回,賦值給實體類中的指定欄位。

單條記錄插入並返回

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> 
新增useGeneratedKeys="true" 以及keyProperty="id"即可。id為實體類中的欄位名稱

多條記錄插入並返回


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沒有返回的情況。檢查以下幾點:

1、Mybatis版本3.3.1及其以上。

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

3、Mapper.xml中使用list變數接受Dao中的集合。

相關推薦

Mybatis插入資料返回id

有時候使用mybatis插入資料後,需要用到記錄在資料庫中的自增id,可以利用keyProperty來返回,賦值給實體類中的指定欄位。 單條記錄插入並返回 First, if your database supports auto-generated key fields (e.g. MySQL and SQ

MYSQL插入資料返回

最近做專案,要求不能使用mybatis等框架,所以一切資料庫訪問操作都是用原生的JDBC。 用過mybatis的都知道,插入新資料後可以直接返回主鍵的值,但是使用JDBC不行,於是上網搜了一下,找到一個還蠻好用的方法: SELECT LAST_INSERT_ID();

Mybatis_插入資料返回ID_返回資料欄位與類中欄位相對應

目的:利用mapper 向資料庫中插入記錄,並返回主鍵ID  注意返回資料的名字需要與類中欄位相對應上 若不對應上會出現以下錯誤: JavaBean XXXMapper.xml中的SQL  正

Mybatis 插入資料返回的方法

mysql插入資料後獲得主鍵 針對自增主鍵的表,在插入時不需要主鍵,而是在插入過程自動獲取一個自增的主鍵,比如MySQL, <insert id="add" parameterType="vo.Category" useGeneratedKeys="t

Oracle 在插入資料獲取ID的解決辦法

Mybatis批量插入返回自增主鍵:  對於支援生成自增主鍵的資料庫:useGenerateKeys和keyProperty 不支援生成自增主鍵的資料庫:<selectKey> 1、Oracle資料庫中建立自增序列 create sequence ARCHI

mybatis 新增資料返回的兩種寫法

第一種:加上  useGeneratedKeys="true" keyProperty="id"<insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="id" parameterType="com

MyBatis插入記錄時返回id的方法

str 字段 () into tty ava 通過 vat 進行   有時候插入記錄之後需要使用到插入記錄的主鍵,通常是再查詢一次來獲取主鍵,但是MyBatis插入記錄時可以設置成返回主鍵id,簡化操作,方法大致有兩種。 對應實體類: 1 public cla

mybatis插入資料返回(mysql資料庫)

第一種方式使用useGeneratedKeys屬性 User類 public class User { private i

Mybatis批量插入返回插入成功id

我們都知道Mybatis在插入單條資料的時候有兩種方式返回自增主鍵: 1、對於支援生成自增主鍵的資料庫:增加 useGenerateKeys和keyProperty ,<insert>標籤屬性。 2、不支援生成自增主鍵的資料庫:使用<selectKe

mybatis單條和批量插入返回插入成功id

有些時候我們在新增記錄成功後希望能直接獲取到該記錄的主鍵id值,而不需要再執行一次查詢操作。在使用mybatis作為ORM元件時,可以很方便地達到這個目的。鑑於mybatis目前已經支援xml配置和註解2種方式,所以分別給予詳細介紹。使用xml配置方式1.xml配置:<!-- 插入資料:返回記錄的id值

mybatis 插入資料返回id

正常插入資料後返回型別為int,現在想要得到新新增資料的id解決辦法 在mapper.xml中修改如下程式碼 before <insert id="insert" parameterType="com.entity.xxx"> after <insert id

xorm插入資料庫返回自增id

golang使用xorm連線資料庫後,插入結構體,無法返回自增主鍵id,飯後的主鍵id都是0。經過研究發現,如果給結構體id設定xorm tag,則會預設id為0,不會返回插入成功後的主鍵id。 xorm文件中如下描述   1 package main 2 3 import

MyBatis在insert插入操作時返回ID的配置

很多時候,在向資料庫插入資料時,需要保留插入資料的id,以便進行後續的update操作或者將id存入其他表作為外來鍵。 但是,在預設情況下,insert操作返回的是一個int值,並且不是表示主鍵id,而是表示當前SQL語句影響的行數。。。 接下來,我們看看MyBatis如

Mybatis+Mysql 批量插入的時候返回ID

<insert id="insertAlarmLinkmanList" useGeneratedKeys="true" keyProperty="alarmLinkmanId" > insert into alarm_linkman (user_name,

MyBatis 在insert插入操作時返回ID的配置

很多時候,在向資料庫插入資料時,需要保留插入資料的id,以便進行後續的update操作或者將id存入其他表作為外來鍵。 但是,在預設情況下,insert操作返回的是一個int值,並且不是表示主鍵i

Oracle 在函數或存儲過程中執行一條插入語句並返回ID

num c# 如果 acl get col oracle style 建表語句 有時,我們需要往一張表插入一條記錄,同時返回主鍵ID值。 假定主鍵ID的值都是通過對應表的SEQUENCE來獲得,然後進行ID賦值 這裏有幾種情況需要註意: 1)如果建表語句含有主鍵ID的觸發器

Mybatis新增記錄,返回id

<insert id="addRole" parameterType="SysRole" useGeneratedKeys="true" keyProperty="roleId" keyColumn="role_id"> &nb

mybatis插入記錄獲得主id

今天做練習的時候發現了mybatis的一個坑 在進行插入操作後,我們需要獲得新插入記錄的主鍵id,一般來說有兩種方法 <insert id="insertBlog" keyProperty="blogId" useGeneratedKeys="true" parame

Mybatis中insert返回

需求:使用MyBatis往MySQL資料庫中插入一條記錄後,需要返回該條記錄的自增主鍵值。 方法:在mapper中指定keyProperty屬性,示例如下: Xml程式碼   <insert id="insertAndGetId" useGeneratedKe

mysql 插入資料返回自增 ID 的七種方法

   2. 因為 LAST_INSERT_ID 是基於 Connection 的,只要每個執行緒都使用獨立的 Connection 物件,LAST_INSERT_ID 函式 將返回該 Connection 對 AUTO_INCREMENT列 最新的 insert or update* 作生成的第一個 reco