1.直接調用save方法
2.自定義 參考 How do I update an entity using spring-data-jpa?
@Query:自定義sql
@Modifying:告訴spring-data-jps 這個sql是更新操作,需要用 executeUpdate()
而不是 executeQuery()
.
@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
@Modifying
@Query("update Fruit f set f.name=:name where f.color=:color")
void updateFruits(@Param("name") String name, @Param("color") String color);
}
返回值int
: the number of records being updated.boolean
: true if there is a record being updated. Otherwise, false.