Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#13 API 고급 - 실무 필수 최적화 #15

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MemberApiController {
public CreateMemberResponse saveMemberV1(@RequestBody @Valid Member member){
// @RequestBody : JSON으로 온 body를 Member에 그대로 매핑해서 넣어준다.

Long id = memberService.join(member);
Long id = memberService.join(member); // OSIV OFF라면 이렇게 반환된 후엔 영속성 컨텍스트도 끝, 데이터베이스 커넥션도 끝
return new CreateMemberResponse(id);
}

Expand Down
16 changes: 5 additions & 11 deletions jpashop/src/main/java/jpabook/jpashop/api/OrderApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jpabook.jpashop.repository.order.query.OrderQueryDto;
import jpabook.jpashop.repository.order.query.OrderQueryRepository;
import jpabook.jpashop.repository.OrderSearch;
import jpabook.jpashop.service.query.OrderQueryService;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -62,21 +63,14 @@ public List<OrderDto> ordersV2() {
return result;
}

private final OrderQueryService orderQueryService;

// 엔티티를 조회해 DTO로 반환
// 쿼리 수 최적화 + 페이징 X
// 컬렉션까지 한 번에 fetch join
@GetMapping("/api/v3/orders")
public List<OrderDto> ordersV3() {
List<Order> orders = orderRepository.findAllWithItem();

for (Order order : orders) {
System.out.println("order ref="+ order + " id="+ order.getId());
}
List<OrderDto> result = orders.stream()
.map(o -> new OrderDto(o))
.collect(toList());

return result;
public List<jpabook.jpashop.service.query.OrderDto> ordersV3() {
return orderQueryService.ordersV3();
}

// 엔티티를 조회해 DTO로 반환
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class MemberService {
* 회원 가입
*/
@Transactional // 읽기 전용인 기능이 많으니까 전체를 readOnly = true로 해두고 이 기능만 false로
// 영속성 컨텍스트 만들어지고 데이터베이스 커넥션 가져오고
// OSIV OFF라면 로직이 끝난 후 데이터 베이스 커넥션 플러시 커밋하고 영속성 컨텍스트를 없애고 데이터 베이스 커넥션 반환
public Long join(Member member){
validateDuplicateMember(member); // 중복 회원 검증
memberRepository.save(member);
Expand Down
42 changes: 42 additions & 0 deletions jpashop/src/main/java/jpabook/jpashop/service/query/OrderDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jpabook.jpashop.service.query;

import jpabook.jpashop.api.OrderApiController;
import jpabook.jpashop.domain.Address;
import jpabook.jpashop.domain.Order;
import jpabook.jpashop.domain.OrderItem;
import jpabook.jpashop.domain.OrderStatus;
import lombok.Data;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;

import static java.util.stream.Collectors.toList;

@Getter
public class OrderDto {

private Long orderId;
private String name;
private LocalDateTime orderDate;
private OrderStatus orderStatus;
private Address address;
private List<OrderItemDto> orderItems;
// List<OrderItem>이면 DTO 안에 엔티티가 있는 상황.
// 엔티티에 대한 의존을 완전히 끊어야 한다.
// OrderItem도 DTO로 바꿔야 한다.

public OrderDto(Order order) {
orderId = order.getId();
name = order.getMember().getName();
orderDate = order.getOrderDate();
orderStatus = order.getStatus();
address = order.getDelivery().getAddress();
orderItems = order.getOrderItems().stream()
.map(orderItem -> new OrderItemDto(orderItem))
.collect(toList());
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jpabook.jpashop.service.query;

import jpabook.jpashop.domain.OrderItem;
import lombok.Getter;

@Getter
class OrderItemDto {

private String itemName;
private int orderPrice;
private int count;
public OrderItemDto(OrderItem orderItem) {
itemName = orderItem.getItem().getName();
orderPrice = orderItem.getOrderPrice();
count = orderItem.getCount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package jpabook.jpashop.service.query;

import jpabook.jpashop.api.OrderApiController;
import jpabook.jpashop.domain.Order;
import jpabook.jpashop.repository.OrderRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static java.util.stream.Collectors.toList;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class OrderQueryService {
private final OrderRepository orderRepository;
public List<OrderDto> ordersV3() {
List<Order> orders = orderRepository.findAllWithItem();

for (Order order : orders) {
System.out.println("order ref="+ order + " id="+ order.getId());
}
List<OrderDto> result = orders.stream()
.map(o -> new OrderDto(o))
.collect(toList());

return result;
}
}
1 change: 1 addition & 0 deletions jpashop/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ spring:
driver-class-name: org.h2.Driver

jpa:
# open-in-view: false
hibernate:
ddl-auto: create
# create : 애플리케이션 실행 시점에 테이블 드롭하고 다시 만든다.
Expand Down