1. 程式人生 > >(二十二)查詢訂單的詳情

(二十二)查詢訂單的詳情

servlet 需求 對象 .com oid exception images map ttr

案例2-查詢訂單的詳情
需求:
    只有未付款的訂單,點擊"付款",跳轉到訂單的詳情頁面
步驟分析:
    1.在訂單列表,點擊付款,
        /store/order?method=getById&oid=?
    2.在orderservlet中編寫getById()
        接受oid
        調用service 返回Order
        將order放入域中,請求轉發 order_info.jsp
    3.orderDao中 通過一個訂單號,查詢訂單詳情
        select * from orderitem oi,product p where oi.pid = p.pid and oi.oid = ?
        用mapListhandler封裝結果集,然後使用BeanUtils封裝成指定的bean對象 ,添加到order的items中即可
        

/store/WebContent/jsp/order_list.jsp

                        <c:if test="${o.state==0 }">
                                            <a href="${pageContext.request.contextPath }/order?method=getById&oid=${o.oid}">付款</a>
                                        </c:if>
                                        <c:if
test="${o.state==1 }"> 已付款 </c:if> <c:if test="${o.state==2 }"> <a href="#">確認收貨</a> </c:if
> <c:if test="${o.state==3 }"> 已完成 </c:if>

com.louis.web.servlet.OrderServlet

    public String getById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.獲取oid
        String oid = request.getParameter("oid");
        
        //2.調用service 通過oid 返回值:order
        OrderService os=(OrderService) BeanFactory.getBean("OrderService");
        Order order=os.getById(oid);
        
        //3.將order放入request域中
        request.setAttribute("bean", order);
        
        return "/jsp/order_info.jsp";
    }

com.louis.service.impl.OrderServiceImpl

    /**
     * 查看訂單詳情
     */
    @Override
    public Order getById(String oid) throws Exception {
        OrderDao od=(OrderDao) BeanFactory.getBean("OrderDao");
        return od.getById(oid);
    }

com.louis.dao.impl.OrderDaoImpl

    /**
     * 通過oid 查詢訂單詳情
     */
    @Override
    public Order getById(String oid) throws Exception {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from orders where oid = ?";
        Order order = qr.query(sql, new BeanHandler<>(Order.class), oid);
        
        //封裝orderitems
        sql="select * from orderitem oi,product p where oi.pid = p.pid and oi.oid = ?";
        List<Map<String, Object>> query = qr.query(sql, new MapListHandler(), oid);
        for (Map<String, Object> map : query) {
            //封裝product
            Product product = new Product();
            BeanUtils.populate(product, map);
            
            //封裝orderitem
            OrderItem oi = new OrderItem();
            BeanUtils.populate(oi, map);
            oi.setProduct(product);
            
            //將orderitem假如中order的items中
            order.getItems().add(oi);
        }
        return order;
    }

技術分享

(二十二)查詢訂單的詳情