码迷,mamicode.com
首页 > 其他好文 > 详细

(二十)生成订单

时间:2017-10-14 21:19:58      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:tco   pid   ble   primary   utf8   需要   serial   创建表   col   

案例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主键外键

 

(二十)生成订单

标签:tco   pid   ble   primary   utf8   需要   serial   创建表   col   

原文地址:http://www.cnblogs.com/Michael2397/p/7668333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!