一、接口和实现类
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); } }
原文地址:https://www.cnblogs.com/ZXF6/p/10777188.html
时间: 2024-10-10 12:28:33