SSM购物车之购物车模块day04
购买行为 就是创建一条一条的订单项
而显示购物车,也就是把这些订单项显示在页面上。
在这个阶段,订单项都会保存在session中,直到最后生成订单的时候,才会把这些订单项保存在数据库中。
1.SQL
暂时不需要为OrderItem创建表,因为在这个环节OrderItem还是保存在Session中的
2.实体类

1 package cn.gb.pojo; 2 3 public class OrderItem { 4 private String id; 5 private Product product; 6 private Order order; 7 private int num; 8 9 public Order getOrder() { 10 return order; 11 } 12 13 public void setOrder(Order order) { 14 this.order = order; 15 } 16 17 public String getId() { 18 return id; 19 } 20 21 public void setId(String id) { 22 this.id = id; 23 } 24 25 public Product getProduct() { 26 return product; 27 } 28 29 public void setProduct(Product product) { 30 this.product = product; 31 } 32 33 public int getNum() { 34 return num; 35 } 36 37 public void setNum(int num) { 38 this.num = num; 39 } 40 }
3.Mapper层(dao)
4.Service(业务层)
5.Controller(控制层)
1. 获取购买数量
2. 获取购买商品的id
3. 根据id获取商品对象
4. 创建一个新的OrderItem对象
5. 从session中取出一个List , 这个List里面存放陆续购买的商品。
如果是第一次从session中获取该List,那么它会是空的,需要创建一个ArrayList
6. 把新创建的OrderItem对象放入该List 中
7. 跳转到显示购物车的listOrderItem
当加入相同的商品时-----------
遍历session中所有的OrderItem
如果找到对应的product.id一样的条目,就调整其数量
如果没有找到,就新增加一条

1 package cn.gb.controller; 2 3 import cn.gb.pojo.OrderItem; 4 import cn.gb.pojo.Product; 5 import cn.gb.service.ProductService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.http.HttpRequest; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.ResponseBody; 12 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpSession; 15 import java.util.ArrayList; 16 import java.util.List; 17 18 @Controller 19 @RequestMapping("cart") 20 public class CartController { 21 @Autowired 22 private ProductService productService; 23 24 @RequestMapping("showCart") 25 public String showCart( HttpSession session, Model model){ 26 List<OrderItem> ois= (List<OrderItem>)session.getAttribute("carts"); 27 if (null!=ois){ 28 model.addAttribute("ois",ois); 29 } 30 return "/cart.jsp"; 31 } 32 33 @RequestMapping("addCart") 34 @ResponseBody 35 public String addCart(String pid,String num,HttpSession session){ 36 OrderItem oi=new OrderItem(); 37 Product product=productService.info(pid); 38 oi.setNum(Integer.parseInt(num)); 39 oi.setProduct(product); 40 List<OrderItem> itemList= (List<OrderItem>) session.getAttribute("carts"); 41 if (null==itemList){ 42 itemList=new ArrayList<>(); 43 session.setAttribute("carts",itemList); 44 } 45 boolean found=false; 46 for (OrderItem orderItem : itemList){ 47 if (orderItem.getProduct().getId()==oi.getProduct().getId()){ 48 orderItem.setNum(orderItem.getNum()+oi.getNum()); 49 found=true; 50 break; 51 } 52 } 53 if (!found) 54 itemList.add(oi); 55 return "true"; 56 } 57 }
6.jsp页面
加入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/2/2 0002 Time: 10:24 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> < html > < head > < title >Title</ title > < script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"/> < script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></ script > < link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"> </ head > < script > $(function () { $(".addBtn").click(function () { var button=$(this); var pid=$(this).attr("pid"); /* alert(pid);*/ var number=$(".number[pid="+pid+"]").val(); /*alert(number);*/ $.ajax({ url:"${pageContext.request.contextPath}/cart/addCart.action", data:{"pid":pid,"num":number}, success:function (result) { window.location.href="/cart/showCart.action" } }) }) }) </ script > < body > < jsp:include page="header.jsp"/> < table class="table table-striped table-bordered" style="width: 500px;margin: 44px auto" border="1" align="center" cellspacing="0"> < tr > < td >编号</ td > < td >名称</ td > < td >价格</ td > < td >购买</ td > </ tr > < c:forEach items="${list}" var="pro"> < tr > < td >${pro.id}</ td > < td >${pro.name}</ td > < td >${pro.price}</ td > < td > 数量< input pid="${pro.id}" type="text" class="number" value="1" name="num"> < input type="submit" pid="${pro.id}" class="addBtn" value="购买"> </ td > </ tr > </ c:forEach > </ table > < jsp:include page="footer.jsp"/> </ body > </ html > |
展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/2/3 0003 Time: 11:15 To change this template use File | Settings | File Templates. --%> <%@ 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" %> < html > < head > < title >Title</ title > < script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"/> < script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></ script > < link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"> </ head > < form id="orderForm" style="margin-top: 5px; margin-left: 150px;" action="${pageContext.request.contextPath}/order/saveOrder.action" method="post"> < table class="table table-striped table-bordered" style="width: 400px;margin: 44px auto" border="1" align="center" cellspacing="0"> < tr > < td >商品名称</ td > < td >单价</ td > < td >数量</ td > < td >小计</ td > </ tr > < c:forEach var="oi" items="${ois}"> < tr > < td >${oi.product.name}</ td > < td >${oi.product.price}</ td > < td >${oi.num}</ td > < td >< fmt:formatNumber minFractionDigits="1" value="${oi.product.price*oi.num}"></ fmt:formatNumber ></ td > </ tr > </ c:forEach > < c:if test="${!empty ois}"> < tr > < td colspan="5" align="right"> < a href="javascript:document.getElementById('orderForm').submit();"> < img src="${pageContext.request.contextPath}/image/finalbutton.gif" width="204" height="51" border="0" /> </ a > </ td > </ tr > </ c:if > </ table > </ form > < nav > </ nav > </ body > </ html > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器