案例29-购物车提交订单

Posted jepson6669

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了案例29-购物车提交订单相关的知识,希望对你有一定的参考价值。

1 案例分析

1 订单分析1

2 订单分析2

3 订单的业务操作

2 代码实现

1 cart.jsp代码修改

<a href="${pageContext.request.contextPath }/product?method=submitOrder">
     <input type="button" width="100" value="提交订单" name="submit" border="0" style="background: url(\'./images/register.gif\') no-repeat scroll 0 0 rgba(0, 0, 0, 0);
      height:35px;width:100px;color:white;">
</a>

2 ProductServlet代码

// 8 提交订单  submitOrder
public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    HttpSession session = request.getSession();
    
    //首先需要判断用户是否登录
    User user = (User) session.getAttribute("user");
    if(user==null){
        //如果用户没有登录的话,就重定向到登录页面
        response.sendRedirect(request.getContextPath()+"/login.jsp");
        //不在执行继续执行之后的代码
        return;
    }
    
    
    //目的:封装好一个Order对象 传递给service层
    Order order = new Order();
    // 1 private String oid; //该订单的编号
    String oid = CommonsUtils.getUUID();
    order.setOid(oid);
    
    // 2 private Date ordertime ; //下单时间
    order.setOrdertime(new Date());
    
    // 3 private double total; //该订单的总金额
      //首先应该获得session中的购物车Cart对象
    Cart cart = (Cart) session.getAttribute("cart");
    double total = 0.0;
    if(cart!=null){
        total = cart.getTotal();
    }
    order.setTotal(total);
    
    // 4 private int state ; //该订单的支付状态 1代表已付款 0代表未付款
    order.setState(0);
    
    // 5 private String address; //收获地址
    order.setAddress(null);
    // 6 private String name; //姓名
    order.setName(null);
    // 7 private String telephone; //电话
    order.setTelephone(null);
    
    // 8 private User user;//该订单属于哪个用户
    order.setUser(user);
    
    // 9 该订单中有多少订单项
      //从购物车中获取所有的cartItem
    Map<String, CartItem> cartItems = cart.getCartItems();
    //遍历所有的购物车项,加入到订单项orderItem中
    for(Map.Entry<String, CartItem> entry:cartItems.entrySet()){
        //取出里面的每一个购物项
        CartItem cartItem = entry.getValue();
        
        // 创建新的订单项
        OrderItem orderItem = new OrderItem();
        
        // 1)private String itemid; //订单项
        orderItem.setItemid(CommonsUtils.getUUID());
        // 2)private int count; //订单项内商品的购买数量
        orderItem.setCount(cartItem.getBuyNum());
        // 3)private double subtotal; //订单项小计
        orderItem.setSubtotal(cartItem.getSubtotal());
        // 4)private  Product product; //订单项内的商品
        orderItem.setProduct(cartItem.getProduct());
        // 5)private Order order; //该订单项属于哪个订单
        orderItem.setOrder(order);
        
        //将该订单项添加到订单的订单项集合中
        order.getOrderItems().add(orderItem);
    }
    
     //order对象封装完毕
    //传递数据到service层
    ProductService service = new ProductService();
    service.submitOrder(order);
    
    session.setAttribute("order", order);
    
    //页面跳转
    response.sendRedirect(request.getContextPath()+"/order_info.jsp");
    
}

3 ProductService代码

//订单提交 将订单的数据和订单项的数据存储到数据库中
public void submitOrder(Order order) {
    ProductDao dao = new ProductDao();
    
    
    try {
        // 1 、开启事务
        C3P0Utils.startTransaction();
        
        //2、调用dao存储order表数据的方法
        boolean isaddOrdersSuccess = false;
        int rowAddOrders = dao.addOrders(order);
        if(rowAddOrders>0){
            isaddOrdersSuccess = true;
        }
        
        //3、调用dao存储orderItem表数据的方法
        boolean isaddOrderItemSuccess = false;
        int rowAddOrderItem = dao.addOrderItems(order);
        if(rowAddOrderItem>0){
            isaddOrderItemSuccess = true;
        }
        
        //只要有一项操作不成功就回滚。
        if(!isaddOrdersSuccess&&isaddOrderItemSuccess){
            C3P0Utils.rollback();
        }
        
    } catch (SQLException e) {
        try {
            C3P0Utils.rollback();
        } catch (SQLException e1) {
            
            e1.printStackTrace();
        }
        e.printStackTrace();
    }finally {
        //无论是否成功都提交并且释放资源
        try {
            C3P0Utils.commitAndRelease();
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
    }
}

4 ProductDao代码

// 向orders表中插入数据
public int addOrders(Order order) throws SQLException {
    QueryRunner qr = new QueryRunner();
    String sql ="insert into orders values (?,?,?,?,?,?,?,?)";
    Connection conn = C3P0Utils.getConnection();
    int rowAddOrders = qr.update(conn,sql, order.getOid(),order.getOrdertime(),order.getTotal(),order.getState(),
            order.getAddress(),order.getName(),order.getTelephone(),order.getUser().getUid());
    return rowAddOrders;
}


// 向orderitem表插入数据
public int addOrderItems(Order order) throws SQLException {
    QueryRunner qr = new QueryRunner();
    String sql ="insert into orderItem values (?,?,?,?,?)";
    Connection conn = C3P0Utils.getConnection();
    List<OrderItem> orderItems = order.getOrderItems();
    int rowAddOrderItem = 0;
    for (OrderItem item : orderItems) {
        rowAddOrderItem += qr.update(conn, sql,item.getItemid(),item.getCount(),
                item.getSubtotal(),item.getProduct().getPid(),
                item.getOrder().getOid());
    }
    return rowAddOrderItem;
}

5 order_info.jsp修改代码

<table class="table table-bordered">
    <tbody>
        <tr class="warning">
            <th colspan="5">订单编号:${order.oid }</th>
        </tr>
        <tr class="warning">
            <th>图片</th>
            <th>商品</th>
            <th>价格</th>
            <th>数量</th>
            <th>小计</th>
        </tr>
        
        <c:forEach items="${order.orderItems }" var="orderItem">
        
            <tr class="active">
                <td width="60" width="40%">
                    <img src="${pageContext.request.contextPath }/${orderItem.product.pimage}" width="70" height="60">
                </td>
                <td width="30%"><a target="_blank">${orderItem.product.pname}</a></td>
                <td width="20%">¥${orderItem.product.shop_price}</td>
                <td width="10%">${orderItem.count}</td>
                <td width="15%"><span class="subtotal">¥${orderItem.subtotal }</span></td>
            </tr>
        </c:forEach> 
    </tbody>
</table>

3 domain部分

1 orderItem

package www.test.domain;

public class 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*/
    
    private String itemid;//订单项的id
    private int count;//订单项内商品的购买数量
    private double subtotal;//订单项小计
    private Product product;//订单项内部的商品
    private Order order;//该订单项属于哪个订单
    
    
    public String getItemid() {
        return itemid;
    }
    public void setItemid(String itemid) {
        this.itemid = itemid;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int 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;
    }
    
}

2 order

package www.test.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Order {

    /*`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 int state;//订单支付状态 1代表已付款 0代表未付款
    
    private String address;//收货地址
    private String name;//收货人
    private String telephone;//收货人电话
    
    private User user;//该订单属于哪个用户
    
    //该订单中有多少订单项
    List<OrderItem> orderItems = new ArrayList<OrderItem>();
    
    

    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 int getState() {
        return state;
    }

    public void setState(int 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> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }

}

 

以上是关于案例29-购物车提交订单的主要内容,如果未能解决你的问题,请参考以下文章

(二十)生成订单

新星计划Django基于PythonWeb的Django框架设计实现天天生鲜系统-9购物车提交订单订单提交成功页面功能实现

[项目构建 十二]babasport 订单的处理原理及代码实现.

php的购物车中的数组在下订单时怎样传到数据库中

电商订单的操作

跨域解决方案与提交订单