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