mybatis插入自增主键返回

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mutou_ren/article/details/82721113

对于支持主键自增的sql而言,如mysql、sql server 如下方法:

<insert id="addBook" parameterType="Book" useGeneratedKeys="true" keyProperty="book.bookId">

添加

useGeneratedKeys="true" keyProperty="book.bookId"

其中keyProperty对应实体类属性,如果使用@Parm注解,要采用book.bookId的形式。

命令执行后返回的仍然是影响的行数,但book对象的id已经被自动set为自增后的id值,可以由id属性的get方法得到。

猜你喜欢

转载自blog.csdn.net/Mutou_ren/article/details/82721113
今日推荐