分析订单表和订单项表
一个用户可以有多个订单
订单表: order
列名 |
类型 |
说明 |
备注 |
id |
int |
id |
主键 |
total |
float |
总价 |
|
amount |
int |
商品总数 |
|
status |
tinyint |
订单状态 |
(1未付款/2已付款/3已发货/4已完成) |
paytype |
tinyint |
支付方式 |
(1微信/2支付宝/3货到付款) |
name |
varchar |
收货人 |
|
phone |
varchar |
收货电话 |
|
address |
varchar |
收货地址 |
|
datetime |
timestamp |
下单时间 |
|
user_id |
int |
下单用户 |
外键 |
订单项表: orderitem
列名 |
类型 |
说明 |
备注 |
id |
int |
id |
主键 |
price |
float |
购买时价格 |
|
amount |
int |
数量 |
|
goods_id |
int |
产品id |
外键 |
order_id |
int |
订单id |
外键 |
price购买时的价格,因为一段时间后它的价格会发生变化
在MySQL中进行创建表:
order:
orderitem:
外键的添加:外键名不能重复
创建订单和订单项对应的数据模型类
model层中创建一个Order.java类和一个OrderItem.java
package com.guiyan.model; import java.sql.Date; public class Order { private int id; private float total;//总价 private int amount;//商品总数 private int status;//1未付款/2已付款/3已发货/4已完成 private int paytype;//1微信/2支付宝/货到付款 private String name; private String phone; private String address; private Date datetime; private User user; public int getId() { return id; } public void setId(int id) { this.id = id; } public float getTotal() { return total; } public void setTotal(float total) { this.total = total; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public int getPaytype() { return paytype; } public void setPaytype(int paytype) { this.paytype = paytype; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getDatetime() { return datetime; } public void setDatetime(Date datetime) { this.datetime = datetime; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Order() { super(); } }
1 package com.guiyan.model; 2 3 public class OrderItem { 4 5 private int id; 6 private float price; 7 private int amount; 8 private Goods goods; 9 private Order order;//order_id 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public int getAmount() { 23 return amount; 24 } 25 public void setAmount(int amount) { 26 this.amount = amount; 27 } 28 public Goods getGoods() { 29 return goods; 30 } 31 public void setGoods(Goods goods) { 32 this.goods = goods; 33 } 34 public Order getOrder() { 35 return order; 36 } 37 public void setOrder(Order order) { 38 this.order = order; 39 } 40 public OrderItem() { 41 super(); 42 } 43 44 45 }
完成发起购买的请求:
通过触发onclick事件
通过buy()函数来实现的
通过jQuery中的ajax方法来请求来实现加入购物车的实现:
jQuery中ajax数据请求的方法:详细介绍请看网址:http://api.jquery.com/jQuery.post/
里面有示例:
测试点击加入购物车时,获取其id:
cart.js中的ajax
$.post("goods_buy", {goodsid:goodid}, function(data) });
GoodsBuyServlet.java
@WebServlet("/goods_buy") public class GoodsBuyServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Map<String,String[]> map=request.getParameterMap(); // for(String key:map.keySet()) { // System.out.println(key+":"+map.get(key)); // } System.out.println(request.getParameter("goodsid")); }
订单对象的生成:
在order.java中加入:
private Map<Integer,OrderItem> itemMap=new HashMap<Integer,OrderItem>(); public void addGoods(Goods g) { //订单项存在时 if(itemMap.containsKey(g.getId())) { OrderItem item=itemMap.get(g.getId()); item.setAmount(item.getAmount()+1); amount++; total+=g.getPrice(); }else {//不存在时 OrderItem item=new OrderItem(g.getPrice(),1,g,this); itemMap.put(g.getId(), item); } } //使用Map集合,通过商品的id
并在orderitem.java中创建构造方法,不需要id
public OrderItem(float price, int amount, Goods goods, Order order) { super(); this.price = price; this.amount = amount; this.goods = goods; this.order = order; }
添加商品到购物车:
GoodsBuyServlet.java加入:
private GoodsService gService=new GoodsService();//作为查询 int goodsid=Integer.parseInt(request.getParameter("goodsid")); Goods goods=gService.getById(goodsid); //判断库存是否够 if(goods.getStock()>0) { o.addGoods(goods); response.getWriter().print("ok"); }else { response.getWriter().print("fail"); }
cart.js中
/** * 加入购物车 */ function buy(goodid){ $.post("goods_buy", {goodsid:goodid}, function(data){ alert(data); if(data=="ok"){ layer.msg("已添加到购物车!!!", {time:800}, function(){ location.reload(); }); }else if(data=="fail"){ layer.msg("库存不足,请购买其它商品!!!", {time:800}, function(){ }); }
最终效果: