在while循环中而不是 null
应该有一个属于此订单的订单行列表。
查询:
SELECT orders.order_id, orders.orderNumber, orderRow.itemName, orderRow.quantity, orderRow.price
FROM orders
LEFT JOIN orderRow
ON orders.order_id=orderRow.order_id
方法:
public List<Order> findAllOrders() {
String sql = "SELECT orders.order_id, orders.orderNumber, orderRow.itemName, orderRow.quantity, orderRow.price\n" +
"FROM orders\n" +
"LEFT JOIN orderRow\n" +
"ON orders.order_id=orderRow.order_id";
try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
return null;
}
List<Order> orders = new ArrayList<>();
while (rs.next()) {
orders.add(new Order(rs.getLong("order_id"),
rs.getString("ordernumber"), null));
}
return orders;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
顺序:
package model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Order {
private Long id;
private String orderNumber;
private List<OrderRow> orderRows;
}
订单行:
package model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class OrderRow {
private String itemName;
private Integer quantity;
private Integer price;
}
暂无答案!
目前还没有任何答案,快来回答吧!