Spring 2022/04/01 Shop, ItemImage(2번용), Cart

무간·2022년 4월 1일
0

파일명 CartDTO.java

package com.example.dto;

import java.util.Date;

import lombok.Data;

@Data
public class CartDTO {
    // 카트번호
  private Long cno;
  // 수량
  private Long ccnt;
  // 등록일
  private Date cregdate;
  // 물품코드
  private Long icode;
  // 이메일
  private String uemail;
}

파일명 shop/home.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 style="padding: 10px;">
    <h3>쇼핑몰 페이지 입니다.</h3>

    <hr />

    <h3>등록일순</h3>
    <div style="padding: 20px;">       
        <th:block th:each="tmp, idx : ${list1}" >
            <div style="display: inline-block; 
                text-align:center;
                border:1px solid #cccccc; 
                padding:20px; width:140px;">
                <a th:href="@{/shop/detail(code=${tmp.icode})}">
                    <img th:src="@{/item/image(code=${tmp.icode})}" style=" width: 50px; height: 50px;"><br />
                    코드 <span th:text="${tmp.icode}"></span><br />
                    이름 <span th:text="${tmp.iname}"></span><br />
                    가격 <span th:text="${tmp.iprice}"></span><br />
                </a>
            </div>
            <th:block th:if=" ${idx.count %4 ==0 }">
                <br />
            </th:block>
        </th:block>        
    </div>    

    <h3>물품명순</h3>
    <div style="padding: 20px;">       
        <th:block th:each="tmp, idx : ${list2}" >
              
            <div style="display: inline-block; 
                text-align:center;
                border:1px solid #cccccc; 
                padding:20px; width:140px;">
                <a th:href="@{/shop/detail(code=${tmp.icode})}">
                    <img th:src="@{/item/image(code=${tmp.icode})}" style="width: 50px; height: 50px;"><br />
                    코드 <span th:text="${tmp.icode}"></span><br />
                    이름 <span th:text="${tmp.iname}"></span><br />
                    가격 <span th:text="${tmp.iprice}"></span><br />
                </a>          
            </div>
            <th:block th:if=" ${idx.count %4 ==0 }">
                <br />
            </th:block>
        </th:block>        
    </div>    

    <h3>가격순</h3>
    <div style="padding: 20px;">       
        <th:block th:each="tmp, idx : ${list3}" >
            <div style="display: inline-block; 
                text-align:center;
                border:1px solid #cccccc; 
                padding:20px; width:140px;">
                <a th:href="@{/shop/detail(code=${tmp.icode})}">
                    <img th:src="@{/item/image(code=${tmp.icode})}" style="width: 50px; height: 50px;"><br />
                    코드 <span th:text="${tmp.icode}"></span><br />
                    이름 <span th:text="${tmp.iname}"></span><br />
                    가격 <span th:text="${tmp.iprice}"></span><br />       
                </a> 
            </div>
            <th:block th:if=" ${idx.count %4 ==0 }">
                <br />
            </th:block>
        </th:block>        
    </div>    

</body>
</html>

파일명 shpo/detail.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 style="padding: 10px;">
    <h3>물품상세</h3>

    <hr />
    <form th:action="@{/shop/cart}" method="post">
        <div style="padding:20px">
            <label style="width:75px; height: 30px; display:inline-block;">이미지 :</label>
            <img th:src="@{/item/image(code=${obj.icode})}" style="width: 200px; height: 200px;"><br />

            <label style="width:75px; height: 30px; display:inline-block;">물품번호 :</label>
            <P style="display:inline-block" th:text="${obj.icode}"></P><br />

            <label style="width:75px; height: 30px; display:inline-block;">물품명 :</label>
            <P style="display:inline-block" th:text="${obj.iname}"></P><br />

            <label style="width:75px; height: 30px; display:inline-block;">물품내용 :</label>
            <P style="display:inline-block" th:text="${obj.icontent}"></P><br />

            <label style="width:75px; height: 30px; display:inline-block;">물품가격 :</label>
            <P style="display:inline-block" th:text="${obj.iprice}"></P><br />

            <label style="width:75px; height: 30px; display:inline-block;">물품수량 :</label>
                    
            <select name="cnt">
                <th:block th:if="${obj.iquantity < 200 }">
                    <option th:each="i : ${#numbers.sequence(1,obj.iquantity)}" 
                            th:text="${i}" th:value="${i}">
                    </option>
                </th:block>

                <th:block th:if="${obj.iquantity >= 200 }">
                    <option th:each="i : ${#numbers.sequence(1,200)}" 
                            th:text="${i}" th:value="${i}">
                    </option>
                </th:block>
            </select> <br />

            <th:block th:each="tmp : ${list}">
                <span th:text="${tmp}"></span>
                <img th:src="@{/item/subimage(imgcode=${tmp}) }" style="width: 50px; height: 50px;"/>
            </th:block> <br />
            
                <input type="hidden" th:value="${obj.icode}" name="code">
                <input type="submit" value="장바구니" />
                <input type="submit" value="주문하기" />
            <hr />
        </form>
        <br />
        <table border="1">
            <tr>
                <th>물품이미지코드</th>
                <th>이미지</th>
                <th>버튼</th>
            </tr>
            <tr th:each="tmp : ${list}">
                <td th:text="${tmp}"></td>
                <td><img th:src="@{/item/subimage(imgcode=${tmp}) }" style="width: 50px; height: 50px;"/></td>
                <td>수정 삭제</td>
            </tr>
        </table>
    </div>
</body>
</html>

파일명 ShopController.java

package com.example.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.example.dto.ItemDTO;
import com.example.mapper.ItemImageMapper;
import com.example.mapper.ItemMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/shop")
public class ShopController {

    @Autowired ItemMapper iMapper;
    @Autowired ItemImageMapper iiMapper;
    @Autowired HttpSession httpSession;

    @PostMapping(value = "cart")
    public String cartPOST(
        @RequestParam(name = "code") long code,
        @RequestParam(name = "cnt") long cnt        
    ){
        String em = (String)httpSession.getAttribute("M_EMAIL");
        if(em != null){
            System.out.println(cnt);
            System.out.println(code);
            System.out.println(em); // 로그인 되었을때는 이메일
        }
        else{
            System.out.println(cnt);
            System.out.println(code);
            System.out.println(httpSession.getId()); // 로그인 되지 않았을때는 고유한 값(ex 세션의 키)
        }
        return "redirect:/shop/detail?code="+ code;
    }

    // 127.0.0.1:9090/ROOT/shop/detail?code=12
    @GetMapping(value = "detail")
    public String detailGET(
            Model model,
            @RequestParam(name = "code")long code
    ){
        ItemDTO item = iMapper.selectItemOne(code);
        model.addAttribute("obj", item);

        List<Long> list = iiMapper.selectItmeImageCodeList(code);
        model.addAttribute("list", list);
        return "/shop/detail";
    }

    // 127.0.0.1:9090/ROOT/shop/home
    @GetMapping(value = {"/","/home"})
    public String shopGET(Model model){
        // 등록일
        List<ItemDTO> list1 = iMapper.selectItemList(1);
        model.addAttribute("list1", list1);

        // 물품명
        List<ItemDTO> list2 = iMapper.selectItemList(2);
        model.addAttribute("list2", list2);

        // 가격
        List<ItemDTO> list3 = iMapper.selectItemList(3);
        model.addAttribute("list3", list3);

        return "/shop/home";
    }    
}

파일명 ItemMapper.java

package com.example.mapper;

import java.util.List;

import com.example.dto.ItemDTO;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ItemMapper {

    @Select({
        "SELECT I.ICODE, I.INAME, I.ICONTENT, I.IPRICE, I.IQUANTITY",
        " FROM ITEM I WHERE I.ICODE = #{code}"
    })
    public ItemDTO selectItemOne(@Param(value = "code")long code);

    // 등록일 기준 내림차순 1
    // SELECT * FROM (	
    // SELECT I.ICODE, I.INAME, I.IPRICE,  ROW_NUMBER() OVER (ORDER BY IREGDATE DESC) ROWN FROM ITEM I
    // ) WHERE ROWN BETWEEN 1 AND  12 ORDER BY ROWN ASC;

    // 물품명 기준 오름차순 2
    // SELECT * FROM (	
    // SELECT I.ICODE, I.INAME, I.IPRICE,  ROW_NUMBER() OVER (ORDER BY INAME ASC) ROWN FROM ITEM I
    // ) WHERE ROWN BETWEEN 1 AND  12 ORDER BY ROWN ASC;

    // 가격 기준 오름차순 3
    // SELECT * FROM (	
    // SELECT I.ICODE, I.INAME, I.IPRICE,  ROW_NUMBER() OVER (ORDER BY IPRICE ASC) ROWN FROM ITEM I
    // ) WHERE ROWN BETWEEN 1 AND  12 ORDER BY ROWN ASC;

    // 가격 기준 내림차순 4
    
    // SELECT I.*, ROW_NUMBER() OVER (ORDER BY INAME ASC)ROWN FROM ITEM I

    @Select({
        "<script>",
            "SELECT * FROM (",
                "SELECT I.ICODE, I.INAME, I.IPRICE,  ROW_NUMBER() OVER (",                
                    "<choose>",
                        "<when test = 'type == 1'>ORDER BY IREGDATE DESC</when>",
                        "<when test = 'type == 2'>ORDER BY INAME ASC</when>",
                        "<when test = 'type == 3'>ORDER BY IPRICE ASC</when>",
                    "</choose>",
                ") ROWN FROM ITEM I",
            ") WHERE ROWN BETWEEN 1 AND  8 ORDER BY ROWN ASC",
        "</script>"
    })
    public List<ItemDTO> selectItemList(
        @Param(value = "type")int rype );
    
}

파일명 ItemImageMapper.java

package com.example.mapper;

import java.util.List;

import com.example.dto.ItemImageDTO;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ItemImageMapper {

    // 물품코드가 오면 관련되어 있는 서브이미지 번호들 반환
    @Select({
        "SELECT IMGCODE FROM ITEMIMAGE WHERE ICODE = #{code}"
    })
    public List<Long> selectItmeImageCodeList(
        @Param("code") long code
    );

    // 서브이미지코드에 해당하는 1개의 이미지 정보를 반환
    @Select({
        "SELECT * FROM ITEMIMAGE WHERE IMGCODE = #{imgcode}"
    })
    public ItemImageDTO selectItemImageCodeOne(
        @Param(value = "imgcode")long imgcode
    );
}
profile
당신을 한 줄로 소개해보세요

0개의 댓글