一.效果预览
二.实现思路
2.1 实现结算
①形成订单,并赋值
//从session中拿购物车 List<OrderItem> items =(List<OrderItem>)session.getAttribute("mycart"); //从session中拿用户 User user = (User)session.getAttribute("user"); //形成一个订单 Order order = new Order(); //给订单赋值 缺少oid!!!!!!! order.setItems(items); order.calcSum();//务必先给集合赋值 order.setUser(user); order.setOaddress(user.getUaddress()); //调用增加订单的方法 int n = iob.addOrder(order); if(n>0) { //说明结算成功 session.setAttribute("mycart", null); //跳转到我的订单页面 resp.sendRedirect("orders.do"); } else { resp.sendRedirect("cart.jsp?index=5"); }
②将订单插入到数据表tb_order中,同时将该订单所对应的订单项插入到数据表tb_orderitem中
package com.zking.entity; /** * 实体类订单类 * @author PC * */ import java.io.Serializable; import java.util.List; public class Order implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private int oid;//订单编号 private double osumprice;//订单总价 private User user;//用户对象 private String oaddress;//订单地址 //一个订单由多个订单项组成的√ private List<OrderItem> items=null; //计算订单总价 public void calcSum() { double sum = 0; for (OrderItem oi : items) { sum+=oi.getOiprice();//累加求小计的和 } this.osumprice=sum;//再计算总价 } public int getOid() { return oid; } public void setOid(int oid) { this.oid = oid; } public double getOsumprice() { return osumprice; } public void setOsumprice(double osumprice) { this.osumprice = osumprice; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getOaddress() { return oaddress; } public void setOaddress(String oaddress) { this.oaddress = oaddress; } public List<OrderItem> getItems() { return items; } public void setItems(List<OrderItem> items) { this.items = items; } public Order() { // TODO Auto-generated constructor stub } public Order(int oid, double osumprice, User user, String oaddress, List<OrderItem> items) { this.oid = oid; this.osumprice = osumprice; this.user = user; this.oaddress = oaddress; this.items = items; } public Order(double osumprice, User user, String oaddress, List<OrderItem> items) { this.osumprice = osumprice; this.user = user; this.oaddress = oaddress; this.items = items; } @Override public String toString() { return "Order [oid=" + oid + ", osumprice=" + osumprice + ", user=" + user + ", oaddress=" + oaddress + ", items=" + items + "]"; } }
package com.zking.entity; import java.io.Serializable; /** * 实体类:订单项类 * @author PC * */ public class OrderItem implements Serializable{ private static final long serialVersionUID = 1L; private int oiid;//订单项编号 private int oid;//订单编号 // private int gid;//商品编号 private Goods goods;//商品对象 可以间接拿到其所有数据 private int oinum;//购买数量 private double oiprice;//小计 public void calcxj() { //小计=单价*数量 this.oiprice=this.goods.getGprice()*this.oinum; String ss = String.format("%.1f",oiprice); double s = Double.parseDouble(ss); this.oiprice=s; } public int getOiid() { return oiid; } public void setOiid(int oiid) { this.oiid = oiid; } public int getOid() { return oid; } public void setOid(int oid) { this.oid = oid; } public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; } public int getOinum() { return oinum; } public void setOinum(int oinum) { this.oinum = oinum; } public double getOiprice() { return oiprice; } public void setOiprice(double oiprice) { this.oiprice = oiprice; } public OrderItem() { // TODO Auto-generated constructor stub } public OrderItem(int oiid, int oid, Goods goods, int oinum, double oiprice) { this.oiid = oiid; this.oid = oid; this.goods = goods; this.oinum = oinum; this.oiprice = oiprice; } public OrderItem(int oid, Goods goods, int oinum, double oiprice) { this.oid = oid; this.goods = goods; this.oinum = oinum; this.oiprice = oiprice; } @Override public String toString() { return "OrderItem [oiid=" + oiid + ", oid=" + oid + ", goods=" + goods + ", oinum=" + oinum + ", oiprice=" + oiprice + "]"; } }
package com.zking.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.zking.entity.Order; import com.zking.entity.OrderItem; import com.zking.entity.User; import com.zking.util.DBHelper; /** * 数据访问层 tb_order * @author PC * */ public class OrderDao implements IOrderDao { private Connection con = null; private PreparedStatement ps = null; private ResultSet rs = null; boolean flag; /** * 根据用户拿到订单 * @param u 用户对象 * @return 订单集合 */ public List<Order> getAllByUser(User u){ List<Order> ls = new ArrayList<Order>(); try { con=DBHelper.getCon(); String sql="select * from (select * from tb_order order by oid desc) where uuid="+u.getUuid(); ps=con.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()) { Order o =new Order(); o.setOid(rs.getInt(1)); String ss = String.format("%.1f",rs.getDouble(2)); double s = Double.parseDouble(ss); o.setOsumprice(s); o.setUser(u); o.setOaddress(rs.getString(4)); ls.add(o); } } catch (Exception e) { e.printStackTrace(); }finally { DBHelper.myClose(con, ps, rs); } return ls; } /** * 单个查询 * @param oid * @return */ public Order getOrder(String oid) { Order o = new Order(); OrderItemDao odd = new OrderItemDao(); try { con=DBHelper.getCon(); String sql="select * from tb_orderitem where oid="+oid; ps=con.prepareStatement(sql); rs=ps.executeQuery(); if(rs.next()) { o.setOid(rs.getInt(1)); o.setOsumprice(rs.getDouble(2)); o.setOaddress(rs.getString(4)); //拿的该订单所对应的订单项集合 List<OrderItem> items = odd.getAllByOid(oid); o.setItems(items); } } catch (Exception e) { e.printStackTrace(); }finally { DBHelper.myClose(con, ps, rs); } return o; } /** * 增加订单 * @param order 订单对象 * @return 影响行数 */ public int addOrder(Order order) { int n =0; int oid = getID("tb_order", "oid");///!!!赋值 订单编号 OrderItemDao odao = new OrderItemDao(); try { con=DBHelper.getCon(); String sql="insert into tb_order(oid,osumprice,uuid,oaddress) values(?,?,?,?)"; ps=con.prepareStatement(sql); //给占位符赋值 ps.setInt(1, oid); ps.setDouble(2, order.getOsumprice()); ps.setInt(3, order.getUser().getUuid()); ps.setString(4, order.getOaddress()); n=ps.executeUpdate(); if(n>0) {//说明订单下单成功 //那么就把该订单所对应的订单项全部加入到数据库中 //拿到该订单所对应的订单项集合 List<OrderItem> items = order.getItems(); //循环遍历 for (OrderItem oi : items) { //接下来我需要将所有的oi都要进tb_orderitem表 oi.setOid(oid);//关键 odao.addOrderItem(oi); //把对应商品的销量+购买数量 库存-购买数量 new GoodsDao().updateXL(oi.getGoods().getGid(),oi.getOinum()); } } } catch (Exception e) { e.printStackTrace(); }finally { DBHelper.myClose(con, ps, rs); } return n; } /** * 删除订单 * @param oid * @return */ public boolean deleteOrderByOid(int oid) { try { con=DBHelper.getCon(); String sql="delete from tb_order where oid="+oid; ps=con.prepareStatement(sql); if(ps.executeUpdate()>0) { flag = true; } } catch (Exception e) { e.printStackTrace(); }finally { DBHelper.myClose(con, ps, rs); } return flag; } /** * 标识列实现 ②取表的最大序号+1 * @param tabName 表名 * @param colName 列名 * @return */ public int getID(String tabName,String colName) {//表名 列名 int n = 0; try { con=DBHelper.getCon(); String sql="select nvl(max("+colName+"),0)+1 from "+tabName; ps=con.prepareStatement(sql); rs=ps.executeQuery(); if(rs.next()) { n=rs.getInt(1); } } catch (Exception e) { e.printStackTrace(); }finally { DBHelper.myClose(con, ps, rs); } return n; } }