오늘은 장바구니 기능 구현에 대한 첫번째 포스팅을 해보겠습니다 ╰(*°▽°*)╯1
구현 순서
1. 테이블 생성
2. CartDTO
1. 테이블 생성
장바구니 구현을 위해 테이블을 새로 만들어 줍니다.
시퀀스도 같이 생성해 줍니다.
ON DELETE CASCADE 관련 설명 : https://y-umi.tistory.com/43
CREATE TABLE CART(
CART_NUM NUMBER(10) PRIMARY KEY,
memberId VARCHAR2(20) CONSTRAINT CART_memberId_FK REFERENCES MEMBER(memberId) ON DELETE CASCADE,
PRODUCT_NUM NUMBER(38) NULL CONSTRAINT CART_PRODUCT_NUM_FK REFERENCES product_tbl(pno) ON DELETE CASCADE,
AMOUNT NUMBER(5)
);
CREATE SEQUENCE CART_SEQ;
cart 테이블의 memberId가 member 테이블의 memeberId를 참조하고 있고,
cart 테이블의 product_num이 product_tbl테이블의 product_num을 참조하고 있습니다.
2. CartDTO
com.angel.model 패키지에 CartDTO를 생성해 줍니다.
장바구니와 관련된 변수들을 선언해 주었습니다
@Data 어노테이션을 사용해서 getter와 setter, tostring 메서드를 자동으로 생성해 줄 수 있지만 sale_price와 total_price는 setter 메서드를 생성해 주지 않기 위해 우클릭 -> source -> generate getters and setters 를 통해 만들어 줍니다.
우클릭 -> source -> generate toString()을 통해 toString을 생성해 줍니다.
sale_price와 total_price를 초기화 해주는 메서드를 생성해 줍니다.
sale_price와 total_price의 변수 값을 변경 하기 위해선 오직 이 메서드를 통해서만 변경이 가능하도록 하기 위해 위에서 sale_price와 total_price 메서드를 setter 메서드에 추가해주지 않았습니다.
CartDTO 전체 ↓↓↓
package com.angel.model;
public class CartDTO {
//DB에 있는 변수
private int cart_num;
private String memberId;
private Long product_num;
private int amount;
//product
private String product_name;
private int product_price;
private double product_discount;
private int sale_price;
private int total_price;
public int getCart_num() {
return cart_num;
}
public void setCart_num(int cart_num) {
this.cart_num = cart_num;
}
public String getMemberId() {
return memberId;
}
public void setMemberId(String memberId) {
this.memberId = memberId;
}
public Long getProduct_num() {
return product_num;
}
public void setProduct_num(Long product_num) {
this.product_num = product_num;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public int getProduct_price() {
return product_price;
}
public void setProduct_price(int product_price) {
this.product_price = product_price;
}
public double getProduct_discount() {
return product_discount;
}
public void setProduct_discount(double product_discount) {
this.product_discount = product_discount;
}
//sale_price와 total_price 초기화
public void initSaleTotal() {
this.sale_price = (int)(this.product_price * (1-this.product_discount));
this.total_price = this.sale_price * this.amount;
}
@Override
public String toString() {
return "CartDTO [cart_num=" + cart_num + ", memberId=" + memberId + ", product_num=" + product_num + ", amount="
+ amount + ", product_name=" + product_name + ", product_price=" + product_price + ", product_discount="
+ product_discount + ", sale_price=" + sale_price + ", total_price=" + total_price + "]";
}
}
이상입니당 (∩^o^)⊃━☆
참고 : Kim VamPa (tistory.com) 👍
'코딩 > Spring 쇼핑몰 프로젝트' 카테고리의 다른 글
Spring [쇼핑몰프로젝트] - 장바구니 구현[26] (0) | 2023.05.17 |
---|---|
Spring [쇼핑몰프로젝트] - 장바구니 구현[25] (0) | 2023.05.16 |
Spring [쇼핑몰프로젝트] - 인터셉터 적용[23] (0) | 2023.05.12 |
Spring [쇼핑몰프로젝트] - GNB 영역 구현[22] (0) | 2023.05.04 |
Spring [쇼핑몰프로젝트] - GNB 영역 구현[21] (0) | 2023.05.03 |