我正在为我的大学项目开发一个电子商务网站(springmvc,java,mysql,hibernate)。我有各种型号,如客户,产品,订单,客户订单历史等。
我能够显示所有客户的详细信息(在管理页面)和所有产品的客户浏览。
但我面临的问题是,如何显示客户的订单和订单历史?
我的orders表有orderid、productid、quantity和total price。但是要显示产品名,我必须使用productid并从product表中重新查看产品名。
我不知道怎么做。
到目前为止,我一直在使用jstlforeach循环迭代productlist.jsp页面中的产品,并将它们显示在一个表中。但正如我上面所说的,我需要一次从3-4个表中检索数据,比如productname(来自产品表)、ProductManufacturer(也来自产品表)、shippingaddress(来自客户表)。我不知道该怎么做。
编辑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;
@实体公共类cart实现可序列化{
private static final long serialversionuid=-2479653100533857L;
@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;}
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(列表cartitems){this.cartitems=cartitems;}
public customer getcustomer(){return customer;}
public void setcustomer(customer客户){this.customer=customer;}
}
cartitem.java文件
package com.site.model;
导入com.fasterxml.jackson.annotation.jsonignore;
导入javax.persistence.*;导入java.io.serializable;导入java.util.list;
@实体公共类cartitem实现可序列化{
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;
@实体公共类customerorder实现可序列化{
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实现可序列化{
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:如何在CustomerOrder表和orderhistory表中存储数据,以及如何在jsp页面上显示数据?
2条答案
按热度按时间xkftehaa1#
您的订单表中应该有customerid。这样您就可以通过提供customerid来获取客户的订单历史记录。
avkwfej42#
您应该在order表中有一个customerid列,用于检测与哪个客户关联的订单。只需在其中插入登录的userid,并通过与customer表建立join来获取客户名称。