SSM购物车之订单模块day05
在购物车页面,增加一个确实订单的超链接。通过创建订单,把order_表里新增一条数据,同时把session中的订单项条目都保存在数据库。
1.SQL
CREATE TABLE order_( id VARCHAR(100), uid INT(11) ) ENGINE=INNODB DEFAULT CHARSET=utf8; CREATE TABLE orderitem( id VARCHAR(100), pid INT(11), num INT(11), oid VARCHAR(100),
ordertime DATETIME ) ENGINE=INNODB DEFAULT CHARSET=utf8;
2.实体类
在之前的orderitem基础上加入Order属性
package cn.gb.pojo; public class OrderItem { private String id; private Product product; private Order order; private int num; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
package cn.gb.pojo; public class Order { String id; User user; public String getId() { return id; } public void setId(String id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
3.Mapper层(DAO层)
package cn.gb.mapper; import cn.gb.pojo.Order; import cn.gb.pojo.OrderItem; public interface OrderMapper { void saveOrder(Order order); void saveOrderItems(OrderItem orderItem); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.gb.mapper.OrderMapper"> <insert id="saveOrder" parameterType="Order"> INSERT INTO order_ VALUES (#{id},#{user.id}) </insert> <insert id="saveOrderItems" parameterType="OrderItem"> INSERT INTO orderitem VALUES (#{id},#{product.id},#{order.id},#{num}) </insert> </mapper>
4.Service层(业务层)
package cn.gb.service; import cn.gb.mapper.OrderMapper; import cn.gb.mapper.ProductMapper; import cn.gb.pojo.Order; import cn.gb.pojo.OrderItem; import cn.gb.pojo.Product; import cn.gb.pojo.User; import cn.gb.utils.UUIDUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @Service @Transactional public class OrderService { @Autowired private OrderMapper orderMapper; @Autowired private ProductMapper mapper; //创建订单 public String saveOrder(User user){ Order order=new Order(); order.setId(UUIDUtils.getUUID()); order.setUser(user); orderMapper.saveOrder(order); List<OrderItem> products=new ArrayList<>(); OrderItem item=new OrderItem(); item.setId(UUIDUtils.getUUID()); item.setOrder(order); for (OrderItem orderItem : products){ orderMapper.saveOrderItems(orderItem); } return order.getId(); } }
5.Controller层(控制层)
package cn.gb.controller; import cn.gb.pojo.Product; import cn.gb.pojo.User; import cn.gb.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; import java.util.List; @Controller @RequestMapping("order") public class OrderController { @Autowired private OrderService orderService; @RequestMapping("saveOrder") @ResponseBody public String saveOrder(HttpSession session){ List<Product> products= (List<Product>) session.getAttribute("carts"); User user= (User) session.getAttribute("user"); String orderid=orderService.saveOrder(user); return "ok"; } }
6.jsp页面
<%-- 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>