MyBatis 3在Insert之后返回主键

XML:

<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">

注解:

@Insert("your sql goes here")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insert(FileAttachment fileAttachment) throws Exception;

说明:

返回的主键值会直接赋值到实体,而不是方法体的返回值。

 

参考:

https://stackoverflow.com/questions/12103606/get-the-id-of-last-inserted-record-in-mybatis

posted @ 2017-10-14 09:56  EasonJim  阅读(811)  评论(0编辑  收藏  举报