简体   繁体   中英

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

I am trying the following in Java

@Insert("INSERT INTO USERS (ID,NAME,AGE) VALUES(USER_SEQ.NEXTVAL,#{name},#{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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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