구조
1.SqlMap
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="cart">
<insert id="insertCart" parameterClass="CartVO">
insert into CART values(fn_cart_id, #mem_id#, sysdate, #cart_price#)
</insert>
<insert id="insertGameCart" parameterClass="String">
insert into cart_game values(fn_cartgame_id, #gameId#)
</insert>
<select id="getPrice" parameterClass="String" resultClass="String">
select game_price
from game
where game_id = #gameId#
</select>
<resultMap class="CartVO" id="CartMap">
<result property="game_id" column="game_id"/>
<result property="cart_price" column="cart_price"/>
<result property="cartNo" column="cart_no"/>
</resultMap>
<select id="getCart" parameterClass="String" resultMap="CartMap">
select distinct a.cart_price, b.game_id, a.cart_no
from cart a
inner join cart_game b
on(a.CART_NO = b.CART_NO)
and a.mem_id= #mem_id#
</select>
<resultMap class="CartVO" id="CartMap">
<result property="cart_price" column="cart_price"/>
<result property="cartNo" column="cart_no"/>
</resultMap>
<select id="selectCart" parameterClass="String" resultMap="CartMap">
select cart_no, cart_price
from cart
where not cart_no in (
select a.cart_no
from cart a
inner join orders b
on(a.cart_no = b.cart_no)
)
and mem_id = #mem_id#
</select>
<select id="getSelectGameId" parameterClass="String" resultClass="String">
select game_id
from cart_game
where cart_no = #cartNo#
</select>
<select id="checkExist" parameterClass="CartVO" resultClass="String">
select a.cart_no
from cart a
inner join cart_game b
on(a.CART_NO = b.CART_NO)
where a.mem_id = #mem_id# and b.game_id = #game_id#
</select>
<delete id="deleteCart" parameterClass="String">
delete from cart
where cart_no = #cartNo#
</delete>
<delete id="deleteCartGame" parameterClass="String">
delete from cart_game
where cart_no = #cartNo#
</delete>
<select id="orderList" resultClass="String">
select cart_no
from orders
</select>
</sqlMap>
2.CartVO
package orders.cart.vo;
public class CartVO {
private String cartNo;
private String mem_id;
private String cart_price;
private String game_id;
public String getGameId() {
return game_id;
}
public void setGameId(String gameId) {
this.game_id = gameId;
}
public String getCartNo() {
return cartNo;
}
public void setCartNo(String cartNo) {
this.cartNo = cartNo;
}
public String getMemId() {
return mem_id;
}
public void setMemId(String memId) {
this.mem_id = memId;
}
public String getCartPrice() {
return cart_price;
}
public void setCartPrice(String cartPrice) {
this.cart_price = cartPrice;
}
}
3.CartDAO
ICartDAO
package orders.cart.dao;
import java.util.List;
import orders.cart.vo.CartVO;
import orders.vo.OrderVO;
public interface ICartDAO {
public int insertCart(CartVO vo);
public int insertCartGame(String gameId);
public String getPrice(String gameId);
public List<CartVO> getCartList(String mem_id);
public String checkExist(CartVO vo);
public int deleteCart(String cartNo);
public int deleteCartGame(String cartNo);
public List<String> orderList();
public List<CartVO> getSelectCart(String mem_id);
public String getSelectGame(String cartNo);
}
CartDAOImpl
package orders.cart.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
import config.SqlMapClientFactory;
import orders.cart.vo.CartVO;
public class CartDAOImpl implements ICartDAO {
private static ICartDAO dao;
private SqlMapClient smc;
private CartDAOImpl() {
smc = SqlMapClientFactory.getInstance();
}
public static ICartDAO getInstance() {
if(dao == null) {
dao= new CartDAOImpl();
}
return dao;
}
@Override
public int insertCart(CartVO vo) {
int cnt =0;
try {
cnt = smc.update("cart.insertCart", vo);
} catch (SQLException e) {
e.printStackTrace();
}
return cnt;
}
@Override
public int insertCartGame(String gameId) {
int cnt =0;
try {
cnt = (int) smc.update("cart.insertGameCart",gameId);
} catch (SQLException e) {
e.printStackTrace();
}
return cnt;
}
@Override
public String getPrice(String gameId) {
String price= null;
try {
price = (String) smc.queryForObject("cart.getPrice",gameId);
System.out.println("게임의 가격 찾아오기 : "+price);
} catch (SQLException e) {
e.printStackTrace();
}
return price;
}
@Override
public List<CartVO> getCartList(String mem_id) {
List<CartVO> cartList = new ArrayList<CartVO>();
try {
cartList = smc.queryForList("cart.getCart",mem_id);
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException("회원의 장바구니 조회중 예외 발생!",ex);
}
return cartList;
}
@Override
public List<String> orderList() {
List<String> orderLsit = null;
try {
orderLsit = (List<String>) smc.queryForList("cart.orderList");
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException("주문완료한 카트넘버 쿼리해오는중 예외 발생!",ex);
}
return orderLsit;
}
@Override
public String checkExist(CartVO vo) {
String check = null;
try {
check = (String) smc.queryForObject("cart.checkExist",vo);
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException("카트넘버 쿼리해오는중 예외 발생!",ex);
}
return check;
}
@Override
public int deleteCart(String cartNo) {
int cnt = 0;
try {
cnt = smc.delete("cart.deleteCart",cartNo);
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException("Cart테이블 게임 삭제중 예외 발생",ex);
}
return cnt;
}
@Override
public int deleteCartGame(String cartNo) {
int cnt = 0;
try {
cnt = smc.delete("cart.deleteCartGame",cartNo);
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException("CartGme테이블 게임 삭제중 예외 발생",ex);
}
return cnt;
}
@Override
public List<CartVO> getSelectCart(String mem_id) {
List<CartVO> selectCartList = new ArrayList<CartVO>();
try {
selectCartList = smc.queryForList("cart.selectCart",mem_id);
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException("회원이 주문완료한 게임을 제외한 장바구니목록 조회중 예외발생!",ex);
}
return selectCartList;
}
@Override
public String getSelectGame(String cartNo) {
String selectGame = "";
try {
selectGame = (String) smc.queryForObject("cart.getSelectGameId",cartNo);
System.out.println("셀렉트겜네임 쿼리 잘되나 >>"+selectGame);
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException("주문완료 제외 장바구니의 게임아이디 조회중 예외!",ex);
}
return selectGame;
}
}
4.CartService
ICartService
package orders.cart.service;
import java.util.List;
import orders.cart.vo.CartVO;
import orders.vo.OrderVO;
public interface ICartService {
public String getPrice(String gameId);
public int insertCart(CartVO vo);
public int insertCartGame(String gameId);
public List<CartVO> getCartList(String memId);
public String checkExist(CartVO vo);
public int deleteCart(String cartNo);
public int deleteCartGame(String cartNo);
public List<String> orderList();
public List<CartVO> getSelectCart(String mem_id);
public String getSelectGame(String cartNo);
}
CartServiceImpl
package orders.cart.service;
import java.util.List;
import orders.cart.dao.CartDAOImpl;
import orders.cart.dao.ICartDAO;
import orders.cart.vo.CartVO;
import orders.vo.OrderVO;
public class CartServiceImpl implements ICartService {
private static ICartService service;
private ICartDAO dao;
private CartServiceImpl() {
dao= CartDAOImpl.getInstance();
}
public static ICartService getInstance() {
if(service == null) {
service = new CartServiceImpl();
}
return service;
}
@Override
public int insertCart(CartVO vo) {
return dao.insertCart(vo);
}
@Override
public int insertCartGame(String gameId) {
return dao.insertCartGame(gameId);
}
@Override
public String getPrice(String gameId) {
return dao.getPrice(gameId);
}
@Override
public List<CartVO> getCartList(String memId) {
return dao.getCartList(memId);
}
@Override
public String checkExist(CartVO vo) {
return dao.checkExist(vo);
}
@Override
public int deleteCart(String cartNo) {
return dao.deleteCart(cartNo);
}
@Override
public int deleteCartGame(String cartNo) {
return dao.deleteCartGame(cartNo);
}
@Override
public List<String> orderList() {
return dao.orderList();
}
@Override
public List<CartVO> getSelectCart(String mem_id) {
return dao.getSelectCart(mem_id);
}
@Override
public String getSelectGame(String cartNo) {
return dao.getSelectGame(cartNo);
}
}
5.CartController
CartDetailController
package orders.cart.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import games.game.vo.FreeGameVO;
import games.game.vo.GameVO;
import members.member.vo.MemberVO;
import orders.cart.service.CartServiceImpl;
import orders.cart.service.ICartService;
import orders.cart.vo.CartVO;
import orders.service.IOrderService;
import orders.service.OrderServiceImpl;
import wishList.service.IWishListService;
import wishList.service.WishListServiceImpl;
import wishList.vo.WishListVO;
@WebServlet("/cart/cartDetail.do")
public class CartDetailController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("\n\n겟카트컨트롤러에 들어옴");
System.out.println("멤버 아이디 잘들어오나확인해볼게");
ICartService cartService = CartServiceImpl.getInstance();
HttpSession session = req.getSession();
if(session.getAttribute("loginMember") == null) {
req.setAttribute("msg", "장바구니 조회는 로그인후에 가능합니다.");
req.getRequestDispatcher("/main/main.jsp").forward(req, resp);
return;
}
MemberVO member = (MemberVO) session.getAttribute("loginMember");
String memId = member.getMem_id();
System.out.println("멤버 아이디 잘들어오나?????"+memId);
List<CartVO> selectCartList = cartService.getSelectCart(memId);
List<String> selectGameList = new ArrayList<String>();
for(CartVO vo : selectCartList) {
selectGameList.add(cartService.getSelectGame(vo.getCartNo()));
}
for(CartVO vo : selectCartList) {
System.out.println("카트리스트:"+vo.getCartNo());
}
for(String vo : selectGameList) {
System.out.println("겜리스트:"+vo);
}
req.setAttribute("selectCartList", selectCartList);
req.setAttribute("selectGameList", selectGameList);
IOrderService orderService = OrderServiceImpl.getInstance();
List<GameVO> gameInfoList = new ArrayList<GameVO>();
for(String vo : selectGameList) {
gameInfoList.add(orderService.getGamesInfo(vo));
}
IWishListService wishService = WishListServiceImpl.getInstance();
List<FreeGameVO> freeGameList = new ArrayList<FreeGameVO>();
for(String vo : selectGameList) {
if(wishService.freeGameInWish(vo) != null) {
freeGameList.add(wishService.freeGameInWish(vo));
}
}
for(FreeGameVO vo : freeGameList) {
System.out.println("[리스트] 장바구니에 있는 무료게임 아이디 : "+vo.getGame_id());
}
req.setAttribute("freeGameList", freeGameList);
req.setAttribute("gameInfoList", gameInfoList);
req.getRequestDispatcher("/cart/cartDetail.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
InsertCartController
package orders.cart.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import members.member.vo.MemberVO;
import orders.cart.service.CartServiceImpl;
import orders.cart.service.ICartService;
import orders.cart.vo.CartVO;
import wishList.service.IWishListService;
import wishList.service.WishListServiceImpl;
import wishList.vo.WishListVO;
@WebServlet("/cart/insertCart.do")
public class InsertCartController extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
MemberVO member = (MemberVO) session.getAttribute("loginMember");
String memId = member.getMem_id();
String gameId = req.getParameter("gameId");
System.out.println("insert카트에서 세션의 아이디 잘 들어오나?"+memId);
ICartService cartService = CartServiceImpl.getInstance();
CartVO vo = new CartVO();
vo.setMemId(memId);
vo.setGameId(gameId);
String price = cartService.getPrice(gameId);
vo.setCartPrice(price);
String check = cartService.checkExist(vo);
int cnt1=0;
int cnt2=0;
if(check != null) {
System.out.println("장바구니에 이미 존재함");
} else {
cnt1 = cartService.insertCart(vo);
cnt2 = cartService.insertCartGame(gameId);
}
String msg ="";
if(cnt1>0&&cnt2>0) {
msg = "장바구니 담기 성공";
}else {
msg="장바구니 담기 실패";
}
session.setAttribute("msg", msg);
String redirectUrl = req.getContextPath()+"/detailGame.do?id="+gameId;
resp.sendRedirect(redirectUrl);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
DeleteCartController
package orders.cart.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import members.member.vo.MemberVO;
import orders.cart.service.CartServiceImpl;
import orders.cart.service.ICartService;
import orders.cart.vo.CartVO;
@WebServlet("/cart/deleteCart.do")
public class DeleteCartController extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
MemberVO vvo = (MemberVO) session.getAttribute("loginMember");
String memId = vvo.getMem_id();
System.out.println(memId);
String gameId = req.getParameter("gameId");
CartVO vo = new CartVO();
vo.setGameId(gameId);
vo.setMemId(memId);
ICartService cartService = CartServiceImpl.getInstance();
String cartNo = null;
cartNo = cartService.checkExist(vo);
int cnt1 =0;
int cnt2 =0;
if(cartNo != null) {
cnt1 = cartService.deleteCartGame(cartNo);
cnt2 = cartService.deleteCart(cartNo);
}else {
System.out.println("테이블에 해당 사용자의 게임이 존재하지 않음..........");
}
String msg ="";
if(cnt1>0 && cnt2>0) {
msg = "성공";
}else {
msg="실패";
}
session.setAttribute("msg", msg);
String redirectUrl = req.getContextPath()+"/cart/cartDetail.do";
resp.sendRedirect(redirectUrl);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}