一、接口和實現類
package com.aaa.dao; import java.util.List; import java.util.Map; public interface IProductDAO { List<Map<String,Object>> getAllProduct(); /** * 獲得商品的id * @param pid * @return */ Map<String ,Object>getProduct(String pid); /** * 添加購物車 */ boolean addCart(String pid,String num,Integer uid); /** * 判斷某個商品 是否已經加到 購物車 */ boolean isAddCart(String pid,Integer uid); /** * 商品數量的更新 */ boolean updateCart (String pid,String num, Integer uid); /** * 刪除訂單 根據用戶id刪除 */ boolean deleteOrders(Integer uid); Map<String, Object> getProduct(int pid); }
package com.aaa.dao.Impl; import com.aaa.dao.IProductDAO; import com.aaa.util.DBUtil; import java.util.List; import java.util.Map; public class ProductDAOImpl implements IProductDAO { @Override public List<Map<String, Object>> getAllProduct() { String sql="select * from product "; return DBUtil.executeQuery(sql); } @Override public Map<String, Object> getProduct(String pid) { String sql="select * from product where pid=?"; List<Map<String, Object>> list = DBUtil.executeQuery(sql, pid); if (list.size()>0){ return list.get(0); }else { return null; } } /** * 添加購物車的方法 */ public boolean addCart(String pid, String num, Integer uid) { String sql="insert into car (pid,num,uid) values (?,?,?)"; return DBUtil.executeUpdate(sql,pid,num,uid); } /** * 判斷商品是否 已經添加到購物車 * select * from car where pid=? and uid=? * 查詢在 car 數據表中是否有 商品的名字 和 用戶的id */ public boolean isAddCart(String pid, Integer uid) { String sql="select * from car where pid=? and uid=?"; List<Map<String, Object>> list = DBUtil.executeQuery(sql, pid, uid); return list.size()>0; } /** * 商品數量的更新 * @param pid * @param num * @param uid * @return */ public boolean updateCart(String pid, String num, Integer uid) { String sql="insert into set num=num+? where uid=? and pid=?"; return DBUtil.executeUpdate(sql,num,uid,pid); } @Override public boolean deleteOrders(Integer uid) { String sql="delete orders where uid=?"; return DBUtil.executeUpdate(sql, uid); } @Override public Map<String, Object> getProduct(int parseInt) { return null; } }
二、在produc.jsp頁面中跳轉到 AddCartServlet
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"+"views/"; %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML > <html> <head> <base href="<%=basePath%>"> <meta charset="UTF-8"> <title>商品詳情</title> <link rel="stylesheet" type="text/css" href="css/product.css"/> <script type="text/javascript" src="js/product.js"> </script> </head> <%-- when test="${empty map}"語句就是等於 if else語句 判斷的作用 結合otherwise使用 empty map 判斷 是否非空 --%> <body> <!-- 頭部導航 --> <jsp:include page="/views/header.jsp"></jsp:include> <c:choose> <c:when test="${empty map}"> 找不到該頁面 </c:when> <c:otherwise> <div class="wrap"> <img src="${map.pimage}" /> <div class="description"> <form action="/shop/addCart.do" method="post"> <h2>${map.pname}</h2> <div class="old_price"> 原價: <span>¥465</span> </div> <div class="price"> 折扣價: <span>¥465</span> </div> <div> 尺碼:均碼 </div> <input type="text" value="${map.pid}" hidden="hidden" name="pid" id="pid"/> <div class="count"> 數量: <span class="s">-</span> <input type="text" value="1" name="num" class="num" /> <span class="s">+</span> </div> <div> <input type="submit" value="加入購物車" class="goods_cart" /> </div> <div> <input type="submit" value="立即購買" class="buy"/> </div> </form> </div> </div> </c:otherwise> </c:choose> <script> // 如果error 不等於 空 那就是出現 error 就要提示用戶登錄 if ("${error}" != ""){ if (confirm("對不起,您還沒登錄,請先登錄"));{ window.location.href="/shop/views/login.jsp"; } } // 如果 不等於空 就是有 addSuccess 就是添加成功啊 if ("${addSuccess}" != ""){ if (confirm("添加成功,是否去購物車?")){ // 瀏覽器發送請求的方式之一 window.location.href="/shop/myCart.do"; } } </script> </body> </html>
三、servlet 業務操作
package com.aaa.servlet; import com.aaa.dao.IProductDAO; import com.aaa.dao.Impl.ProductDAOImpl; 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 java.io.IOException; import java.util.Map; /** 用戶購物車添加商品功能的實現? * * 1.判斷用戶是否已經登錄? * 我們是為用戶實現購物車 所以判斷用戶是否登錄 * * 登錄? * * 2.獲取用戶的購物車參數 pid num * 獲取用戶的id 是Interger 類型 強轉走一波 * * 3.繼續判斷 購物車中是否有這個商品? * 有 ,就更新數量? * 沒有,就重新去添加商品。 * * 4.添加成功后 請求轉發到product.jsp 頁面 展示給用戶 * */ @WebServlet("/addCart.do") public class AddCartServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Map user = (Map) req.getSession().getAttribute("user"); if (user == null){ req.setAttribute("error","請先登錄"); }else { String pid = req.getParameter("pid"); String num = req.getParameter("num"); Integer uid = (Integer) user.get("uid"); IProductDAO dao=new ProductDAOImpl(); boolean addCart = dao.isAddCart(pid, uid); if (addCart){ dao.updateCart(pid,num,uid); }else{ dao.addCart(pid,num,uid); } req.setAttribute("addSuccess","添加成功"); } req.getRequestDispatcher("/views/product.jsp").forward(req,resp); } }