Spring Data JPA是更大的Spring Data系列的一部分,可以輕鬆實現基於JPA的存儲庫。此模塊處理對基於JPA的數據訪問層的增強支持。它使構建使用數據訪問技術的Spring驅動應用程序變得更加容,這給我們帶來方便快捷,但是也有一些坑,例如用 @Query修改數據
如果你用下面接口修改會報錯
@Query(value = "update test SET stal = :stal where id= :id", nativeQuery = true) Integer updateBystal(@Param("stal")int stal, @Param("id")Integer id);
這樣執行會報錯,這事因爲@Query是默認查詢,不會執行修改語句,我們想要其執行修改語句,需要加上@Modifying註解,說明這個方法是修改,代碼如下
@Modifying @Query(value = "update test SET stal = :stal where id= :id", nativeQuery = true) Integer updateBystal(@Param("stal")int stal, @Param("id")Integer id);