我正在为我的大学项目开发一个电子商务网站(Spring MVC,java,mySql,Hibernate)。我有各种模型,如客户,产品,订单,custOrderHistory等。
我能够显示所有客户详细信息(在管理页面中)以及供客户浏览的所有产品。
但我面临的问题是,如何显示客户的订单和订单历史记录?
我的订单表有orderId,productId,数量和总价。但为了显示产品名称,我必须使用productId并从产品表中恢复产品名称。
我不清楚如何做到这一点。
到目前为止,我正在使用jstl foreach循环来迭代productList.jsp页面中的产品并将其显示在表格中。 但正如我上面所说,我需要一次从3-4个表中检索数据,如productName(来自产品表),productManuacturer(也来自产品表),ShippingAddress(来自客户表)。我不确定如何做到这一点。
编辑1:我从其他人的计算机上问过这个问题,然后没有我的代码。 所以,我在这里给我的代码。
Customer.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
package com.site.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence。*; import java.io.Serializable; import java.util.ArrayList; import java.util.List;
@Entity public class Cart实现了Serializable {
private static final long serialVersionUID = -2479653100535233857L;
@Id @GeneratedValue private int cartId;
@OneToMany(mappedBy =" cart",cascade = CascadeType.ALL,fetch = FetchType.EAGER) private List cartItems = new ArrayList();
@OneToOne @JoinColumn(name =" customerId") @JsonIgnore 私人客户;
private double grandTotal;
public int getCartId(){ 返回购物车; }
public void setCartId(int cartId){ this.cartId = cartId; }
public double getGrandTotal(){ return grandTotal; }
public void 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;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence。*; import java.io.Serializable; import java.util.List;
@Entity public class 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;
import javax.persistence。*; import 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
package com.site.model;
import javax.persistence。*; import java.io.Serializable; import java.util.ArrayList; import 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页面上显示它?
答案 0 :(得分:3)
您应该在订单表中有一个CustomerId列,用于检测订单与哪个客户相关联。只需在其中插入登录的用户ID并通过与客户表进行联接来获取客户名称。
答案 1 :(得分:0)
您的订单表中应该有customerid。这样您就可以通过提供customerid来获取客户的订单历史记录。