一、明确目标:
1、将订单项表加入到数据库
2、将订单表加入到数据库
3、改变订单项表中数量时书表中的销量会随之改变
二、将订单项表加入到数据库
当加入到购物车的表是订单项表,而最终结算用的是订单表
1、思路:
1、首先要将关于订单项表的属性创建一个实体类
2、接下来写好OrderItemDao层方法
3、接下来就是Action里面的代码
4、最后进行配置
2、实现代码以及运行结果:
Order:
package com.xcx.entity;
public class OrderItem {
private long id;
private long oid;
private String bid;
private int quantity;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getOid() {
return oid;
}
public void setOid(long oid) {
this.oid = oid;
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public OrderItem(long id, long oid, String bid, int quantity) {
super();
this.id = id;
this.oid = oid;
this.bid = bid;
this.quantity = quantity;
}
@Override
public String toString() {
return "OrderItem [id=" + id + ", oid=" + oid + ", bid=" + bid + ", quantity=" + quantity + "]";
}
public OrderItem() {
}
}
OrderItemDao:
package com.xcx.dao;
import com.xcx.entity.OrderItem;
import com.xcx.util.BaseDao;
public class OrderItemDao extends BaseDao<OrderItem>{
public void add(OrderItem t) throws Exception {
String sql="insert into t_easyui_orderitem(oid,bid,quantity) values(?,?,?)";
super.executeUpdate(sql, t, new String[] {"oid","bid","quantity"});
}
}
ShoppingAction:
if (StringUtils.isNotBlank(pageStr) && pageStr.length() > 1) {
for (String item : pageStr.substring(1).split(",")) {
OrderItem oi=new OrderItem();
String[] itemEle=item.split("-");
oi.setBid(itemEle[0]);
oi.setOid(newest.getId());
oi.setQuantity(Integer.valueOf(itemEle[2]));
oid.add(oi);
Book b=new Book();
b.setName(itemEle[0]);
b.setSales(Integer.valueOf(itemEle[2]));
bd.editSales(b);
}
运行结果:
三: 将订单表加入到数据库
Order:
package com.xcx.entity;
import java.util.Date;
public class Order {
private long id;
private long uid;
private Date orderTime;
private String consignee;
private String phone;
private String postalcode;
private String address;
private int sendType;
private Date sendTime;
private float orderPrice;
private int orderState;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
public Date getOrderTime() {
return orderTime;
}
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPostalcode() {
return postalcode;
}
public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getSendType() {
return sendType;
}
public void setSendType(int sendType) {
this.sendType = sendType;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
public float getOrderPrice() {
return orderPrice;
}
public void setOrderPrice(float orderPrice) {
this.orderPrice = orderPrice;
}
public int getOrderState() {
return orderState;
}
public void setOrderState(int orderState) {
this.orderState = orderState;
}
@Override
public String toString() {
return "Order [id=" + id + ", uid=" + uid + ", orderTime=" + orderTime + ", consignee=" + consignee + ", phone="
+ phone + ", postalcode=" + postalcode + ", address=" + address + ", sendType=" + sendType
+ ", sendTime=" + sendTime + ", orderPrice=" + orderPrice + ", orderState=" + orderState + "]";
}
public Order(long id, long uid, Date orderTime, String consignee, String phone, String postalcode, String address,
int sendType, Date sendTime, float orderPrice, int orderState) {
super();
this.id = id;
this.uid = uid;
this.orderTime = orderTime;
this.consignee = consignee;
this.phone = phone;
this.postalcode = postalcode;
this.address = address;
this.sendType = sendType;
this.sendTime = sendTime;
this.orderPrice = orderPrice;
this.orderState = orderState;
}
public Order() {
// TODO Auto-generated constructor stub
}
}
ShoppingAction:
public void createOrder(HttpServletRequest req, HttpServletResponse resp) {
User cuser = (User) req.getSession().getAttribute("cuser");
if (cuser == null) {
return;
}
Order order = new Order();
order.setAddress(vo.getAddress());
order.setConsignee(vo.getConsignee());
float orderPrice = 0f;
String pageStr = vo.getPageStr();
if (StringUtils.isNotBlank(pageStr) && pageStr.length() > 1) {
for (String item : pageStr.substring(1).split(",")) {
String[] itemEle = item.split("-");
orderPrice += Float.valueOf(itemEle[3]);
}
}
order.setOrderPrice(orderPrice);
order.setOrderState(1);
order.setPhone(vo.getPhone());
order.setPostalcode(vo.getSendType());
order.setUid(cuser.getId());
try {
od.add(order);
Order newest=od.list(null, null).get(0);
if (StringUtils.isNotBlank(pageStr) && pageStr.length() > 1) {
for (String item : pageStr.substring(1).split(",")) {
OrderItem oi=new OrderItem();
String[] itemEle=item.split("-");
oi.setBid(itemEle[0]);
oi.setOid(newest.getId());
oi.setQuantity(Integer.valueOf(itemEle[2]));
oid.add(oi);
Book b=new Book();
b.setName(itemEle[0]);
b.setSales(Integer.valueOf(itemEle[2]));
bd.editSales(b);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
改变订单项表中数量时书表中的销量会随之改变:
BookDao:根据名字进行销量的修改
public void editSales(Book t) throws Exception {
super.executeUpdate("update t_easyui_book set sales=sales+? where name=?", t, new String[] { "sales", "name" });
}
ShoppingAction:
Book b=new Book();
b.setName(itemEle[0]);
b.setSales(Integer.valueOf(itemEle[2]));
bd.editSales(b);
效果展示: