myBatis插入操作获取不到返回的自增id问题

myBatis插入操作后想返回自增 id 有多种方式

其中一种使用率较高的就是:

在<insert></insert> 标签中添加 useGeneratedKeys 和 keyProperty 属性

具体操作可以看我另一篇博客

但是就是没有返回出来,结果是因为 我在 mapper 接口中入参时使用了 @Param 注解

当使用了 @Param 注解后,想把 insert 插入操作成功后的自增 id 返回出来,需要在

keyProperty 设置值时,添加 @Param("xxx") 括号中定义的别名

例:

Mapper 接口:

Long insertStudent(@Param("data")Student student);

Mapper xml:

<insert id="insertStudent" parameterType="com.test.Student" useGeneratedKeys="true" keyProperty="data.id">

</insert>

这样在业务逻辑层调用 mapper 接口后,在入参的对象中就可以 get 到对应的自增 id 了!

希望能够帮助到你

over

猜你喜欢

转载自blog.csdn.net/qq_41402200/article/details/83995470