一个订单应该包括一个订单项,和一个订单
订单项的商品id关联商品表,订单项的订单id关联订单表
一个订单项应该包括:订单项ID,商品ID,订单ID,数量和小计
一个订单表应该包括:订单ID,订单时间,订单总金,收货人,电话,地址,状态,用户ID
一个订单项的实体:
class OrderItem { //订单项ID private String itemid; //数量 private Integer count; //小计 private Double subtotal; //订单项id直接由订单实体对象代替 private Order order; //商品ID也由商品实体对象代替 private Product product; public String getItemid() { return itemid; } public void setItemid(String itemid) { this.itemid = itemid; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public Double getSubtotal() { return subtotal; } public void setSubtotal(Double subtotal) { this.subtotal = subtotal; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
一个订单的实体:
class Order{ //订单的ID private String oid; //订单的创建时间 private Date ordertime; //订单的总金额 private Double total; //订单的状态 private Integer state; // 1:未付款 2:已经付款,未发货. 3:已结发货,没有确认收货. 4:已结确认收货,订单结束. //订单的地址 private String address; //订单的收件人 private String name; //订单的收件人电话 private String telephone; //订单关联的用户信息id,由用户实体对象代替 private User user; //订单关联的订单项的集合 private List<OrderItem> orderItems = new ArrayList<OrderItem>(); public String getOid() { return oid; } public void setOid(String oid) { this.oid = oid; } public Date getOrdertime() { return ordertime; } public void setOrdertime(Date ordertime) { this.ordertime = ordertime; } public Double getTotal() { return total; } public void setTotal(Double total) { this.total = total; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<OrderItem> getOrderItems() { return orderItems; } public void setOrderItems(List<OrderItem> orderItems) { this.orderItems = orderItems; } }
在用户点击提交订单的时候,需要清空购物车,跳转到一个servlet的saveCart()方法
在这个方法中要封装order和orderItem对象
下面是这个方法,省略service层和dao层
public String saveOrder(HttpServletRequest request,HttpServletResponse response) { //首先创建order实体对象 Order order = new order(); //设置订单的编号,由uuid的工具类直接生成 order.setOid(UUIDUtils.getUUID()); //设置订单的生成时间 order.setOrderTime(new Date()); //设置付款状态 order.setState(1); //首先获得购物车对象, Cart cart = (Cart) request.getSession().getAttribute("cart"); //判断购物车是否为空 if(cart == null) { request.setAttribute("msg", "购物车是空的!"); return "/jsp/msg.jsp"; } //设置总金额 order.setTotal(cart.getTotal()); //获得User对象 User user = (User) request.getSession().getAttribute("existUser"); //判断用户是否登录 if(user == null) { request.setAttribute("msg", "您还没有登录!"); return "/jsp/login.jsp"; } //设置订单的客户 order.setUser(user); //创建订单项的实体 OrderItem orderItem = new OrderItem(); //因为不确定一个订单中有多少个订单项,所以遍历为购物车中的购物项设置值 for(CartItem cartItem:cart.getMap().values()) { //设置订单项id orderItem .setItemid(UUIDUtils.getUUID()); //设置数量 orderItem.setCount(cartItem.getCount()); //设置小计 orderItem.setSubtotal(cartItem.getSubtotal()); //设置商品 orderItem.setProduct(cartItem.getProduct()); //设置订单 orderItem.setOrder(order); } //调用业务层保存把order保存到数据库中 OrderService os = new OrderServiceImpl(); os.save(order); //订单一旦生成就清空购物车 cart.clearCart(); //把order保存到域对象然后转发 request.setAttribute("order", order); return "/jsp/order_info.jsp"; }