ItemController.java
@GetMapping(value = "/item/{itemId}")
public String itemDtl(Model model, @PathVariable("itemId") Long itemId){
ItemFormDto itemFormDto = itemService.getItemDtl(itemId);
model.addAttribute("item", itemFormDto);
return "item/itemDtl";
}
ItemService.java
- @Transactional(readOnly = true) 설정. JPA가 더티체킹(변경감지)을 수행하지 않아서 성능을 향상
- itemImgRepository를 이용하여 해당상품 이미지 조회, 등록순가져오기 위해 아이디 오름차순으로 정렬
- .of 메소드를 이용하여 엔티티 객체에서 DTO 객체로 전환
- itemRepository를 이용하여 해당상품 데이터 조회
@Transactional(readOnly = true)
public ItemFormDto getItemDtl(Long itemId){
List<ItemImg> itemImgList = itemImgRepository.findByItemIdOrderByIdAsc(itemId);
List<ItemImgDto> itemImgDtoList = new ArrayList<>();
for(ItemImg itemImg : itemImgList){
ItemImgDto itemImgDto = ItemImgDto.of(itemImg);
itemImgDtoList.add(itemImgDto);
}
Item item = itemRepository.findById(itemId).orElseThrow(EntityNotFoundException::new);
ItemFormDto itemFormDto = ItemFormDto.of(item);
itemFormDto.setItemImgDtoList(itemImgDtoList);
return itemFormDto;
}
ItemImgDto.java
public static ItemImgDto of(ItemImg itemImg){
return modelMapper.map(itemImg, ItemImgDto.class);
}
ItemFormDto.java
public static ItemFormDto of(Item item){
return modelMapper.map(item, ItemFormDto.class);
}
itemDtl.html
<div layout:fragment="content" style="margin-left:25%;margin-right:25%">
<input type="hidden" id="itemId" th:value="${item.id}">
<div class="d-flex">
<div class="repImgDiv">
<img th:src="${item.itemImgDtoList[0].imgUrl}" class = "rounded repImg" th:alt="${item.itemNm}">
</div>
<div class="wd50">
<span th:if="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}" class="badge badge-primary mgb-15">
판매중
</span>
<span th:unless="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}" class="badge btn-danger mgb-15" >
품절
</span>
<div class="h4" th:text="${item.itemNm}"></div>
<hr class="my-4">
<div class="text-right">
<div class="h4 text-danger text-left">
<input type="hidden" th:value="${item.price}" id="price" name="price">
<span th:text="${item.price}"></span>원
</div>
<div class="input-group w-50">
<div class="input-group-prepend">
<span class="input-group-text">수량</span>
</div>
<input type="number" name="count" id="count" class="form-control" value="1" min="1">
</div>
</div>
<hr class="my-4">
<div class="text-right mgt-50">
<h5>결제 금액</h5>
<h3 name="totalPrice" id="totalPrice" class="font-weight-bold"></h3>
</div>
<div th:if="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}" class="text-right">
<button type="button" class="btn btn-light border border-primary btn-lg" onclick="addCart()">장바구니 담기</button>
<button type="button" class="btn btn-primary btn-lg" onclick="order()">주문하기</button>
</div>
<div th:unless="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}" class="text-right">
<button type="button" class="btn btn-danger btn-lg">품절</button>
</div>
</div>
</div>
<div class="jumbotron jumbotron-fluid mgt-30">
<div class="container">
<h4 class="display-5">상품 상세 설명</h4>
<hr class="my-4">
<p class="lead" th:text="${item.itemDetail}"></p>
</div>
</div>
<div th:each="itemImg : ${item.itemImgDtoList}" class="text-center">
<img th:if="${not #strings.isEmpty(itemImg.imgUrl)}" th:src="${itemImg.imgUrl}" class="rounded mgb-15" width="800">
</div>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
</div>
반응형
'2022 > JPA입문(完)' 카테고리의 다른 글
상품관리 (0) | 2022.02.06 |
---|---|
등록된 상품수정 (0) | 2022.02.04 |
상품 등록 (0) | 2022.02.04 |
엔티티 관계도 / 엔티티 클래스 다이어그램 (0) | 2022.02.04 |
스프링 시큐리티 - 2 (0) | 2022.01.24 |