(二十)生成订单

Posted Michael2397

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(二十)生成订单相关的知识,希望对你有一定的参考价值。

案例4-生成订单
需求:
    在购物车页面上,有i一个提交订单,点击的时候,将用户购物车中的商品添加到数据库中.
实体:
    用户
    订单
    订单项(中间表)
    商品

需要在order实体提供 user对象和list<OrderItem>
需要在orderItem实体中提供 product对象和order对象

 

创建表

订单表
        CREATE TABLE `orders` (
          `oid` varchar(32) NOT NULL,
          `ordertime` datetime DEFAULT NULL,
          `total` double DEFAULT NULL,
          `state` int(11) DEFAULT NULL,
          `address` varchar(30) DEFAULT NULL,
          `name` varchar(20) DEFAULT NULL,
          `telephone` varchar(20) DEFAULT NULL,
          `uid` varchar(32) DEFAULT NULL,
          PRIMARY KEY (`oid`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 订单项表
        CREATE TABLE `orderitem` (
          `itemid` varchar(32) NOT NULL,
          `count` int(11) DEFAULT NULL,
          `subtotal` double DEFAULT NULL,
          `pid` varchar(32) DEFAULT NULL,
          `oid` varchar(32) DEFAULT NULL,
          PRIMARY KEY (`itemid`),
          KEY `fk_0001` (`pid`),
          KEY `fk_0002` (`oid`),
          CONSTRAINT `fk_0001` FOREIGN KEY (`pid`) REFERENCES `product` (`pid`),
          CONSTRAINT `fk_0002` FOREIGN KEY (`oid`) REFERENCES `orders` (`oid`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

创建bean

com.louis.domain.Order

package com.louis.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class Order implements Serializable{
/*
 * `oid` varchar(32) NOT NULL,
  `ordertime` datetime DEFAULT NULL,
  `total` double DEFAULT NULL,
  
  `state` int(11) DEFAULT NULL,
  `address` varchar(30) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  
  `telephone` varchar(20) DEFAULT NULL,
  `uid` varchar(32) DEFAULT NULL,
 */
    private String oid;
    private Date ordertime;
    private Double total;
    
    private Integer state=0;//订单状态  0:未支付  1:已支付 
    private String address;
    private String name;
    
    private String telephone;
    
    //属于那个用户
    private User user;
    

    //包含那些订单项
    private List<OrderItem> items=new LinkedList<>();
    
     //“属于”对应mysql中的字段,"包含"则不用,但“被包含”会对应mysql字段
    
    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> getItems() {
        return items;
    }

    public void setItems(List<OrderItem> items) {
        this.items = items;
    }

}

com.louis.domain.OrderItem

package com.louis.domain;

import java.io.Serializable;

public class OrderItem implements Serializable{
/**
 * `itemid` varchar(32) NOT NULL,
  `count` int(11) DEFAULT NULL,
  `subtotal` double DEFAULT NULL,
  `pid` varchar(32) DEFAULT NULL,
  `oid` varchar(32) DEFAULT NULL,
 */
    
    private String itemid;
    private Integer count;
    private Double subtotal;
    
    //包含那个商品
    private Product product;
    
    //属于那个订单
    private Order order;

    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;
    }
    
    
}

 

问题

1、mysql主键外键

 

以上是关于(二十)生成订单的主要内容,如果未能解决你的问题,请参考以下文章

(二十二)查询订单的详情

(二十一)查询我的订单

开源web框架django知识总结(二十)

开源web框架django知识总结(二十)

开源web框架django知识总结(二十)

谷粒商城二十订单服务