简体   繁体   English

如何在MyBatis中使用generateId插入?

[英]How to insert using generatedId in MyBatis?

When I tried to insert a data into table, I always get a Exception said id is NULL . 当我尝试向表中插入数据时,总是会收到一个ExceptionidNULL Here is the code: 这是代码:

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

Also I tried to add 我也尝试添加

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

But still got the error. 但是仍然出现错误。 Then I run the sql( SELECT SCOPE_IDENTITY() ) and get a null. 然后我运行sql( SELECT SCOPE_IDENTITY() )并得到一个null。

The environment is: MyBatis 3.4.0 , SQL Server 2008 , Spring-Boot 环境是: MyBatis 3.4.0SQL Server 2008Spring-Boot

尝试在@SelectKey批注中设置before=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM