SPRING #7 - BUYLIST

김형우·2022년 4월 4일
0

Spring #2

목록 보기
7/8

ShopController

  • 버튼 두개가 submit 이므로 name을 btn으로 통일하고 value값에 따라 수행하는 명령이 다르게 처리 되도록 조건을 정한다.
    : if (btn.equals("장바구니"))
    : else if (btn.equals("주문하기"))

@PostMapping(value = "/cart")
public String cartPOST(
        @RequestParam(name = "code") long code,
        @RequestParam(name = "ccnt") long ccnt,
        @RequestParam(name = "btn") String btn) {
    String em = (String) httpSession.getAttribute("M_EMAIL");
    if (em == null) {
        // 로그인 되지 않았다면

		// aop 설명
        // 되돌아 올수 있도록 url 정보를 세션에 저장
        // 로그인 후 다시 돌아오기 위함
        // 이걸 모든페이지에 다 넣을래? ㄴㄴ
        // 그래서 스프링 기능 사용
        // 컨트롤러로 진입하는 통로에 해당 코드를 짜넣는다.
        // 컨트롤러 진입하는 통로 = 필터개념
        // 컨트롤러로 진입할건데 이런이런 컨트롤러는 이런 필터를 거쳐서 진입하세요 같은거        
        // httpSession.setAttribute("BACKURL", "/shop/cart");
        // 작동 안해서 interceptor 사용
        return "redirect:/member/login";
    }
    if (btn.equals("장바구니")) {
        // 로그인 되었다면
        // System.out.println(code);
        // System.out.println(ccnt);
        // System.out.println(em);
        CartDTO cart = new CartDTO();
        cart.setCcnt(ccnt);
        cart.setIcode(code);
        cart.setUemail(em);

        int ret = cMapper.insertCartOne(cart);
        System.out.println("cart => " + cart);
        System.out.println(ret);
        // if (ret == 1) {
        // return "redirect:/shop/home";
        // }
        // return "redirect:/shop/detail?code=" + code;
        return "redirect:/shop/cartlist";
    } else if (btn.equals("주문하기")) {
        BuyDTO buy = new BuyDTO();
        buy.setBcnt(ccnt);
        buy.setIcode(code);
        buy.setUemail(em);
        int ret = bMapper.insertBuyOne(buy);
        System.out.println("buy => " + buy);
        System.out.println("ret => " + ret);
        return "redirect:/member/buylist";
    }
    return "redirect:/";
}

MemberController.java

  • 주문목록 GET
  • 리턴타입 Map
    : Join해서 VIEW로 만든 임의의 테이블을 조회하기때문에 해당하는 DTO가 없어서 MAP으로 받는다
  • 조회 한 VIEW를 model로 html에 던진다
    : model.addAttribute("list", list);
@GetMapping(value = "/buylist")
public String buylistGET(Model model) {
    String em = (String) httpSession.getAttribute("M_EMAIL");
    if (em == null) {
        // 로그인 되지 않았다면
        return "redirect:/member/login";
    }
    // 로그인 되었을때
    List<Map<String, Object>> list = bMapper.selectBuyListMap(em);
    System.out.println("list => " + list);
    model.addAttribute("list", list);
    return "/shop/buylist";
}

BuyMapper.java

  • 미리 VIEW를 생성한다
    : BUYLIST_VIEW
CREATE OR REPLACE VIEW BUYLIST_VIEW AS
    SELECT 
    	M.UEMAIL, M.UNAME, M.UPHONE, 
        I.ICODE, I.INAME, I.IPRICE, 
        B.BNO, B.BCNT, B.BREGDATE 
    FROM 
    	MEMBER M, ITEM I, BUY B
    WHERE 
    	M.UEMAIL = B.UEMAIL AND I.ICODE = B.ICODE;
  • Mapper => @Select
    : 위에서 생성한 VIEW를 조회한다.
// 주문목록 조회
// 리턴은 DTO가 아닌 MAP 컬렉션을 이용함
// VIEW로 만들어서 해당하는 DTO가 없기때문
@Select({
        "SELECT * FROM BUYLIST_VIEW WHERE UEMAIL=#{em}"
})
public List<Map<String, Object>> selectBuyListMap(
        @Param(value = "em") String em);

buylist.html

  • 테이블에 던지면 끝!
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>주문내역</title>
</head>
<body>
    <h3>주문내역</h3>
    <hr>   
    <table>
        <tr>
            <th>주문번호</th>
            <th>물품명</th>
            <th>주문수량</th>
            <th>물품가격</th>
            <!-- <th>물품코드</th> -->
            <th>주문일</th>
            <th>주문자명</th>
            <th>주문자연락처</th>
            <th>가격합계</th>
        </tr>
        <tr th:each="list : ${list}">
            <td th:text="${list.BNO}"></td>
            <td th:text="${list.INAME}"></td>
            <td th:text="${list.BCNT}"></td>
            <td th:text="${list.IPRICE}"></td>
            <!-- <td th:text="${list.ICODE}"></td> -->
            <td th:text="${list.BREGDATE}"></td>
            <td th:text="${list.UNAME}"></td>
            <td th:text="${list.UPHONE}"></td>
            <td th:text="${list.IPRICE}*${list.BCNT}"></td>
        </tr>
    </table>
    
    
</body>
</html>
profile
The best

0개의 댓글