1.Create a table:
CREATE TABLE `orders` ( `oid` int(11) NOT NULL AUTO_INCREMENT, `total` double DEFAULT NULL, `ordertime` datetime DEFAULT NULL, `state` int(11) DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `addr` varchar(100) DEFAULT NULL, `phone` varchar(20) DEFAULT NULL, `uid` int(11) DEFAULT NULL, PRIMARY KEY (`oid`), KEY `FKC3DF62E5AA3D9C7` (`uid`), CONSTRAINT `FKC3DF62E5AA3D9C7` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; CREATE TABLE `orderitem` ( `itemid` int(11) NOT NULL AUTO_INCREMENT, `count` int(11) DEFAULT NULL, `subtotal` double DEFAULT NULL, `pid` int(11) DEFAULT NULL, `oid` int(11) DEFAULT NULL, PRIMARY KEY (`itemid`), KEY `FKE8B2AB6166C01961` (`oid`), KEY `FKE8B2AB6171DB7AE4` (`pid`), CONSTRAINT `FKE8B2AB6166C01961` FOREIGN KEY (`oid`) REFERENCES `orders` (`oid`), CONSTRAINT `FKE8B2AB6171DB7AE4` FOREIGN KEY (`pid`) REFERENCES `product` (`pid`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
2.bean
public class Order { private Integer oid; private Double total; private Date ordertime; private String name; private String addr; private String phone; private Integer state; // 与用户的关联关系 private User user; // 与订单项关联关系 private Set<OrderItem> orderItems = new HashSet<OrderItem>(); .... }
public class OrderItem { private Integer itemid; private Integer count; private Double subtotal; private Product product; private Order order; .... }
3. Generate order
public String createOrder() { // 将Order对象存入到数据库中: // 封装Order对象: Order order = new Order(); // 封装总价---从购物车的信息获得. // 获得购物车: Cart cart = (Cart) ServletActionContext.getRequest().getSession() .getAttribute("cart"); // 判断: if (cart == null) { this.addActionError("亲!您还没有购物!请先去购物!"); return "msg"; } // 设置所属用户: User existUser = (User) ServletActionContext.getRequest().getSession() .getAttribute("existUser"); if (existUser == null) { this.addActionError("亲!您还没有登录!请先去登录!"); return "msg"; } order.setUser(existUser); order.setTotal(cart.getTotal()); // 封装时间 order.setOrdertime(new Date()); // 封装状态 order.setState(1); // 1 未付款 2 已经付款,未发货 3.已经发货,没有确认收货 4.订单完成. // 为订单设置订单项集合: for (CartItem cartItem : cart.getCartItems()) { // 将购物项的数据封装到订单项中. OrderItem orderItem = new OrderItem(); orderItem.setCount(cartItem.getCount()); orderItem.setSubtotal(cartItem.getSubtotal()); orderItem.setProduct(cartItem.getProduct()); orderItem.setOrder(order); // 放入订单的集合: order.getOrderItems().add(orderItem); } // 购物车清空了. cart.clearCart(); // 调用Service保存订单的操作: orderService.save(order); // 将订单存入到值栈中: ActionContext.getContext().getValueStack().set("order", order); // 页面跳转: return "createOrderSuccess"; }
4. Check my order
1.Query the order based on the user’s id
1.Follow orderidQuery order