簡體   English   中英

如何在MyBatis中使用generateId插入?

[英]How to insert using generatedId in MyBatis?

當我嘗試向表中插入數據時,總是會收到一個ExceptionidNULL 這是代碼:

@Insert({
    "insert into device (tag)",
    "values (#{tag,jdbcType=VARCHAR})"
})
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insert(Device record);

我也嘗試添加

@SelectKey(before = true, keyColumn = "id", 
    keyProperty = "id", 
    resultType = Integer.class, 
    statement = "SELECT SCOPE_IDENTITY()")

但是仍然出現錯誤。 然后我運行sql( SELECT SCOPE_IDENTITY() )並得到一個null。

環境是: MyBatis 3.4.0SQL Server 2008Spring-Boot

嘗試在@SelectKey批注中設置before=false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM