장바구니 목록

mybatis

package shop.persistence;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import common.base.DAOMyBatisBase;
import shop.domain.CartVO;

public class CartDAOMyBatis extends DAOMyBatisBase {

	private final String NS = "common.mapper.CartMapper";
	private SqlSession ses;
	public int addCart(CartVO vo) {
		try {
			ses=this.getSqlSessionFactory().openSession(true); //true주면 auto commit;
			int n = ses.insert(NS+".addCart",vo);
			return n;
		}finally {
			close(ses);
		}
	}
	public int selectCartByPnum(CartVO cvo) {
		try {
			ses=this.getSqlSessionFactory().openSession();
			int cnt=ses.selectOne(NS+".selectCartByPnum",cvo);
			return cnt;
		}finally {
			close(ses);
		}
	}
	public int updateCartOqty(CartVO cvo) {
		try {
			ses=this.getSqlSessionFactory().openSession(true);
			int n = ses.update(NS+".updateCartOqty",cvo);
			return n;
		}finally {
			close(ses);
		}
	}
	public List<CartVO> selectCartByIdx(int idx){
		try {
			ses=this.getSqlSessionFactory().openSession();
			List<CartVO> cList = ses.selectList(NS+".selectCartByIdx",idx);
			return cList;
		}finally {
			close(ses);
		}
	}
}


CartMapper

	<!-- 장바구니에 상품추가 -->
	<insert id="addCart" parameterType="Cart">
		insert into cart(cartNum, pnum, idx, oqty,indate)
		values(cart_seq.nextval,#{pnum},#{idx},#{oqty},sysdate)
	</insert>
	<!-- 장바구니에 있는 특정 상품 갯수 알아내기 -->
	<select id="selectCartByPnum" resultType="_int" parameterType="Cart">
		select count(*) from cart where idx=#{idx} and pnum=#{pnum}
	</select>
	<!-- 장바구니 수량 수정 -->
	<update id="updateCartOqty" parameterType="Cart">
		update cart set oqty = oqty + #{oqty} where idx=#{idx} and pnum=#{pnum}
	</update>
	<select id="selectCartByIdx" parameterType="int" resultType="Cart">
		select cartnum, c.pnum, p.pname, p.pimage1, p.price, p.saleprice,
		c.oqty, p.point, (saleprice*oqty) totalPrice, (point*oqty) totalPoint
		from cart c join product p
		on c.pnum=p.pnum where idx=${idx} order by cartNum desc
	</select>


CartListAction.jsp

package shop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.controller.AbstractAction;
import shop.domain.CartVO;
import shop.persistence.CartDAOMyBatis;
import user.domain.UserVO;

public class CartListAction extends AbstractAction {
	CartDAOMyBatis dao = new CartDAOMyBatis();
	@Override
	public void execute(HttpServletRequest req, HttpServletResponse res) throws Exception {

		HttpSession ses = req.getSession();
		UserVO user=(UserVO)ses.getAttribute("loginUser");
		int idx=user.getIdx();

		//특정 회원의 장바구니 목록 가져오기
		List<CartVO> cartList = dao.selectCartByIdx(idx);
		//장바구니 총액과 총 포인트 가져오기
		CartVO cart = dao.getCartTotal(idx);
			
		req.setAttribute("cartTotalPrice", cart.getCartTotalPrice());
		req.setAttribute("cartTotalPoint", cart.getCartTotalPoint());
		req.setAttribute("cartList", cartList);
		this.setViewPage("../shop/cartList.jsp");
		this.setRedirect(false);
	}

}


jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<jsp:include page="/top.jsp"/>
<h1 class="text-success text-center">${loginUser.name}[${loginUser.userid}]님 장바구니</h1>
<!------------------ 주문 폼 시작 -->
<form name="orderF" id="orderF" action="order.do">
	<table class="table table-striped">
		<thead>
			<tr>
				<th class="text-center">상품번호</th>
				<th class="text-center">상품명</th>
				<th class="text-center">수량</th>
				<th class="text-center">단가</th>
				<th class="text-center">금액</th>
				<th class="text-center">삭제</th>
			</tr>
		</thead>
		<tbody>
			<!--if 하고 forEach 돌리기 ------------ -->
			<c:if test="${cartList eq null or empty cartList}">
				<tr>
					<td colspan="6"><b>담긴 상품이 없습니다.</b></td>
				</tr>
			</c:if>
			<c:if test="${cartList ne null and not empty cartList}">
				<c:forEach var="cp" items="${cartList}">
				<tr>
					<td>
						<label>
							<input type="checkbox" name="pnum" idx="">${cp.pnum}
						</label>
					</td>			
					<td>
						${cp.pname}<br>
						<a href="../prodDetail.do?pnum=${cp.pnum}" target="_blank"><img src="../images/${cp.pimage1}" class="img-thumbnail" alt="${cp.pname}" style="width:140px"></a>
					</td>			
					<td>
						<input type="number" value="${cp.oqty}" min="1" max="50" size="3">개
						<button type="button" class="btn-info">수정</button>
					</td>			
					<td>
						<fmt:formatNumber value="${cp.saleprice}" pattern="###,###"/>원<br>
						<span class="badge badge-danger">${cp.point}</span>Point
					</td>			
					<td style="font-weight:bold">
						<fmt:formatNumber value="${cp.totalPrice}" pattern="###,###"/>원<br>
						<span class="badge badge-danger">${cp.totalPoint}</span>Point
					</td>			
					<td>
						<button class="btn-warning">삭제</button>
					</td>			
				</tr>
				</c:forEach>
			</c:if>
			<!-- ------------ -->
			<tr>
				<td colspan="3">
					<h5>장바구니 총     액: 
					<span class="text-danger">
					<fmt:formatNumber value="${cartTotalPrice}" pattern="###,###"/>
					</span>원</h5>
					<h5>장바구니 총포인트: 
					<span class="text-success">
					<fmt:formatNumber value="${cartTotalPoint}" pattern="###,###"/>
					</span>point</h5>
				</td>
				<td colspan="3">
					<button type="button" class="btn btn-warning">주문하기</button>
					<button type="button" class="btn btn-success">계속쇼핑</button>
				</td>
			</tr>
		</tbody>
	</table>
</form>
<!------------------ 주문 끝-->
<jsp:include page="/foot.jsp"/>

 

+ Recent posts