2017-06-11 84 views
0

如何更新資料庫? 我寫了它更新春季資料

@Transactional 
@Modifying 
@Query("UPDATE Product SET description = :description, inStock = :inStock, name = :name, price = :price WHERE id = :id") 

void updateProduct(@Param("description") String description, @Param("inStock") int inStock, @Param("name") String name, @Param("price") int price, @Param("id") int id); 

雖然我認爲這是不好的。 這是很好的做法? 如何做得更好? 如何將完整對象轉移到查詢?

回答

1

沒有必要創建自定義的方法,只需要用你的資料庫save()方法,例如:

@Service 
public class ProductService { 

    @Autoware 
    private ProductRepository productRepository; 

    @Transaction 
    public void updatePrice(Product product, int price) { 
     product.setPrice(price); 
     productRepository.save(product); 
    } 
}