mysql - 显示客户下的所有订单的详细信息
问题描述
我正在为我的大学项目开发一个电子商务网站(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; } }
购物车.java
包 com.site.model;
导入 com.fasterxml.jackson.annotation.JsonIgnore;
导入 javax.persistence.*;导入 java.io.Serializable;导入 java.util.ArrayList;导入 java.util.List;
@Entity 公共类 Cart 实现 Serializable{
私有静态最终长序列版本UID = -2479653100535233857L;
@Id @GeneratedValue private int cartId;
@OneToMany(mappedBy = "cart", cascade= CascadeType.ALL, fetch = FetchType.EAGER) 私有列表 cartItems = new ArrayList();
@OneToOne @JoinColumn(name = "customerId") @JsonIgnore 私人客户客户;
私人双总计;
public int getCartId() { return cartId; }
公共无效 setCartId(int cartId) { this.cartId = cartId; }
公共双getGrandTotal(){返回grandTotal;}
公共无效 setGrandTotal(double grandTotal) { this.grandTotal = grandTotal; }
public List getCartItems() { return cartItems; }
public void setCartItems(List cartItems) { this.cartItems = cartItems; }
public Customer getCustomer() { 返回客户;}
public void setCustomer(Customer customer) { this.customer = customer; }
}
CartItem.java
package com.site.model;
导入 com.fasterxml.jackson.annotation.JsonIgnore;
导入 javax.persistence.*;导入 java.io.Serializable;导入 java.util.List;
@Entity 公共类 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; } }
客户订单.java
package com.site.model;
导入 javax.persistence.*;导入 java.io.Serializable;
@Entity 公共类 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;
@Entity 公共类 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 页面上显示?
解决方案
您应该在订单表中有一个 CustomerId 列,用于检测订单与哪个客户相关联。只需在其中插入登录的用户 ID,并通过与客户表连接来获取客户名称。
推荐阅读
- python - API Gmail:返回错误“字节类型的对象不是 JSON 可序列化的”
- r - 清理 HTML 表,从下一行值添加列,然后删除该行
- css - 一个 div 正确,其他以 flex 为中心
- python - 编写一个python代码来合并两个具有以下条件的列表
- python - 当我尝试使用 urllib 发送请求时,我得到 InvalidURL: URL can't contain control characters
- pentaho - 3 查询,3 张一张 Excel SPOON pentaho
- angular - 使用 ngxPrint 在隐藏的 div 中打印内容时无法读取 null 的属性“getElementsByTagName”
- lua - 如何在不插入索引的情况下使用 table.insert?
- android - 如何在 mpandroidchart 中绘制 Xaxis?(折线图、条形图)
- r - 在 R 中创建传单地理空间图