본문 바로가기

스프링 (인프런)/스프링부트

(16)
스프링 데이터 JPA 소개 정의 - 스프링 데이터 JPA는 JPA를 사용할 때 지속적으로 반복되는 코드를 줄여준다. - 자세한 건 https://spring.io/guides/gs/accessing-data-jpa/ Getting Started | Accessing Data with JPA You can run the application from the command line with Gradle or Maven. You can also build a single executable JAR file that contains all the necessary dependencies, classes, and resources and run that. Building an executable jar makes it easy to shi..
OSIV와 성능 최적화 1. 정의 - 이름 - 하이버네이트 : Open Session in View - JPA : Open EntityManager in View 2023-07-16 11:54:43.810 WARN 36992 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning - 스프링을 시작할 때마다 WARN 로그가 지속적으로 발생한다. -..
API 개발 고급 정리 1. 개발 순서 엔티티 조회 - 엔티티를 조회해서 그대로 반환 : V1 - 엔티티 조회 후 DTO로 변환하여 반환 : V2 - fetch join으로 쿼리 수 최적화 : V3 - 컬렉션과 페이징 한계 돌파 : V3.1 - 컬렉션은 fetch join하면 중복 데이터 때문에 페이징 불가 - 따라서, - toOne 관계는 fetch join 유지 - toMany 관계, 즉 컬렉션은 지연 로딩은 유지한 채 default_batch_fetch_size를 활용 DTO 직접 조회 - JPA에서 DTO를 직접 조회 : V4 - 컬렉션 조회 최적화 : V5 - 일대다 관계인 컬렉션은 WHERE IN절을 활용해서 메모리에 미리 조회해서 최적화 - 플랫 데이터 최적화 : V6 - 컬렉션 포함 모든 관계와의 join 결과를..
API 개발 고급 - 컬렉션 조회 최적화 2 1. 주문 조회 V4 : JPA에서 DTO 직접 조회 @GetMapping("/api/v4/orders") public List ordersV4() { return orderQueryRepository.findOrderQueryDTOs(); } - 심플 오더 컨트롤러에서 했던 것처럼, 쿼리에서 DTO 객체를 생성해서 엔티티의 필드를 바로 옮기는 방식이다. - 다만, 컬렉션을 추가해야 한다는 점에 차이가 있다. - OrderQueryRepository를 따로 파서 findOrderQueryDTOs()를 호출하여 위임하고, OrderQueryDTO를 리스트로 반환한다. - 심플 오더 컨트롤러 때와 동일하다. - order 패키지에 query 패키지를 파서 리포지토리와 DTO를 몰아줬다. - 참고로, DTO를..
API 개발 고급 - 컬렉션 조회 최적화 1 0. 들어가며 - 저번 장은 toOne 관계에 관해서만 다뤘다. - 이번 장은 OneToMany 관계에 대해 다룬다. 1. 주문 조회 V1 : 엔티티 직접 노출 @RestController @RequiredArgsConstructor public class OrderApiController { private final OrderRepository orderRepository; @GetMapping("/api/v1/orders") public List ordersV1() { List orders = orderRepository.findAllByString(new OrderSearch()); for (Order order : orders) { order.getMember().getName(); order.g..
API 개발 고급 - 지연 로딩과 조회 성능 최적화 0. 목표 - 주문 + 배송정보 + 회원을 조회하는 API 제작. - 지연 로딩 때문에 발생하는 성능 문제를 단계적으로 해결. 1. 간단한 주문 조회 V1: 엔티티를 직접 노출 @RestController @RequiredArgsConstructor public class OrderSimpleApiController { private final OrderRepository orderRepository; @GetMapping("/api/v1/simple-orders") public List ordersV1() { List all = orderRepository.findAllByString(new OrderSearch()); return all; } } - "/api/v1/simple-orders"에 GET..
API 개발 고급 - 준비 1. 조회용 샘플 데이터 입력 @Component @RequiredArgsConstructor public class InitDB { private final InitService initService; @PostConstruct public void init() { initService.dbInit1(); initService.dbInit2(); } @Component @Transactional @RequiredArgsConstructor static class InitService { private final EntityManager em; public void dbInit1() { Member member = createMember("userA", new Address("seoul", "1", "1..
API 개발 기본 1. 회원 등록 API Postman 설치 - https://www.postman.com Postman API Platform | Sign Up for Free Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster. www.postman.com - 여기서 postman을 다운받아야 한다. MemberApiController - v1 @RestController @RequiredArgsConstructor public class MemberApiContr..