MyBatis: How to return the ID of the inserted object under Postgres?

…衆ロ難τιáo~ 提交于 2019-12-12 10:55:04

问题


I've got a postgres table where the ID is defined as bigserial. How can I use @Insert and get back the id of the inserted entity? I am expecting the mapper method to either return the id or populate the id field inside the entity object. Any ideas?


回答1:


NVM, i think i found the answer on the other thread, http://mybatis-user.963551.n3.nabble.com/How-to-return-the-ID-of-the-inserted-object-under-Postgres-td1926959.html

There's the link for anyone else who lands here.




回答2:


The mapper will return you the number of records that were actually inserted. In order to get back the id of the inserted record, you'll need to add a second annotation (that will populate the id) :

@Options(useGeneratedKeys=true, keyProperty="idSomething")

Note that keyProperty is not necessary if the identifiyng property is named "id" in your entity object.



来源:https://stackoverflow.com/questions/4260428/mybatis-how-to-return-the-id-of-the-inserted-object-under-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!