2017-06-11 21 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); 
    } 
}