0

我正在尝试生成每个客户订单的网页报告,其中包含每个订单的行项目详细信息。所以它看起来像这样:

Customer 1  | Order 1  |     Item 1    |
            |          | == New Row == |
            |          |     Item 2    |                     
            |          | == New Row == |
            |          |     Item 3    |                     
            |          | == New Row == |
            | ====== New Row ========= |
            | Order 2  |     Item 1    |                     
            |          | == New Row == |
            |          |     Item 2    |                     
            |          | == New Row == |
            |          |     Item 3    |                     
            |          | == New Row == |
            | ====== New Row ========= |
            | Order 3  |     Item 1    |                     
            |          |     Item 2    |
== New Row ==============================                     
Customer 2  | Order 1  |     Item 1    |                     
            |          | == New Row == |
            |          |     Item 2    |                     
            |          | == New Row == |
            |          |     Item 3    |                     
            |          | == New Row == |
            |          |     Item 4    |                     
            |          | == New Row == |
            |          |     Item 5    |                     
            |          | == New Row == |
            | ====== New Row ========= |
            | Order 2  |     Item 1    |                     
            |          | == New Row == |
            |          |     Item 2    |                     
            |          | == New Row == |
            |          |     Item 3    |                     
== New Row ==============================                     

获取数据的数组列表是否更好,如下所示:

1  | 1 |  1 |                     
1  | 1 |  2 |                     
1  | 1 |  3 |                     
1  | 2 |  1 |                     
1  | 2 |  2 |                     
1  | 2 |  3 |                     
1  | 3 |  1 |                     
1  | 3 |  2 |                     
2  | 1 |  1 |                     
2  | 1 |  2 |                     
2  | 1 |  3 |                     
2  | 1 |  4 |                     
2  | 1 |  5 |                     
2  | 2 |  1 |                     
2  | 2 |  2 |                     
2  | 2 |  3 |                     

并传递 1 个 JavaBean 并使用 JSTL 确定嵌套如下:

  this is incomplete for brevity
  <table><tr><th>Customer No</th><th>Orders</th></tr>
  <c:forEach var="custOrderLineItem" items="${customerOrderLineItemList}">
  <c:set var="currentOrder" value="custOrderLineItem.orderId">
  <c:set var="currentCustomer" value="custOrderLineItem.customerId">

  <c:if test="${currentOrder != custOrderLineItem.orderId}">
      ==New Row==
  </c:if>

还是使用嵌套的 JavaBeans 更好

Customer.setOrders<List>
Orders.setOrderDetails<List>
OrderDetails.setLineItem<List>

然后像这样使用JSTL

      <c:forEach var="customer" items="${customerList}">
         <c:forEach var="order" items="${customer.orderList}">
             <c:forEach var="lineItem" items="${order.detailList}">

在花时间写完这篇文章后,我觉得第二种方法看起来更干净、更容易。但是第一种方法似乎有一个更简单的 SQL 查询。我没有使用 JPA,只是基本的 JDBC sql 调用。那么如何在没有 JPA 的情况下填充嵌套的 JavaBeans 呢?你会做这样的事情吗?

  List<Customer> custList = getCustomerList();
  ListIterator custListIter = custList.listIterator();
     while (custListIter.hasNext()) {
         customer = (Customer) custListIter.next();
         List<Order> orderList = getOrderList(customer.getId());
         ListIterator orderListIter = orderList.listIterator();
        while (orderListIter.hasNext()) {
                 order = (Order) orderListIter.next();
                 List orderDetailsList<OrderDetail> = getOrderDetailList(order.getId);
                 order.setOrderDetails(orderDetailsList);
                 orderListIter.set(order);
             }
         customer.setOrderList(orderList);
         custListIter.set(customer);
      }
4

1 回答 1

1

您可以简单地创建返回所有内容的 SQL 查询,并从结果集中构建对象图。使用映射来保持 ID 和相应对象之间的关联:

Map<Long, Customer> customersById = new HashMap<Long, Customer>();
Map<Long, Order> ordersById = new HashMap<Long, Order>();
Map<Long, Item> itemsById = new HashMap<Long, Item>();
while (rs.next()) {
    Long customerId = rs.getLong(1);
    Customer customer = customersById.get(customerId);
    if (customer == null) {
        customer = new Customer(customerId);
        // populate other fields of customer
        customersById.put(customerId, customer);
    }

    Long orderId = rs.getLong(5);
    Order order = ordersById.get(orderId);
    if (order == null) {
        order = new Order(orderId);
        customer.addOrder(order);
        // populate other fields of order
        ordersById.put(orderId, order);
    }

    // same for items

}

在循环结束时,您拥有所有客户,每个客户都有他们的订单,每个客户都有他们的物品。

于 2012-10-18T20:53:11.610 回答