Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

게으른 완벽주의자의 개발자 도전기

[shop] 검색조건을 활용하여 아이템 검색하기 본문

Spring Boot

[shop] 검색조건을 활용하여 아이템 검색하기

머리방울 2022. 10. 9. 16:18

1. html 화면

<div class="row mb-3">
  <div class="col">
  	<form th:action="@{/item/itemList}" method="post">
     <table class="table align-middle table-bordered text-center test"><!-- table-borderless -->
        <colgroup>
           <col width="8%">
           <col width="8%">
           <col width="15%">
           <col width="8%">
           <col width="19%">
           <col width="8%">
           <col width="8%">
           <col width="18%">
           <col width="8%">
        </colgroup>
        <tr>
           <td rowspan="2">
              <div class="row">
                 <div class="col-12">
                    검 색
                 </div>
                 <div class="col-12">
                    조 건
                 </div>
              </div>
           </td>
           <td class="searchTitle">카테고리</td>
           <td>
              <select class="form-select" name="cateCode">
                <option value="" th:selected="${paramMap['cateCode'] == null or paramMap['cateCode'] eq ''}">전체</option>
                 <th:block th:each="cateList: ${cateList}">
                    <option th:text="${cateList.cateName}" th:value="${cateList.cateCode}" th:selected="${paramMap['cateCode'] eq cateList.cateCode}"></option>
                 </th:block>
              </select>
           </td>
           <td class="searchTitle">상품명</td> 

           <td colspan="2">
              <input class="form-control" type="text" name="itemName" id="" th:value="${paramMap['itemName']}">
           </td>
           <td class="searchTitle">재고</td>
           <td>
              <input class="form-control" type="text" name="itemStock" id="" th:value="${paramMap['itemStock']}">
           </td>
           <td rowspan="2">
              <button class="btn btn-primary" style="height: 90px;" type="submit">Search</button>
           </td>
        </tr>
        <tr>
           <td class="searchTitle">등록일</td>
           <td colspan="3">
              <div class="row">
                 <div class="col-6">
                    <input class="form-control" type="date" name="fromDate" id="" th:value="${paramMap['fromDate']}" >
                 </div>
                 <div class="col-6">
                    <input class="form-control" type="date" name="toDate" id="" th:value="${paramMap['toDate']}" >
                 </div>
              </div>
           </td>
           <td class="searchTitle">상태</td>
           <td colspan="2">
              <div class="form-check form-check-inline">			<!-- null값이거나 빈값이거나 -->
                <input class="form-check-input" type="radio" name="itemStatus" id="" value="" th:checked="${paramMap['itemStatus'] == null or paramMap['itemStatus'] eq ''}">
                <label class="form-check-label" for="" >전체</label>
              </div>
                 <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="itemStatus" id="" value="ON_SALE" th:checked="${paramMap['itemStatus'] eq 'ON_SALE'}">
                <label class="form-check-label" for="">판매중</label>
              </div>
                 <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="itemStatus" id="" value="SOLD_OUT" th:checked="${paramMap['itemStatus'] eq 'SOLD_OUT'}" >
                <label class="form-check-label" for="">매진</label>
              </div>
           </td>
        </tr>
     </table>
  </form>
  </div>
   <div class="row" style="margin-top: 2rem;">
    <div class="col" style="font: 10px;">
    	<div>총 <span th:text="${#lists.size(itemList)}"></span>개의 상품이 검색되었습니다. </div>
    </div>
   </div>
</div>

 

2. mapper

<select id="selectItem" resultMap="item" parameterType="hashMap">
<!--  parameterType="hashMap" 작성하지 않아도 알아서 가져옴 -->
SELECT ITEM_NAME
    , ITEM_CODE
    , TO_CHAR(REG_DATE, 'YYYY-MM-DD') AS REG_DATE
    , ITEM_STOCK
    , (SELECT CATE_NAME FROM ITEM_CATEGORY WHERE CATE_CODE = S.CATE_CODE) AS CATE_NAME
    , ITEM_STATUS
    , ITEM_PRICE
    , ITEM_COMMENT
    , CATE_CODE
FROM SHOP_ITEM S
WHERE 1=1
<!--모두 if 조건절 넣기  -->  <!-- null 아니고 빈문자가 아니면 실행하겠다 -->
<if test="cateCode != null and !cateCode.equals('')"> 
  AND CATE_CODE = #{cateCode}
  </if>
  <if test="itemName != null and !itemName.equals('')">
  AND UPPER(ITEM_NAME) LIKE UPPER('%'||#{itemName}||'%')
  </if>      
  <if test="itemStock != null and !itemStock.equals('')">
  AND ITEM_STOCK &lt;= #{itemStock}
  </if>      

  <if test="fromDate != null and !fromDate.equals('')">
  AND TO_CHAR(REG_DATE, 'YYYY-MM-DD') &gt;= #{fromDate}  
  </if>

  <if test="toDate != null and !toDate.equals('')">
  AND TO_CHAR(REG_DATE, 'YYYY-MM-DD') &lt;= #{toDate}
  </if>

  <if test="itemStatus !=null and !itemStatus.equals('')">
  AND ITEM_STATUS = #{itemStatus}
  </if>

</select>

&gt; greater then

&lt; less then

 

public List<ItemVO> selectItem(Map<String, String> paramMap) {

	return sqlSession.selectList("itemMapper.selectItem", paramMap);
}

 

3. controller

 

@RequestMapping("/itemList")	//요청된 것 map에 다 넣어달라 key: name value:입력한 값
public String selectItem (@RequestParam Map<String, String> paramMap, Model model){

    //카테고리 리스트 
    model.addAttribute("cateList", adminService.cateList());

    //상품목록조회
    model.addAttribute("itemList", itemService.selectItem(paramMap));

    //현재 날짜 (우리가 static 붙여놨기에 객체 만들지 x 가능)
    String nowDate = ShopDateUtil.getNowDateToString("-"); //2022-10-09

    //한달 전 날짜
    String aMonthAgo = ShopDateUtil.getAmonthAgoToString();

    //넘어오는 fromDate가 없다면 한달 전 날짜로 set 하겠다
    if(paramMap.get("fromDate")==null) {

        paramMap.put("fromDate", aMonthAgo);
    }

    if(paramMap.get("toDate")==null) {
        paramMap.put("toDate", nowDate);
    }

    model.addAttribute("paramMap", paramMap);

    return "content/admin/item_manage"; 
}