1. 程式人生 > >mybatis 插入一條資料返回相應資料ID

mybatis 插入一條資料返回相應資料ID

<insert id="insertTaxiOrder" parameterType="map" useGeneratedKeys="true" keyProperty="ID" > INSERT INTO tb_TaxiOrderInfo ( UserID, DriverID, StartAddress, EndAddress,PublicTime,StartLongitude,StartLatitude ) VALUES (#{userID}, 0, #{StartAdd}, #{EndAdd}, GETDATE(), #{Longitude}, #{Latitude} )

其中加上 useGeneratedKeys=”true” keyProperty=“ID”

這個意思就是說。 他在插入後, 會自動把插入的資料生成的ID

塞入到你這個 keyProperty 後面設定的 ID 這個引數裡面。

執行之後 你直接取ID 的值就好了。

我這裡接收的 parameterType 是 map。 

所以在 之前 map 要有這個 key

如果接收的是 實體類的話, 當然實體類裡面就要定義ID 這個屬性了