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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM