购物车中我的订单查询

1.分析:我的订单的查询主要是从数据库表中的订单表orders中查询出Orderitem中所战士的商品信息

其终极目标是封装Order对象,然后存到域中转发给jsp页面取值

首先要分清楚的问题是:

         * 一个订单集合下(List<Order>)有多个Order对象
         * 一个Order对象下有多个OrderItem订单项

         * 一个OrderItem对应一个Product对象

而要封装Order对象则必须从数据库中查出orderitem和product中的部分信息以封装Order表中的orderiten

购物车中我的订单查询

2.servlet页面代码

//我的订单查询
    public void orderList(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");
        }
        String uid = user.getUid();
        ProductService service = new ProductService();
        //查询该用户所有的订单集合   orders数据库中没有Orderitem这一项  所以要从数据库中查询  然后封装到orders中
        /**
         * 终极目标是封装Order对象
         *
         * 一个订单集合下(List<Order>)有多个Order对象
         * 一个Order对象下有多个OrderItem订单项
         * 一个OrderItem对应一个Product对象
         */
        List<Order> orderList = service.findOrderList(uid);
        //循环所有订单,为每个订单填充订单项集合信息
        if (orderList!=null) {
            for (Order order : orderList) {
                //获得每一个订单的oid---->用于查询每个订单下的所有orderItem项
                String oid = order.getOid();
                //查询该订单下的所有订单项
                List<Map<String, Object>> mapList = service.findAllOrderItem(oid);
                for (Map<String, Object> map : mapList) {
                    try {
                        //封装OrderItem对象
                        OrderItem orderitem = new OrderItem();
                        BeanUtils.populate(orderitem, map);
                        
                        //封装Product对象
                        Product product = new Product();
                        BeanUtils.populate(product, map);
                        
                        //将product对象封装到ordritem中
                        orderitem.setProduct(product);
                        
                        //将orderitem对象封装到Order对象中
                        order.getOrderitem().add(orderitem);
                        
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        //存域
        request.setAttribute("orderList", orderList);
        
        //转发
        request.getRequestDispatcher("/order_list.jsp").forward(request, response);

    }

3.service层页面代码

//查询当前用户下所有的订单集合
        public List<Order> findOrderList(String uid) {
            ProductDao dao = new ProductDao();
            List<Order> orderList = null;
            try {
                orderList = dao.findOrderList(uid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return orderList;
        }

        //查询该订单下的所有订单项
        public List<Map<String, Object>> findAllOrderItem(String oid) {
            ProductDao dao = new ProductDao();
            List<Map<String, Object>> orderitem = null;
            try {
                orderitem = dao.findAllOrderItem(oid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return orderitem;

        }

4.dao层页面代码

//查询当前用户下所有的订单集合
    public List<Order> findOrderList(String uid) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from orders where uid=?";
        List<Order> query = qr.query(sql, new BeanListHandler<Order>(Order.class), uid);
        return query;
    }

    //查询该订单下的所有订单项
    public List<Map<String, Object>> findAllOrderItem(String oid) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select p.pimage,p.pname,p.shop_price,i.count,i.subtotal from product p,orderitem i where p.pid=i.pid and i.oid=?";
        List<Map<String, Object>> query = qr.query(sql, new MapListHandler(), oid);
        return query;

    }

5.jsp页面主要代码

<c:forEach items="${orderList }" var="order">
                    <tbody>
                        <tr class="success">
                            <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.orderitem }" var="items">
                            <tr class="active">
                                <td width="60" width="40%"><input type="hidden" name="id"
                                    value="22"> <img src="${pageContext.request.contextPath }/${items.product.pimage }" width="70"
                                    height="60"></td>
                                <td width="30%"><a target="_blank"> ${items.product.pname }</a></td>
                                <td width="20%">¥${items.product.shop_price }</td>
                                <td width="10%">${items.count }</td>
                                <td width="15%"><span class="subtotal">¥${items.subtotal }</span></td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </c:forEach>