[Study] 6회차 미션
in Study
6회차 미션을 진행해보자!
전체 코드는 여기에서 확인할 수 있다.
JPA 세팅을 해주면서 ProductRepository
인터페이스가 JpaRepository
를 상속받아 사용받게 해주었다.
public interface ProductRepository extends JpaRepository<ProductEntity, Integer>
이 선언만으로 기존에 하나하나 작성했던 메서드들을 이제는 JPA에서 제공하는 것들을 가져다 사용만 하면 된다.
대표적으로 findAll()
, findById()
등이 있는데, 정렬 등 쿼리를 직접 짜지 않아도 JPA 메소드 명명법에 따르면 간편하게 구현할 수 있다.
여기에서 사용법을 확인할 수 있었다.
기존에 구현해뒀던 상품 조회, 등록, 상세 조회 등을 JPA 메소드로 바꾸어주었고, 포스트맨으로 요청을 보내면 콘솔에서 쿼리 실행을 확인할 수 있다.
# insert
Hibernate:
insert
into
product_entity
(name, price)
values
(?, ?)
MySQL bench에서 등록된 상품들을 확인할 수 있다.
# select by name, by id, by price
Hibernate:
select
productent0_.id as id1_0_,
productent0_.name as name2_0_,
productent0_.price as price3_0_
from
product_entity productent0_
where
productent0_.name=?
Hibernate:
select
productent0_.id as id1_0_0_,
productent0_.name as name2_0_0_,
productent0_.price as price3_0_0_
from
product_entity productent0_
where
productent0_.id=?
Hibernate:
select
productent0_.id as id1_0_,
productent0_.name as name2_0_,
productent0_.price as price3_0_
from
product_entity productent0_
where
productent0_.price=?
추가로 dto의 변환을 controller에서 담당하도록 결정했다. entity가 노출되지 않아야 한다는 점도 중요하지만 유연성을 위해 너무 엄격히 제한할 필요성은 없지 않을까 싶어서 우선은 이렇게 결론내렸다.