jpashop : 컬렉션 조회 v4 (jpa - dto 직접조회)
This commit is contained in:
@@ -4,6 +4,8 @@ import com.example.jpashop.domain.Order;
|
||||
import com.example.jpashop.domain.OrderItem;
|
||||
import com.example.jpashop.repository.OrderRepository;
|
||||
import com.example.jpashop.repository.OrderSearch;
|
||||
import com.example.jpashop.repository.order.query.OrderQueryDto;
|
||||
import com.example.jpashop.repository.order.query.OrderQueryRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -17,6 +19,7 @@ import java.util.stream.Collectors;
|
||||
public class OrderApiController {
|
||||
|
||||
private final OrderRepository orderRepository;
|
||||
private final OrderQueryRepository orderQueryRepository;
|
||||
|
||||
@GetMapping("/api/v1/orders")
|
||||
public List<Order> ordersV1() {
|
||||
@@ -55,4 +58,9 @@ public class OrderApiController {
|
||||
.map(OrderDto::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@GetMapping("/api/v4/orders")
|
||||
public List<OrderQueryDto> ordersV4() {
|
||||
return orderQueryRepository.findOrderQueryDtos();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.jpashop.repository.order.query;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class OrderItemQueryDto {
|
||||
|
||||
private Long orderId;
|
||||
private String itemName;
|
||||
private int orderPrice;
|
||||
private int count;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.jpashop.repository.order.query;
|
||||
|
||||
import com.example.jpashop.domain.Address;
|
||||
import com.example.jpashop.domain.OrderStatus;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class OrderQueryDto {
|
||||
|
||||
private Long orderId;
|
||||
private String name;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
private Address address;
|
||||
private List<OrderItemQueryDto> orderItems;
|
||||
|
||||
public OrderQueryDto(Long orderId, String name, LocalDateTime orderDate, OrderStatus orderStatus, Address address) {
|
||||
this.orderId = orderId;
|
||||
this.name = name;
|
||||
this.orderDate = orderDate;
|
||||
this.orderStatus = orderStatus;
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.example.jpashop.repository.order.query;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
public class OrderQueryRepository {
|
||||
|
||||
private final EntityManager em;
|
||||
|
||||
public List<OrderQueryDto> findOrderQueryDtos() {
|
||||
List<OrderQueryDto> result = findOrders();
|
||||
|
||||
result.forEach(o -> {
|
||||
List<OrderItemQueryDto> orderItems = findOrderItems(o.getOrderId());
|
||||
o.setOrderItems(orderItems);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<OrderItemQueryDto> findOrderItems(Long orderId) {
|
||||
return em.createQuery(
|
||||
"select new com.example.jpashop.repository.order.query.OrderItemQueryDto(oi.order.id, i.name, oi.orderPrice, oi.count)" +
|
||||
" from OrderItem oi" +
|
||||
" join oi.item i" +
|
||||
" where oi.order.id = :orderId", OrderItemQueryDto.class)
|
||||
.setParameter("orderId", orderId)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
private List<OrderQueryDto> findOrders() {
|
||||
return em.createQuery(
|
||||
"select new com.example.jpashop.repository.order.query.OrderQueryDto(o.id, m.name, o.orderDate, o.status, d.address)" +
|
||||
" from Order o" +
|
||||
" join o.member m" +
|
||||
" join o.delivery d", OrderQueryDto.class)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user