파일명 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;
}
파일명 CartMapper.java
package com.example.mapper;
import com.example.dto.CartDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface CartMapper {
// INSERT INTO 테이블명(컬렴명들...) VALUES(값들..)
@Insert({
"INSERT INTO CART (CNO, CCNT, ICODE, UEMAIL)",
" VALUES (SEQ_CART_NO.NEXTVAL, #{obj.ccnt}, #{obj.icode}, #{obj.uemail})"
})
public int insertCartOne( @Param(value = "obj") CartDTO cart); }
파일명 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 />
<div style="padding:20px">
<form th:action="@{/shop/cart}" method="post">
<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" name="btn" value="장바구니" />
<input type="submit" name="btn" 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>
파일명 BuyDTO.java
package com.example.dto;
import java.util.Date;
import lombok.Data;
@Data
public class BuyDTO {
// 주문번호
private Long bno;
// 주문수량
private Long bcnt;
// 주문일자
private Date bregdate;
// 물품코드
private Long icode;
// 이메일
private String uemail;
}
파일명 ShopController.java
package com.example.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import com.example.dto.BuyDTO;
import com.example.dto.CartDTO;
import com.example.dto.ItemDTO;
import com.example.mapper.BuyMapper;
import com.example.mapper.CartMapper;
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;
@Autowired CartMapper cMapper;
@Autowired BuyMapper bMapper;
@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(map);
model.addAttribute("list", list);
return "/shop/buylist";
}
@PostMapping(value = "/cart")
public String cartPOST(
@RequestParam(name = "btn") String btn,
@RequestParam(name = "code") long code,
@RequestParam(name = "cnt") long cnt
){
String em = (String)httpSession.getAttribute("M_EMAIL");
if(em == null){ // 로그인 되지 않았다면
return "redirect:/member/login";
}
System.out.println("btn : " + btn);
if(btn.equals("장바구니")){
// 로그인 되었다면
CartDTO cart = new CartDTO();
cart.setCcnt(cnt); // 수량
cart.setIcode(code); // 물품코드
cart.setUemail(em);
cMapper.insertCartOne(cart);
return "redirect:/shop/cartlist";
// System.out.println("주문수량"+cnt);
// System.out.println("물품코드"+code);
// System.out.println("주문자 이메일"+em);
}
else if(btn.equals("주문하기")) {
BuyDTO buy = new BuyDTO();
buy.setBcnt(cnt);
buy.setIcode(code);
buy.setUemail(em);
bMapper.insertBuyOne(buy);
return "redirect:/shop/buylist";
}
return "redirect:/";
}
// 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";
}
}
파일명 BuyMapper.java
package com.example.mapper;
import java.util.List;
import java.util.Map;
import com.example.dto.BuyDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BuyMapper {
// INSERT INTO 테이블명(컬렴명들...) VALUES(값들..)
@Insert({
"INSERT INTO BUY (BNO, BCNT, ICODE, UEMAIL, BREGDATE)",
" VALUES (SEQ_BUY_NO.NEXTVAL, #{obj.bcnt}, #{obj.icode}, #{obj.uemail}, CURRENT_DATE)"
})
public int insertBuyOne( @Param(value = "obj") BuyDTO buy);
// 주문내역 조회 리턴은 DTO가 아닌 Map컬렉션을 이용함
@Select({
"SELECT * FROM BUYLIST_VIEW WHERE UEMAIL = #{em}"
})
public List<Map<String, Object> > selectBuyListMap(@Param(value = "em") String em);
}
파일명 buylist.java
<!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>
주문목록
<table border="1">
<tr>
<th>주문번호</th>
<th>물품명</th>
<th>물품가격</th>
<th>주문수량</th>
<th>주문자명</th>
<th>주문자연락처</th>
<th>주문날짜</th>
</tr>
<tr th:each="obj : ${list}">
<td th:text="${obj.BNO}"></td>
<td th:text="${obj.INAME}"></td>
<td th:text="${obj.IPRICE}"></td>
<td th:text="${obj.BCNT}"></td>
<td th:text="${obj.UNAME}"></td>
<td th:text="${obj.UPHONE}"></td>
<td th:text="${obj.BREGDATE}"></td>
</tr>
</table>
<hr />
<a th:href="@{/home}">홈으로</a>
```
---
파일명 Boot20220328Application.java
package com.example.boot_20220328;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@EnableAspectJAutoProxy // aop추가
// 컨트롤러, 환경파일, 서비스(mybatis 1번전용)
@ComponentScan(basePackages = {
"com.example.controller",
"com.example.service",
"com.example.config",
"com.example.aop", // aop추가
"com.example.interceptor" // interceptor 추가
})
// Mapper(mybaris 2번전용)
@MapperScan(basePackages = {"com.example.mapper"})
public class Boot20220328Application {
public static void main(String[] args) {
SpringApplication.run(Boot20220328Application.class, args);
}
}