jpashop : 컬렉션 조회 v6 (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.OrderFlatDto;
|
||||
import com.example.jpashop.repository.order.query.OrderItemQueryDto;
|
||||
import com.example.jpashop.repository.order.query.OrderQueryDto;
|
||||
import com.example.jpashop.repository.order.query.OrderQueryRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -14,6 +16,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.*;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class OrderApiController {
|
||||
@@ -68,4 +72,16 @@ public class OrderApiController {
|
||||
public List<OrderQueryDto> ordersV5() {
|
||||
return orderQueryRepository.findAllByDto_optimization();
|
||||
}
|
||||
|
||||
@GetMapping("/api/v6/orders")
|
||||
public List<OrderQueryDto> ordersV6() {
|
||||
List<OrderFlatDto> flats = orderQueryRepository.findAllByDto_flat();
|
||||
|
||||
return flats.stream()
|
||||
.collect(groupingBy(o -> new OrderQueryDto(o.getOrderId(), o.getName(), o.getOrderDate(), o.getOrderStatus(), o.getAddress()),
|
||||
mapping(o -> new OrderItemQueryDto(o.getOrderId(), o.getItemName(), o.getOrderPrice(), o.getCount()), toList())
|
||||
)).entrySet().stream()
|
||||
.map(e -> new OrderQueryDto(e.getKey().getOrderId(), e.getKey().getName(), e.getKey().getOrderDate(), e.getKey().getOrderStatus(), e.getKey().getAddress(), e.getValue()))
|
||||
.collect(toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.jpashop.repository.order.query;
|
||||
|
||||
import com.example.jpashop.domain.Address;
|
||||
import com.example.jpashop.domain.OrderStatus;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class OrderFlatDto {
|
||||
|
||||
private Long orderId;
|
||||
private String name;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
private Address address;
|
||||
|
||||
private String itemName;
|
||||
private int orderPrice;
|
||||
private int count;
|
||||
|
||||
public OrderFlatDto(Long orderId, String name, LocalDateTime orderDate, OrderStatus orderStatus, Address address, String itemName, int orderPrice, int count) {
|
||||
this.orderId = orderId;
|
||||
this.name = name;
|
||||
this.orderDate = orderDate;
|
||||
this.orderStatus = orderStatus;
|
||||
this.address = address;
|
||||
this.itemName = itemName;
|
||||
this.orderPrice = orderPrice;
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,13 @@ import com.example.jpashop.domain.Address;
|
||||
import com.example.jpashop.domain.OrderStatus;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(of = "orderId")
|
||||
public class OrderQueryDto {
|
||||
|
||||
private Long orderId;
|
||||
@@ -25,4 +27,13 @@ public class OrderQueryDto {
|
||||
this.orderStatus = orderStatus;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public OrderQueryDto(Long orderId, String name, LocalDateTime orderDate, OrderStatus orderStatus, Address address, List<OrderItemQueryDto> orderItems) {
|
||||
this.orderId = orderId;
|
||||
this.name = name;
|
||||
this.orderDate = orderDate;
|
||||
this.orderStatus = orderStatus;
|
||||
this.address = address;
|
||||
this.orderItems = orderItems;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,18 @@ public class OrderQueryRepository {
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<OrderFlatDto> findAllByDto_flat() {
|
||||
return em.createQuery(
|
||||
"select new com.example.jpashop.repository.order.query.OrderFlatDto(o.id, m.name, o.orderDate, o.status, d.address, i.name, oi.orderPrice, oi.count)" +
|
||||
" from Order o" +
|
||||
" join o.member m" +
|
||||
" join o.delivery d" +
|
||||
" join o.orderItems oi" +
|
||||
" join oi.item i", OrderFlatDto.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
|
||||
private Map<Long, List<OrderItemQueryDto>> findOrderItemMap(List<Long> orderIds) {
|
||||
List<OrderItemQueryDto> orderItems = em.createQuery(
|
||||
"select new com.example.jpashop.repository.order.query.OrderItemQueryDto(oi.order.id, i.name, oi.orderPrice, oi.count)" +
|
||||
|
||||
Reference in New Issue
Block a user