网上图书商城项目学习笔记-018生成订单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网上图书商城项目学习笔记-018生成订单相关的知识,希望对你有一定的参考价值。
一、流程分析
1.订单模块介绍
2.生成订单流程
二、代码
1.view层
(1)showitem.jsp
1 <form id="form1" action="<c:url value=‘/OrderServlet‘/>" method="post"> 2 <input type="hidden" name="cartItemIds" value="${cartItemIds }"/> 3 <input type="hidden" name="method" value="createOrder"/> 4 <table width="95%" align="center" cellpadding="0" cellspacing="0"> 5 <tr bgcolor="#efeae5"> 6 <td width="400px" colspan="5"><span style="font-weight: 900;">生成订单</span></td> 7 </tr> 8 <tr align="center"> 9 <td width="10%"> </td> 10 <td width="50%">图书名称</td> 11 <td>单价</td> 12 <td>数量</td> 13 <td>小计</td> 14 </tr> 15 16 17 <c:forEach items="${items }" var="item"> 18 <tr align="center"> 19 <td align="right"> 20 <a class="linkImage" href="<c:url value=‘/jsps/book/desc.jsp‘/>"><img border="0" width="54" align="top" src="<c:url value=‘${item.book.image_b }‘/>"/></a> 21 </td> 22 <td align="left"> 23 <a href="<c:url value=‘/jsps/book/desc.jsp‘/>"><span>${item.book.bname }</span></a> 24 </td> 25 <td>¥${item.book.currPrice }</td> 26 <td>${item.quantity }</td> 27 <td> 28 <span class="price_n">¥<span class="subtotal">${item.subtotal }</span></span> 29 </td> 30 </tr> 31 </c:forEach> 32 33 <tr> 34 <td colspan="6" align="right"> 35 <span>总计:</span><span class="price_t">¥<span id="total">${total }</span></span> 36 </td> 37 </tr> 38 <tr> 39 <td colspan="5" bgcolor="#efeae5"><span style="font-weight: 900">收货地址</span></td> 40 </tr> 41 <tr> 42 <td colspan="6"> 43 <input id="addr" type="text" name="address" value="北京市 昌平区 西三旗 金燕龙办公楼1层 传智播客 张三爷"/> 44 </td> 45 </tr> 46 <tr> 47 <td style="border-top-width: 4px;" colspan="5" align="right"> 48 <a id="linkSubmit" href="javascript:$(‘#form1‘).submit();">提交订单</a> 49 </td> 50 </tr> 51 </table> 52 </form>
(2)ordersucc.js
1 <div class="div2"> 2 <img src="<c:url value=‘/images/duihao.jpg‘/>" class="img"/> 3 <dl> 4 <dt>订单编号</dt> 5 <dd>${order.oid }</dd> 6 <dt>合计金额</dt> 7 <dd><span class="price_t">¥${order.total }</span></dd> 8 <dt>收货地址</dt> 9 <dd>${order.address }</dd> 10 </dl> 11 <span>ITCAST书城感谢您的支持,祝您购物愉快!</span> 12 <a href="<c:url value=‘/jsps/order/pay.jsp‘/>" id="linkPay">支付</a> 13 </div>
2.servlet层
(1)OrderServlet.java
1 /** 2 * 生成订单 3 * @param req 4 * @param resp 5 * @return 6 * @throws ServletException 7 * @throws IOException 8 */ 9 public String createOrder(HttpServletRequest req, HttpServletResponse resp) 10 throws ServletException, IOException { 11 // 1. 获取所有购物车条目的id,查询之 12 String ids = req.getParameter("cartItemIds"); 13 List<CartItem> cartItems = itemService.loadCartItems(ids); 14 if(cartItems.size() == 0) { 15 req.setAttribute("code", "error"); 16 req.setAttribute("msg", "您没有选择要购买的图书,不能下单!"); 17 return "f:/jsps/msg.jsp"; 18 } 19 20 // 2. 创建Order 21 Order order = new Order(); 22 order.setOid(CommonUtils.uuid()); 23 order.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 24 order.setStatus(1); 25 order.setAddress(req.getParameter("address")); 26 User user = (User) req.getSession().getAttribute("sessionUser"); 27 order.setUser(user); 28 29 //设置total 30 BigDecimal total = new BigDecimal("0"); 31 for(CartItem item : cartItems) { 32 total = total.add(item.getSubtotal()); 33 } 34 order.setTotal(total); 35 36 //设置订单对应的orderItems 37 List<OrderItem> orderItems = toOrderItemList(cartItems, order); 38 order.setOrderItems(orderItems); 39 40 service.createOrder(order); 41 itemService.batchDelete(ids); // 删除购物车条目 42 43 req.setAttribute("order", order); 44 return "/jsps/order/ordersucc.jsp"; 45 } 46 47 private List<OrderItem> toOrderItemList(List<CartItem> cartItems, Order order) { 48 if(cartItems == null || cartItems.size() == 0) return null; 49 List<OrderItem> orderItems = new ArrayList<OrderItem>(); 50 OrderItem orderItem = null; 51 for(CartItem cartItem : cartItems){ 52 orderItem = toOrderItem(cartItem); 53 orderItem.setOrder(order); 54 orderItems.add(orderItem); 55 } 56 return orderItems; 57 } 58 59 private OrderItem toOrderItem(CartItem cartItem) { 60 OrderItem orderItem = new OrderItem(); 61 orderItem.setOrderItemId(CommonUtils.uuid()); 62 orderItem.setQuantity(cartItem.getQuantity()); 63 orderItem.setSubtotal(cartItem.getSubtotal()); 64 orderItem.setBook(cartItem.getBook()); 65 return orderItem; 66 }
3.service层
(1)OrderService.java
1 /** 2 * 生成订单 3 * @param order 4 */ 5 public void createOrder(Order order) { 6 try { 7 JdbcUtils.beginTransaction(); 8 dao.add(order); 9 JdbcUtils.commitTransaction(); 10 } catch (SQLException e) { 11 try { 12 JdbcUtils.rollbackTransaction(); 13 } catch (SQLException e1) { 14 e1.printStackTrace(); 15 } 16 e.printStackTrace(); 17 } 18 }
4.dao层
(1)OrderDao.java
1 /** 2 * 生成订单 3 * @param order 4 * @throws SQLException 5 */ 6 public void add(Order order) throws SQLException { 7 /* 8 * 1. 插入订单 9 */ 10 String sql = "insert into t_order values (?,?,?,?,?,?)"; 11 Object [] params = {order.getOid(), order.getOrderTime(), order.getTotal(), order.getStatus(), order.getAddress(), order.getUser().getUid()}; 12 qr.update(sql, params); 13 14 /* 15 * 2. 循环遍历订单的所有条目,让每个条目生成一个Object[] 16 * 多个条目就对应Object[][] 17 * 执行批处理,完成插入订单条目 18 */ 19 sql = "insert into t_orderItem values (?,?,?,?,?,?,?,?)"; 20 List<OrderItem> items = order.getOrderItems(); 21 int len = items.size(); 22 Object [][] params2 = new Object[len][]; 23 OrderItem item = null; 24 Book book = null; 25 for(int i=0 ; i<len ; i++) { 26 item = items.get(i); 27 book = item.getBook(); 28 params2[i] = new Object[]{item.getOrderItemId(), item.getQuantity(), item.getSubtotal(),book.getBid(), 29 book.getBname(), book.getCurrPrice(), book.getImage_b(), item.getOrder().getOid()}; 30 } 31 qr.batch(sql, params2); 32 }
以上是关于网上图书商城项目学习笔记-018生成订单的主要内容,如果未能解决你的问题,请参考以下文章