MyBatis插入資料後返回主鍵id

2021-10-20 20:10:37 字數 931 閱讀 8039

最近開發全棧專案時,前端有個資料行可以被隨便修改,所以必須給他乙個標識記錄該資料行,即向mysql資料庫中插入一條記錄後,需要獲取此條記錄的主鍵id值返回給前端。

"insertarticle"

parametertype

="article"

>

insert into article(title,create_date,content,name)

values(#,#,#,#)

insert

>

這種方式只是返回乙個影響行數值,並不能滿足此次需求,於是做了如下修改:

<

insert id=

"insertarticle" usegeneratedkeys=

"true" keyproperty=

"id" parametertype=

"article"

>

insert

into article(title,create_date,content,name)

values

(#,#,#,#)

<

/insert

>

僅適用於 insert 和 update,這會令 mybatis 使用 jdbc 的 getgeneratedkeys 方法來取出由資料庫內部生成的主鍵(比如mysql的自動遞增主鍵字段),預設值:false。所以要開啟設為 true

僅適用於 insert 和 update,指定能夠唯一識別物件的屬性,mybatis 會使用 getgeneratedkeys 的返回值或 insert 語句的 selectkey 子元素設定它的值,預設值:未設定(unset)。如果生成列不止乙個,可以用逗號分隔多個屬性名稱。

這裡我們要開啟就需要指定為article物件的id。

MyBatis返回插入的資料主鍵

在mybatis開發過程中,我們經常需要解決乙個問題 當插入一條資料的時候,我在接下來的操作中我需要知道剛剛插入的是哪條資料,這樣我們需要獲取插入資料的主鍵 如何在mybatis中解決這個問題呢?首先,我這裡有兩張表,其中計算層數表和計算參數列有這一對多的關係,層次編號在計算層數表中為主鍵在計算參數...

關於mybatis實現插入後返回id的方法

在插入資料後,有時候會想返回插入的資料,但是id好像是不能的,現在來介紹mybatis插入後返回id的方法。mysql下,id為自增型別時,插入前加入 select last insert id select last insert id insert into top line user id,t...

ibatis插入資料後返回id

oracle資料庫 首先看配置 insert into operation id,name,desc values operation seq.nextval,name desc select operation seq.currval as id from dual 首先來解釋下id,這個是對應的...