Spring
테이블의 관계를 나타내고 제약조건을 한눈에 알아볼 수 있게 작성한다.
어떤 테이블의 어떤 내용을 사용하는지 깔끔하게 정리할 수 있다.
Join을 사용할때 사용할 제약조건을 확인하고 기획 및 코딩시 수월하게 동작 할 수있다.
해당 이미지는 oracle model에서 구조확인 할 수있다.
컨트롤러
package com.korea.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.korea.k2.cart.CartService;
import com.korea.k2.cart.CartVO;
import com.korea.k2.cart.OrderJumunseoVO;
import com.korea.k2.cart.OrderMoneyVO;
import com.korea.k2.cart.OrderOneVO;
@Controller
public class CatrController {
@Autowired
private CartService service;
@RequestMapping("/orderList.do")
String orderList( Model model ) throws Exception {
model.addAttribute("li",service.orderList());
return "/cart/orderList.jsp";
}
@RequestMapping("/orderOne.do")
String orderOne( Model model, OrderOneVO vo ) throws Exception {
model.addAttribute("li",service.orderOne(vo));
return "/cart/orderOne.jsp";
}
@RequestMapping("/cartUpdate.do")
String cartUpdate(
@RequestParam String[] cartId,
@RequestParam String[] custNo,
@RequestParam String[] productId,
@RequestParam String[] amount ) throws Exception {
String custNoStr ="";
for (int i=0 ; i < cartId.length ; i++) {
CartVO vo = new CartVO();
custNoStr = custNo[i];
vo.setCartId(Integer.parseInt(cartId[i]));
vo.setAmount(Integer.parseInt(amount[i]));
service.update(vo);
}
return "redirect:/cartList.do?custNo="+custNoStr;
}
@RequestMapping("/cartOrder.do")
String cartOrder(
@RequestParam String[] cartId,
@RequestParam String[] custNo,
@RequestParam String[] productId,
@RequestParam String[] productName,
@RequestParam String[] amount ,
OrderMoneyVO mvo ) throws Exception {
mvo.setOcustNo(Integer.parseInt(custNo[0]));
service.insertOrderMoney(mvo);
OrderMoneyVO vo = service.orderG(mvo);
int custNoStr = 0;
OrderJumunseoVO Ovo =null;
for (int i=0 ; i < cartId.length ; i++) {
Ovo = new OrderJumunseoVO();
custNoStr = Integer.parseInt(custNo[i]);
Ovo.setCustNo(Integer.parseInt(custNo[i]));
Ovo.setCartId(Integer.parseInt(cartId[i]));
Ovo.setProductId(Integer.parseInt(productId[i]));
Ovo.setProductName(productName[i]);
Ovo.setAmount(Integer.parseInt(amount[i]));
Ovo.setOrderG(vo.getIdx());
service.insertOrderJumunseo(Ovo);
}
// 장바구니 삭제하기
CartVO cartVO = new CartVO();
cartVO.setCustNo(custNoStr);
service.deleteAll(cartVO);
String custNoK = String.valueOf(custNoStr);
return "redirect:/cartList.do?custNo="+custNoK;
}
@RequestMapping("/cartInsert.do")
String cartInsert( CartVO vo ) throws Exception {
System.out.println("cartInsert:" + vo);
service.insert(vo);
return "/cartList.do";
}
@RequestMapping("/cartDeleteOne.do")
String cartDeleteOne( CartVO vo ) throws Exception {
service.deleteOne(vo);
return "/cartList.do";
}
@RequestMapping("/cartDeleteAll.do")
String cartDeleteAll( CartVO vo ) throws Exception {
service.deleteAll(vo);
return "/cartList.do";
}
@RequestMapping("/cartList.do")
String cartList( CartVO vo, Model model ) throws Exception {
model.addAttribute("li",service.selectAll(vo) );
return "/cart/cartList.jsp";
}
}
cartDao
package com.korea.k2.cart;
import java.util.List;
public interface CartDao {
// 1. 장바구니 추가
void insert(CartVO vo);
// 2. 장바구니 1개 삭제
void deleteOne(CartVO vo);
// 3. 장바구니 전체삭제
void deleteAll(CartVO vo);
// 4. 장바구니 업데이트
void update(CartVO vo);
// 5. 주문서 저장하기
void insertOrderJumunseo(OrderJumunseoVO vo);
// 6. 주문금액 저장하기
void insertOrderMoney(OrderMoneyVO vo);
// 7. 주문그룹 추가하기
OrderMoneyVO orderG(OrderMoneyVO vo);
// . 장바구니 목록보기
List<CartVO> selectAll(CartVO vo);
// 전체주문 목록보기
List<OrderMoneyVO> orderList();
// 주문목록을 이용한 주문서 출력
List<OrderOneVO> orderOne(OrderOneVO vo);
}
mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="CART">
<insert id="INSERT" parameterType="cart" >
insert into CartTBL(cartId,custNo, productId, amount )
values (seq_CartTBL.nextval ,#{custNo}, #{productId}, #{amount})
</insert>
<insert id="OrderJumunseo" parameterType="OrderJumunseo" >
insert into OrderJumunseo(idx,custNo, cartId , productId, productName , amount, orderG )
values (seq_CartTBL.nextval ,#{custNo},#{cartId}, #{productId},#{productName}, #{amount} , #{orderG} )
</insert>
<insert id="OrderMoney" parameterType="OrderMoney" >
insert into OrderMoney(idx,custNo, baesongbi , totalmoney )
values (seq_CartTBL.nextval ,#{OcustNo},#{baesongbi}, #{totalmoney} )
</insert>
<select id="orderG" parameterType="OrderMoney" resultType="OrderMoney" >
select max(idx) idx from orderMoney
where CUSTNO =#{OcustNo}
</select>
<select id="orderList" parameterType="OrderMoney" resultType="OrderMoney" >
select idx, custNo as custnum, baesongbi, totalmoney, getDate
from orderMoney order by idx desc
</select>
<select id="orderOne" parameterType="OrderOne" resultType="OrderOne" >
select M.BAESONGBI,M.TOTALMONEY, M.GETDATE,
M2.custname,M2.phone, M2.ADDRESS,
J.orderG, J.IDX, J.PRODUCTNAME , J.PRODUCTID, J.amount
from orderMoney M join orderJumunseo J
on M.IDX = J.ORDERG join MEMBER_TBL_02 M2 on
M2.custno = M.CUSTNO
where M.idx=#{idx} order by M.idx desc
</select>
<delete id="DELETEONE" parameterType="cart" >
delete from CartTBL where cartId = #{cartId}
</delete>
<delete id="DELETEALL" parameterType="cart" >
delete from CartTBL where custno = #{custNo}
</delete>
<delete id="UPDATE" parameterType="cart" >
update CartTBL set amount=#{amount} where cartId = #{cartId}
</delete>
<select id="SELECT" resultType="cart" >
select cartId, c.productId, productName,
productPrice, amount, productImg as productImgStr
from CartTBL c join productTBL p
on c.productId = p.productId
where custNo= #{custNo}
</select>
</mapper>
<%@ page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:import url="/include/top.jsp" />
<script>
function listK(){
alert("목록보기")
location.href="${path}/productList.do"
}
function delK(k){
alert("삭제하기" + k)
location.href="${path}/productDelete.do?productId="+k
}
</script>
<section>
<br>
<div align=center>
<h2>쇼핑몰 상품상세 보기</h2>
<form action=${path}/cartInsert.do >
<input type=text name=productId value="${p.productId}" >
<input type=text name=custNo value="${m.custno}" >
<table border=1 width=650 height=350>
<tr><td align="center" width=150>상품번호</td>
<td >   ${p.productId} </td>
<td rowspan=4 width=110>
<img src=${path}/product/files/${p.productImgStr} width=200 height=250 /> </td> </tr>
<tr><td align="center">상품이름</td>
<td>   ${p.productName} </td></tr>
<tr><td align="center">상품가격</td>
<td> ${p.productPrice} </td></tr>
<tr><td align="center">상품설명</td>
<td ><textarea cols=40 rows=9 name=productDesc >
${p.productDesc}
</textarea> </td></tr>
<tr><td align="center">구매수량</td>
<td colspan=2>  
<input type=number name=amount min=1 max=5> </td></tr>
<tr><td align="center">등록날짜</td>
<td colspan=2>   ${p.productDate} </td></tr>
<tr><td colspan=3 align="center">
<input type=submit value="상품구매" >
  
<input type=button value="목록보기" onClick="listK()" >
  
<input type=button value="삭제하기" onClick="delK('${p.productId}')" >
</td></tr>
</table>
</form>
</div>
<br>
</section>
<c:import url="/include/bottom.jsp" />
장바구니에 담겨진 상품을 구매하는 코드이다.
상품의 상세화면으로 진입하여 구매하기, 목록보기, 삭제하기 버튼을 통해 맵핑한
쿼리문 동작으로 DB에서와 view에서 삭제가 가능하고,
목록보기시 상품 리스트 화면으로 이동되며,
구매하기 동작 시 장바구니 목록에 해당 상품이 추가된다.
포트폴리오를 준비하는 동안 게시판을 만들며 상품을 주문하고 삭제하는 내용을 복습하고있다.
Spring의 전반적인 내용을 이해하고 사용할 수 있도록 보다 복습이 더 필요할거 같다.
좋은 글 잘 읽었습니다, 감사합니다.