显示客户下的所有订单的详细信息

Posted

技术标签:

【中文标题】显示客户下的所有订单的详细信息【英文标题】:show details of all orders placed by customer 【发布时间】:2018-10-19 12:19:42 【问题描述】:

我正在为我的大学项目开发​​一个电子商务网站(Spring MVC、java、mysql、Hibernate)。我有各种模型,如客户、产品、订单、custOrderHistory 等。

我能够显示所有客户详细信息(在管理页面中)和所有产品供客户浏览。

但我面临的问题是,如何显示客户的订单和订单历史记录?

我的订单表有 orderId、productId、数量和总价。但要显示产品名称,我必须使用 productId 并从产品表中检索产品名称。

我不知道该怎么做。

到目前为止,我一直在使用 jstl foreach 循环来迭代 productList.jsp 页面中的产品并将它们显示在表格中。 但正如我上面所说,我需要一次从 3-4 个表中检索数据,例如 productName(来自 product 表)、productManuacturer(也来自 product 表)、 ShippingAddress(来自 customer 表)。我不确定该怎么做。

编辑 1:我从别人的电脑上问了这个问题,当时我的代码没有随身携带。 所以,我在这里给出我的代码。

    客户.java

    package com.site.model;
    
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import org.hibernate.validator.constraints.NotEmpty;
    import javax.persistence.*;
    import java.io.Serializable;
    
    @Entity
    public class Customer implements Serializable 
    
    private static final long serialVersionUID = -3280023076408333682L;
    
    @Id
    @GeneratedValue
    private int customerId;
    
    @NotEmpty(message = "  The customer name must not be blank.")
    private String customerName;
    
    @NotEmpty (message = "  The customer email must not be blank.")
    private String customerEmail;
    private String customerPhone;
    
    @NotEmpty (message = "  The username must not be blank.")
    private String username;
    
    @NotEmpty (message = "  The password must not be blank.")
    private String password;
    private boolean enabled;
    
    @OneToOne
    @JoinColumn(name = "billingAddressId")
    private BillingAddress billingAddress;
    
    @OneToOne
    @JoinColumn(name = "shippingAddressId")
    private ShippingAddress shippingAddress;
    
    @OneToOne()
    @JoinColumn(name = "cartId")
    @JsonIgnore
    private Cart cart;
    
    public int getCustomerId() 
        return customerId;
    
    
    public void setCustomerId(int customerId) 
        this.customerId = customerId;
    
    
    public String getCustomerName() 
        return customerName;
    
    
    public void setCustomerName(String customerName) 
        this.customerName = customerName;
    
    
    public BillingAddress getBillingAddress() 
        return billingAddress;
    
    
    public void setBillingAddress(BillingAddress billingAddress) 
        this.billingAddress = billingAddress;
    
    
    public ShippingAddress getShippingAddress() 
        return shippingAddress;
    
    
    public void setShippingAddress(ShippingAddress shippingAddress) 
        this.shippingAddress = shippingAddress;
    
    
    public String getCustomerPhone() 
        return customerPhone;
    
    
    public void setCustomerPhone(String customerPhone) 
        this.customerPhone = customerPhone;
    
    
    public String getCustomerEmail() 
        return customerEmail;
    
    
    public void setCustomerEmail(String customerEmail) 
        this.customerEmail = customerEmail;
    
    
    public String getUsername() 
        return username;
    
    
    public void setUsername(String username) 
        this.username = username;
    
    
    public String getPassword() 
        return password;
    
    
    public void setPassword(String password) 
        this.password = password;
    
    
    public boolean isEnabled() 
        return enabled;
    
    
    public void setEnabled(boolean enabled) 
        this.enabled = enabled;
    
    
    public Cart getCart() 
        return cart;
    
    
    public void setCart(Cart cart) 
        this.cart = cart;
    
    
    
    

    Cart.java

    包 com.site.model;

    导入 com.fasterxml.jackson.annotation.JsonIgnore;

    导入 javax.persistence.*; 导入 java.io.Serializable; 导入 java.util.ArrayList; 导入 java.util.List;

    @实体 公共类 Cart 实现 Serializable

    private static final long serialVersionUID = -2479653100535233857L;

    @Id @GeneratedValue 私人 int cartId;

    @OneToMany(mappedBy = "cart", cascade= CascadeType.ALL, fetch = FetchType.EAGER) 私有列表 cartItems = new ArrayList();

    @OneToOne @JoinColumn(name = "customerId") @JsonIgnore 私人客户客户;

    私人双总计;

    公共 int getCartId() 返回购物车ID;

    public void setCartId(int cartId) this.cartId = cartId;

    公共双 getGrandTotal() 返回总计;

    public void setGrandTotal(double grandTotal) this.grandTotal = 总计;

    公共列表 getCartItems() 返回购物车项目;

    public void setCartItems(列出 cartItems) this.cartItems = cartItems;

    公共客户 getCustomer() 回头客;

    public void setCustomer(Customer customer) this.customer = 客户;

    CartItem.java

    package com.site.model;
    

    导入 com.fasterxml.jackson.annotation.JsonIgnore;

    导入 javax.persistence.*; 导入 java.io.Serializable; 导入 java.util.List;

    @实体 公共类 CartItem 实现 Serializable

    private static final long serialVersionUID = -904360230041854157L;
    
    @Id
    @GeneratedValue
    private int cartItemId;
    
    @ManyToOne
    @JoinColumn(name="cartId")
    @JsonIgnore
    private Cart cart;
    
    @ManyToOne
    @JoinColumn(name = "productId")
    private Product product;
    private int quantity;
    private double totalPrice;
    
    @ManyToMany(cascade=CascadeType.ALL, mappedBy="cartItems")
    @JsonIgnore
    private List<OrderHistory> orderHistoryList;
    
    public int getCartItemId() 
        return cartItemId;
    
    
    public void setCartItemId(int cartItemId) 
        this.cartItemId = cartItemId;
    
    
    public Product getProduct() 
        return product;
    
    
    public void setProduct(Product product) 
        this.product = product;
    
    
    public int getQuantity() 
        return quantity;
    
    
    public void setQuantity(int quantity) 
        this.quantity = quantity;
    
    
    public double getTotalPrice() 
        return totalPrice;
    
    
    public void setTotalPrice(double totalPrice) 
        this.totalPrice = totalPrice;
    
    
    public Cart getCart() 
        return cart;
    
    
    public void setCart(Cart cart) 
        this.cart = cart;
    
    
    public List<OrderHistory> getOrderHistoryList() 
        return orderHistoryList;
    
    
    public void setOrderHistoryList(List<OrderHistory> orderHistoryList) 
        this.orderHistoryList = orderHistoryList;
    
    
    

    CustomerOrder.java

    package com.site.model;
    

    导入 javax.persistence.*; 导入 java.io.Serializable;

    @实体 公共类 CustomerOrder 实现 Serializable

    private static final long serialVersionUID = -3608286390950243118L;
    
    @Id
    @GeneratedValue
    private int customerOrderId;
    
    @OneToOne
    @JoinColumn(name = "cartId")
    private Cart cart;
    
    @OneToOne
    @JoinColumn(name = "customerId")
    private Customer customer;
    
    @OneToOne
    @JoinColumn(name = "billingAddressId")
    private BillingAddress billingAddress;
    
    @OneToOne
    @JoinColumn(name = "shippingAddressId")
    private ShippingAddress shippingAddress;
    
    
    public int getCustomerOrderId() 
        return customerOrderId;
    
    
    public void setCustomerOrderId(int customerOrderId) 
        this.customerOrderId = customerOrderId;
    
    
    public Cart getCart() 
        return cart;
    
    
    public void setCart(Cart cart) 
        this.cart = cart;
    
    
    public Customer getCustomer() 
        return customer;
    
    
    public void setCustomer(Customer customer) 
        this.customer = customer;
    
    
    public BillingAddress getBillingAddress() 
        return billingAddress;
    
    
    public void setBillingAddress(BillingAddress billingAddress) 
        this.billingAddress = billingAddress;
    
    
    public ShippingAddress getShippingAddress() 
        return shippingAddress;
    
    
    public void setShippingAddress(ShippingAddress shippingAddress) 
        this.shippingAddress = shippingAddress;
    
    
    
    

    OrderHistory.java

    包 com.site.model;

    导入 javax.persistence.*; 导入 java.io.Serializable; 导入 java.util.ArrayList; 导入 java.util.List;

    @实体 公共类 OrderHistory 实现 Serializable

    private static final long serialVersionUID = 1083533250613139445L;
    
    @Id
    @GeneratedValue
    private int orderHistoryId;
    private int customerId;
    private String customerName;
    private int customerOrderId;
    private int cartId;
    
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "cartItem_orderHistory", joinColumns = @JoinColumn(name = "orderHistoryId"),
            inverseJoinColumns = @JoinColumn
                    (name = "cartItemId"))
    private List<CartItem> cartItems = new ArrayList<CartItem>();
    private double grandTotal;
    private String billingAddress;
    private String shippingAddress;
    
    public int getOrderHistoryId() 
        return orderHistoryId;
    
    
    public void setOrderHistoryId(int orderHistoryId) 
        this.orderHistoryId = orderHistoryId;
    
    
    public int getCustomerId() 
        return customerId;
    
    
    public void setCustomerId(int customerId) 
        this.customerId = customerId;
    
    
    public String getCustomerName() 
        return customerName;
    
    
    public void setCustomerName(String customerName) 
        this.customerName = customerName;
    
    
    public int getCustomerOrderId() 
        return customerOrderId;
    
    
    public void setCustomerOrderId(int customerOrderId) 
        this.customerOrderId = customerOrderId;
    
    
    public int getCartId() 
        return cartId;
    
    
    public void setCartId(int cartId) 
        this.cartId = cartId;
    
    
    public List<CartItem> getCartItems() 
        return cartItems;
    
    
    public void setCartItems(List<CartItem> cartItems) 
        this.cartItems = cartItems;
    
    
    public double getGrandTotal() 
        return grandTotal;
    
    
    public void setGrandTotal(double grandTotal) 
        this.grandTotal = grandTotal;
    
    
    public String getBillingAddress() 
        return billingAddress;
    
    
    public void setBillingAddress(String billingAddress) 
        this.billingAddress = billingAddress;
    
    
    public String getShippingAddress() 
        return shippingAddress;
    
    
    public void setShippingAddress(String shippingAddress) 
        this.shippingAddress = shippingAddress;
    
    
    

编辑2:如何将数据存储在ustomerorder表和orderhistory表中,以及如何在jsp页面上显示?

【问题讨论】:

【参考方案1】:

您应该在订单表中有一个 CustomerId 列,用于检测订单与哪个客户相关联。只需在其中插入登录的用户 ID,然后通过与客户表连接来获取客户名称。

【讨论】:

【参考方案2】:

您的订单表中应该有 customerid。这样您就可以通过提供 customerid 来获取客户的订单历史记录。

【讨论】:

以上是关于显示客户下的所有订单的详细信息的主要内容,如果未能解决你的问题,请参考以下文章

更改 Woocommerce 结帐端点以显示订单摘要详细信息

如何从 WooCommerce 中的订单中获取客户详细信息?

在 WooCommerce 管理员订单详细信息页面上的订单项目表中显示产品元数据

订单列表查询SQL,查询出订单的数量和订单详细信息(包括订单中的所有商品)。

在 Woocommerce 的管理订单详细信息部分显示自定义结帐字段值

如何在 wordpress/woocommerce 中获取所有订单详细信息? [复制]