1 案例分析
1 订单分析1
2 订单分析2
3 订单的业务操作
2 代码实现
1 cart.jsp代码修改
<a href="${pageContext.request.contextPath }/product?method=submitOrder"> <input type="button" width="100" value="提交订单" name="submit" border="0" style="background: url(‘./images/register.gif‘) no-repeat scroll 0 0 rgba(0, 0, 0, 0); height:35px;width:100px;color:white;"></a>
2 ProductServlet代码
//提交订单 public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); //判断用户是否已经登录 未登录下面代码不执行 User user = (User) session.getAttribute("user"); if(user==null){ //没有登录 response.sendRedirect(request.getContextPath()+"/login.jsp"); return; } //目的:封装好一个Order对象 传递给service层 Order order = new Order(); //1、private String oid;//该订单的订单号 String oid = CommonsUtils.getUUID(); order.setOid(oid); //2、private Date ordertime;//下单时间 order.setOrdertime(new Date()); //3、private double total;//该订单的总金额 //获得session中的购物车 Cart cart = (Cart) session.getAttribute("cart"); double total = 0.0; if(cart!=null){ total = cart.getTotal(); } order.setTotal(total); //4、private int state;//订单支付状态 1代表已付款 0代表未付款 order.setState(0); //5、private String address;//收货地址 order.setAddress(null); //6、private String name;//收货人 order.setName(null); //7、private String telephone;//收货人电话 order.setTelephone(null); //8、private User user;//该订单属于哪个用户 order.setUser(user); //9、该订单中有多少订单项List<OrderItem> orderItems = new ArrayList<OrderItem>(); //获得购物车中的购物项的集合map Map<String, CartItem> cartItems = cart.getCartItems(); for(Map.Entry<String, CartItem> entry : cartItems.entrySet()){ //取出每一个购物项 CartItem cartItem = entry.getValue(); //创建新的订单项 OrderItem orderItem = new OrderItem(); //1)private String itemid;//订单项的id orderItem.setItemid(CommonsUtils.getUUID()); //2)private int count;//订单项内商品的购买数量 orderItem.setCount(cartItem.getBuyNum()); //3)private double subtotal;//订单项小计 orderItem.setSubtotal(cartItem.getSubtotal()); //4)private Product product;//订单项内部的商品 orderItem.setProduct(cartItem.getProduct()); //5)private Order order;//该订单项属于哪个订单 orderItem.setOrder(order); //将该订单项添加到订单的订单项集合中 order.getOrderItems().add(orderItem); } //order对象封装完毕 //传递数据到service层 ProductService service = new ProductService(); service.submitOrder(order); session.setAttribute("order", order); //页面跳转 response.sendRedirect(request.getContextPath()+"/order_info.jsp"); }
3 ProductService代码
//提交订单 将订单的数据和订单项的数据存储到数据库中 public void submitOrder(Order order) { ProductDao dao = new ProductDao(); try { //1、开启事务 DataSourceUtils.startTransaction(); //2、调用dao存储order表数据的方法 dao.addOrders(order); //3、调用dao存储orderitem表数据的方法 dao.addOrderItem(order); } catch (SQLException e) { try { DataSourceUtils.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { try { DataSourceUtils.commitAndRelease(); } catch (SQLException e) { e.printStackTrace(); } } }
4 ProductDao代码
//向orders表插入数据 public void addOrders(Order order) throws SQLException { QueryRunner runner = new QueryRunner(); String sql = "insert into orders values(?,?,?,?,?,?,?,?)"; Connection conn = DataSourceUtils.getConnection(); runner.update(conn,sql, order.getOid(),order.getOrdertime(),order.getTotal(),order.getState(), order.getAddress(),order.getName(),order.getTelephone(),order.getUser().getUid()); } //向orderitem表插入数据 public void addOrderItem(Order order) throws SQLException { QueryRunner runner = new QueryRunner(); String sql = "insert into orderitem values(?,?,?,?,?)"; Connection conn = DataSourceUtils.getConnection(); List<OrderItem> orderItems = order.getOrderItems(); for(OrderItem item : orderItems){ runner.update(conn,sql,item.getItemid(),item.getCount(),item.getSubtotal(),item.getProduct().getPid(),item.getOrder().getOid()); } }
5 order_info.jsp修改代码
<table class="table table-bordered"> <tbody> <tr class="warning"> <th colspan="5">订单编号:${order.oid }</th> </tr> <tr class="warning"> <th>图片</th> <th>商品</th> <th>价格</th> <th>数量</th> <th>小计</th> </tr> <c:forEach items="${order.orderItems }" var="orderItem"> <tr class="active"> <td width="60" width="40%"> <img src="${pageContext.request.contextPath }/${orderItem.product.pimage}" width="70" height="60"> </td> <td width="30%"><a target="_blank">${orderItem.product.pname}</a></td> <td width="20%">¥${orderItem.product.shop_price}</td> <td width="10%">${orderItem.count}</td> <td width="15%"><span class="subtotal">¥${orderItem.subtotal }</span></td> </tr> </c:forEach> </tbody> </table>
原文地址:https://www.cnblogs.com/jepson6669/p/8448025.html
时间: 2024-10-11 17:51:50