16) Spring Data JPA 는?
17) Spring Data JPA 예제
@Entity
public class Product extends Timestamped {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;
private Long userId;
private String title;
private String image;
private String link;
private int lprice;
private int myprice;
}public interface ProductRepository extends JpaRepository<Product, Long> {
}// 1. 상품 생성
Product product = new Product(...);
productRepository.save(product);
// 2. 상품 전체 조회
List<Product> products = productRepository.findAll();
// 3. 상품 전체 개수 조회
long count = productRepository.count();
// 4. 상품 삭제
productRepository.delete(product);public interface ProductRepository extends JpaRepository<Product, Long> {
// (1) 회원 ID 로 등록된 상품들 조회
List<Product> findAllByUserId(Long userId);
// (2) 상품명이 title 인 관심상품 1개 조회
Product findByTitle(String title);
// (3) 상품명에 word 가 포함된 모든 상품들 조회
List<Product> findAllByTitleContaining(String word);
// (4) 최저가가 fromPrice ~ toPrice 인 모든 상품들을 조회
List<Product> findAllByLpriceBetween(int fromPrice, int toPrice);
}