코딩/Spring 쇼핑몰 프로젝트

Spring [쇼핑몰프로젝트] - 장바구니 페이지 구현 - 오류 기록[31]

유미._. 2023. 5. 24. 17:29
728x90

 

 

 

오늘은 지난번 장바구니 페이지 오류 수정에 대한 포스팅을 해보겠습니다 (´。_。`)

 

 

구현 순서

 

1. 오류 발생 상황

2. 해결 방법

  ① cart 테이블 수정

  ② cartMapper.xml 수정

  ③ cartMapperTests.java 수정

  ④ cartMapperTests 테스트

  ⑤ 최종 테스트

 

 

1. 오류 발생 상황

 

- 콘솔창에는 값이 제대로 전달 되었으나, 웹 페이지에선 값이 전달 되지 않는 상황이 발생하였습니다.

 

원인 : 테이블에 칼럼값이 생성되지 않아서 값이 넘어오지 않았습니다.

 

 

2. 해결 방법

 

① cart 테이블 삭제 및 수정

 

- cart 테이블과 시퀀스를 삭제해 줍니다.

 

drop table cart;

drop SEQUENCE CART_SEQ;

 

- cart 테이블과 시퀀스를 생성해 줍니다.

product_name, product_price, product_discount, totalPrice, integerPrice의 칼럼을 추가해 줍니다. 

product_name의 크기는 VARCHAR2(100)으로 값을 넉넉하게 주어야 나중에 오류가 발생하지 않습니다.

 

CREATE TABLE CART(
    CART_NUM NUMBER(10) PRIMARY KEY,
    memberId VARCHAR2(20) CONSTRAINT CART_memberId_FK REFERENCES MEMBER(memberId) ON DELETE CASCADE,
    pno NUMBER(38) NULL CONSTRAINT CART_PRODUCT_NUM_FK REFERENCES product_tbl(pno) ON DELETE CASCADE,
    AMOUNT NUMBER(5),
    product_name VARCHAR2(100),
    product_price VARCHAR2(20),
    product_discount VARCHAR2(20),
    totalPrice NUMBER(38),
    integerPrice NUMBER(38)
);

CREATE SEQUENCE CART_SEQ;

 

 

② cartMapper.xml 수정

 

- cartMapper.xml의 일부분을 수정해 줍니다. (카트 추가와 카트 목록만 수정해 줍니다.)

(카트 삭제와 카트 수량 수정과 카트 확인은 수정할 부분이 없습니다.)

 

 

- 카트 추가

5개의 컬럼 값에 대한 정보를 추가적으로 입력해 줍니다.

(product_name, product_price, product_discount, totalPrice, integerPrice)

 

	<!-- 카트추가 -->
	<insert id="addCart">
		insert into cart(cart_num, memberId, pno, amount, product_name, product_price, product_discount, totalPrice, integerPrice)
		values(cart_seq.nextval, #{memberId}, #{pno}, #{amount}, #{product_name}, #{product_price}, #{product_discount}, #{totalPrice}, #{integerPrice})
	</insert>

 

 

- 카트 목록을 수정해 줍니다.

추가된 칼럼 값을 삽입해 줍니다.

이때 "," 오타에 주의합시다!!

 

	<!-- 카트 목록 -->
	<select id="getCart" resultType="com.angel.model.CartDTO">
		select a.cart_num, a.memberId, a.pno, a.amount, a.product_name, a.product_price, a.product_discount, a.totalPrice, a.integerPrice, b.pno
		from cart a left outer join product_tbl b on a.pno = b.pno
		where memberId = #{memberId}
	</select>

 

 

 

③ cartMapperTests.java 수정

 

- addCart 메서드 수정

product_name, product_price, product_discount, totalPrice, integerPrice 관련 코드들을 넣어 줍니다.

 

	@Test
	public void addCart() throws Exception {
		
		String memberId = "지현";
		Long pno = 101L;
		int amount = 10;
		String product_name = "주혜";
		String product_price = "100만원";
		double product_discount = 123.123;
		int totalPrice = 12345;
		int integerPrice = 54321;
		
		CartDTO cart = new CartDTO();
		cart.setMemberId(memberId);
		cart.setPno(pno);
		cart.setAmount(amount);
		cart.setProduct_name(product_name);
		cart.setProduct_price(product_price);
		cart.setProduct_discount(product_discount);
		cart.setTotalPrice(totalPrice);
		cart.setIntegerPrice(integerPrice);
		
		int result = 0;
		
		result = mapper.addCart(cart);
		
		log.info("결과 : " + result );
	}

 

 

④ cartMapperTests 테스트

 

각 테스트 메스드들의 Junit 테스트를 진행 해보고, 테이블에 값이 잘 들어가는지와 콘솔창에 값이 잘 나오는지 확인해 줍니다.

 

 

addCart 테스트 ↓↓↓

 

 

 

deleteCart 테스트 ↓↓↓

 

 

 

modifyCount 테스트 ↓↓↓

 

 

 

getCart 테스트 ↓↓↓

 

 

 

checkCart 테스트 ↓↓↓

 

 

 

⑤ 최종 테스트

 

프로그램을 실행 시켜 준 뒤, 상품을 장바구니에 넣고 장바구니 페이지에 기존에 들어오지 않았던 product_name, product_price, product_discount의 값이 잘 들어 왔는지 확인해 줍니다.

 

 

 

장바구니 페이지 ↓↓↓

 

 

 

 

 

 

 

이상입니당 (∩^o^)⊃━☆

 

 

 

728x90
반응형