开发者

How to return IDs on insert in mybatis and oracle with annotation

开发者 https://www.devze.com 2023-04-12 11:09 出处:网络
I am trying the following in Java @Insert(\"INSERT INTO USERS (ID,NAME,AGE) VALUES(USER_SEQ.NEXTVAL,#{name开发者_JAVA百科},#{age})\")

I am trying the following in Java

@Insert("INSERT INTO USERS (ID,NAME,AGE) VALUES(USER_SEQ.NEXTVAL,#{name开发者_JAVA百科},#{age})")
@Options(useGeneratedKeys=true, keyProperty="ID", keyColumn="ID")
public int insertUsers(User userBean);

It should return the new genarated ID, but its returning "1" always even though its making insertion into table in a proper way.

Can any one have tryied this "Getting IDs in return or insertion in MyBatis(annotation) with oracle"


Read the MyBatis Documentation.

The keyProperty is the field that MyBatis will set the key into by getGeneratedKeys, or by a selectKey child element of the insert statement.

So, given a Pojo with a field "id" with get and set methods. After the insert statement with the Mapper class is ran, the id field on the pojo will be set with the generated key value.


Thanks all for your responses, but i have got the solution here it is.....

@Insert("INSERT INTO USERS (NAME,AGE) VALUES(#{name},#{age})") 
@SelectKey(statement="select STANDARDS_ID_SEQ.CURRVAL from dual", resultType = int.class, before = false, keyProperty = ID)
@Options(useGeneratedKeys=true, keyProperty="ID", keyColumn="ID")

now it will return the new created ID

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号