ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 240201_Springboot 쇼핑몰 실습
    카테고리 없음 2024. 2. 1. 17:04

     


    DB에 Company  필드 추가
    create database shop default character set utf8 collate utf8_general_ci
    CREATE TABLE shop (
    	company VARCHAR(255)
    );

     

    메인 페이지

    //main.html
    
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{layouts/layout1}">
    
        ...생략
    
        <div class="row">
            <th:block th:each="item, status: ${items.getContent()}">
                <div class="col-md-4 margin">
                    <div class="card">
                        <a th:href="'/item/' +${item.id}" class="text-dark">
                            <img th:src="${item.imgUrl}" class="card-img-top" th:alt="${item.itemNm}" height="400">
                            <div class="card-body">
                                <h4 class="card-title">[[${item.itemNm}]]</h4>
                                <p class="card-text">제조사 : [[${item.company}]]</p>
                                <p class="card-text">[[${item.itemDetail}]]</p>
                                <h3 class="card-title text-danger">[[${item.price}]]원</h3>
                            </div>
                        </a>
                    </div>
                </div>
            </th:block>
        </div>
    
        ...생략
    
    </div>
    <--MainItemDto-->
    
    package com.shop.dto;
    
    import com.querydsl.core.annotations.QueryProjection;
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter @Setter
    public class MainItemDto {
    
        private Long id;
        private String itemNm;
        private String itemDetail;
        private String imgUrl;
        private String company;
        private Integer price;
    
        @QueryProjection
        public MainItemDto(Long id, String itemNm, String itemDetail, String imgUrl,Integer price, String company){
            this.id = id;
            this.itemNm = itemNm;
            this.itemDetail = itemDetail;
            this.imgUrl = imgUrl;
            this.price = price;
            this.company = company;
        }
    
    }
    <--ItemRepositoryCustomImpl-->
    package com.shop.repository;
    
        ...생략
    
        @Override
        public Page<MainItemDto> getMainItemPage(ItemSearchDto itemSearchDto, Pageable pageable) {
            QItem item = QItem.item;
            QItemImg itemImg = QItemImg.itemImg;
    
            List<MainItemDto> content = queryFactory
                    .select(
                            new QMainItemDto(
                                    item.id,
                                    item.itemNm,
                                    item.itemDetail,
                                    itemImg.imgUrl,
                                    item.price,
                                    item.company
                            )
                    )
        ...생략
        }
    
    }

    상세 페이지

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{layouts/layout1}">
    
    ...생략
                <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>
                <br>
                <p>제조사: <span th:text="${item.company}"></span> </p>
                <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>
                ...생략
    </html>

    상품등록

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{layouts/layout1}">
    
        ...생략
    
            <!--제조사  추가-->
    
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">제조사</span>
                </div>
                <input type="text" th:field="*{company}" class="form-control" placeholder="제조사를 입력해주세요">
            </div>
            <p th:if="${#fields.hasErrors('company')}" th:errors="*{company}" class="fieldError">Incorrect data</p>
    
            <!--제조사  추가-->
    
        ...생략
    <--item-->
    
    package com.shop.entity;
    
    import ...
    
    @Entity
    @Table(name="item")
    @Getter
    @Setter
    @ToString
    public class Item extends BaseEntity {
        
        ...생략
    
        @Column(nullable = false, length = 50)
        private String company; //제조사
    
    }
    <--ItemDto-->
    package com.shop.dto;
    
    import lombok.Getter;
    import lombok.Setter;
    
    import java.time.LocalDateTime;
    
    @Getter
    @Setter
    public class ItemDto {
    
        ...생략
    
        private LocalDateTime updateTime;
    
        private String company;
    
    }
    <--ItemFormDto.java-->
    
    package com.shop.dto;
    
    import ...
    
    @Getter @Setter
    public class ItemFormDto {
    
        private Long id;
    
        @NotBlank(message = "제조사는 필수 입력 값입니다.")
        private String company;
    
        ...생략
        
        }
    
        public static ItemFormDto of(Item item){
            return modelMapper.map(item,ItemFormDto.class);
        }
    
    }

    상품관리

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{layouts/layout1}">
    
        ...생략
    
        <form th:action="@{'/admin/items/' + ${items.number}}" role="form" method="get" th:object="${items}">
            <table class="table">
                <thead>
                <tr>
                    <td>상품아이디</td>
                    <td>상품명</td>
                    <td>제조사</td>
                    <td>상태</td>
                    <td>등록자</td>
                    <td>등록일</td>
                </tr>
                </thead>
                <tbody>
                <tr th:each="item, status: ${items.getContent()}">
                    <td th:text="${item.id}"></td>
                    <td>
                        <a th:href="'/admin/item/'+${item.id}" th:text="${item.itemNm}"></a>
                    </td>
                    <td th:text="${item.company}"></td>
                    <td th:text="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL} ? '판매중' : '품절'"></td>
                    <td th:text="${item.createdBy}"></td>
                    <td th:text="${item.regTime}"></td>
                </tr>
                </tbody>
            </table>
    
            <...생략
    
    </html>

    장바구니

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{layouts/layout1}">
    
            ...생략
            <td class="d-flex">
                <div class="repImgDiv align-self-center">
                    <img th:src="${cartItem.imgUrl}" class = "rounded repImg" th:alt="${cartItem.itemNm}">
                </div>
                <div class="align-self-center">
                    <span th:text="${cartItem.itemNm}" class="fs24 font-weight-bold"></span>
                    <span th:text="${cartItem.company}" class="fs24 font-weight-bold"></span> //추가
                    <div class="fs18 font-weight-light">
                        <span class="input-group mt-2">
                            <span th:id="'price_' + ${cartItem.cartItemId}"
                                  th:data-price="${cartItem.price}"
                                  th:text="${cartItem.price} + '원'" class="align-self-center mr-2">
                            </span>
                            <input type="number" name="count" th:id="'count_' + ${cartItem.cartItemId}"
                                   th:value="${cartItem.count}" min="1"
                                   onchange="changeCount(this)" class="form-control mr-2" >
                            <button type="button" class="close" aria-label="Close">
                                <span aria-hidden="true" th:data-id="${cartItem.cartItemId}" 
                                onclick="deleteCartItem(this)">&times;</span>
                            </button>
                        </span>
                    </div>
                </div>
            </td>
            ...생략
    </html>
    package com.shop.dto;
    
    import ...
    
    @Getter @Setter
    public class CartDetailDto {
    
        ...생략
    
        private String company;
    
        public CartDetailDto(Long cartItemId, String itemNm, int price, int count, 
        String imgUrl, String company){
            this.cartItemId = cartItemId;
            this.itemNm = itemNm;
            this.price = price;
            this.count = count;
            this.imgUrl = imgUrl;
            this.company = company;
        }
    
    }

     

    package com.shop.repository;
    
    import ...
    
    public interface CartItemRepository extends JpaRepository<CartItem, Long> {
    
        CartItem findByCartIdAndItemId(Long cartId, Long itemId);
        @Query("select new com.shop.dto.CartDetailDto(ci.id, i.itemNm, i.price, 
        ci.count, im.imgUrl, i.company) " +
                ...생략
                )
        List<CartDetailDto> findCartDetailDtoList(Long cartId);
    }

    구매이력

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{layouts/layout1}">
    
            ...생략
            <div class="align-self-center w-75">
                <span th:text="${orderItem.itemNm}" class="fs24 font-weight-bold"></span>
                <span th:text="${orderItem.company}" class="fs24 font-weight-bold"></span>
                <div class="fs18 font-weight-light">
                    <span th:text="${orderItem.orderPrice} +'원'"></span>
                    <span th:text="${orderItem.count} +'개'"></span>
                </div>
            </div>
            ...생략
    </html>
    package com.shop.dto;
    
    import ...
    
    @Getter @Setter
    public class OrderItemDto {
    
        public OrderItemDto(OrderItem orderItem, String imgUrl){
            this.itemNm = orderItem.getItem().getItemNm();
            this.count = orderItem.getCount();
            this.orderPrice = orderItem.getOrderPrice();
            this.imgUrl = imgUrl;
            this.company = orderItem.getItem().getCompany(); //추가
        }
    
        private String itemNm; //상품명
        private int count; //주문 수량
    
        private int orderPrice; //주문 금액
        private String imgUrl; //상품 이미지 경로
    
        private String company; //제조사
    
    }
    package com.shop.entity;
    
    import lombok.Getter;
    import lombok.Setter;
    import jakarta.persistence.*;
    
    @Entity
    @Getter @Setter
    public class OrderItem extends BaseEntity {
    
        @Id @GeneratedValue
        @Column(name = "order_item_id")
        private Long id;
    
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "item_id")
        private Item item;
    
        @ManyToOne(fetch = FetchType.LAZY) //추가
        @JoinColumn(name = "company") //추가
        private Item company; //추가
    
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "order_id")
        private Order order;
    
        private int orderPrice; //주문가격
    
        private int count; //수량
        public static OrderItem createOrderItem(Item item, int count){
            OrderItem orderItem = new OrderItem();
            orderItem.setItem(item);
            orderItem.setCount(count);
            orderItem.setOrderPrice(item.getPrice());
            item.removeStock(count);
            return orderItem;
        }
        public int getTotalPrice(){
            return orderPrice*count;
        }
    
        public void cancel() {
            this.getItem().addStock(count);
        }
    
    
    }

    shop-master.zip
    9.64MB

     

    원하는 곳에 추가를 하기 위해서는 어디에 무엇을 추가해야 하는지 이해하는 것이 중요하다. 

Designed by Tistory.