gpt4 book ai didi

java - 商店的简单数据库方案

转载 作者:搜寻专家 更新时间:2023-10-30 22:35:24 24 4
gpt4 key购买 nike

我在数据库架构方面遇到了一些问题。我正在自己做一个简单的网上商店。我不知道如何制作逻辑数据库。我有UserCartCartItemProductOrder 表。

我的关系:

用户 OneToOne 购物车
购物车 OneToMany CartItem
CartItem ManyToOne 产品

而且我不知道我应该为表顺序选择哪个关系。用户可以将任意数量的产品添加到他的购物车中,确认购买后我想将结果存储到订单表中。请帮我弄清楚。我是第一次做网上商店,这对我来说很有挑战性。

谨致问候。购物车

Entity
public class Cart {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<CartItem> cartItems;
@OneToOne
private User user;
private Double grandTotal;

public Cart(List<CartItem> cartItems) {
this.cartItems = cartItems;
}

public Cart(Double grandTotal) {
this.grandTotal = grandTotal;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public List<CartItem> getCartItemsl() {
return cartItems;
}

public void setCartItemsl(List<CartItem> cartItemsl) {
this.cartItems = cartItemsl;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public Double getGrandTotal() {
return grandTotal;
}

public void setGrandTotal(Double grandTotal) {
this.grandTotal = grandTotal;
}
}

购物车商品

@Entity
public class CartItem {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JsonIgnore
private Cart cart;
@ManyToOne
private Product product;
private Integer quantity;
private Double totalPrice;

public CartItem() {
}

public CartItem(Integer quantity, Double totalPrice) {
this.quantity = quantity;
this.totalPrice = totalPrice;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Cart getCart() {
return cart;
}

public void setCart(Cart cart) {
this.cart = cart;
}

public Product getProduct() {
return product;
}

public void setProduct(Product product) {
this.product = product;
}

public Integer getQuantity() {
return quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

public Double getTotalPrice() {
return totalPrice;
}

public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}

产品

@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String description;
private Float price;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Category category;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<CartItem> cartItemList;
private boolean available;

public Product() {
}

public Product(String name, String description, Float price, Category category, boolean available) {
this.name = name;
this.description = description;
this.price = price;
this.category = category;
this.available = available;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Float getPrice() {
return price;
}

public void setPrice(Float price) {
this.price = price;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public List<CartItem> getCartItemList() {
return cartItemList;
}

public void setCartItemList(List<CartItem> cartItemList) {
this.cartItemList = cartItemList;
}

public boolean isAvailable() {
return available;
}

public void setAvailable(boolean available) {
this.available = available;
}

最佳答案

当用户完成购买时,您将其写入 Order 表,并将用户的购物车写入 Order Items。订单应在订单记录中包含账单地址和送货地址。

您还可以选择将用户账单信息写入客户表和发货表。 (一个客户可以有多个送货地址)然后不在订单记录中包含该信息并链接到这些记录可能很诱人 - 不要这样做。可能会出现许多复杂情况,但简而言之,特定订单的信息必须保留在订单中。

通过这种方式,您需要保留的用于订单历史目的的所有表格都与用于购物的表格完全分开。

所以听起来您的“购物车”实际上是带有运行总计的“订单”的开始。这很酷,但你可能想给它起别的名字。

重要的是,购物车中的商品应该定期检查 Products 表以确认价格,并确认商品是否有货。这些变化中的任何一个都会给商店带来巨大的问题,特别是如果客户订购的商品已经脱销。商店可能会在联系客户所需的客户服务时间内失去所有利润,前提是他们可以说服客户购买不同的商品。显然,如果产品价格上涨,那么商店就会在销售中损失这笔钱。

这意味着购物车(购物车商品)的唯一责任是保存产品 sku(商品编号)和数量。购物车可以保留价格用于展示,但不对价格负责。至少,在提供运输信息后和最终点击生成交易之前,购物车应检查产品表的价格和库存水平。

你有用户的想法很好,它会让事情变得容易得多。所以要考虑的一件事是让用户持有不同的运行总计——而不是你正在做的“购物车”。因为会有影响处理中订单运行总计的因素——如运费、销售、税收等——与“购物车”无关。

另一个想法是例如将处理中的订单运输信息保存给用户。换句话说,他们已经提交了运输信息,但交易还没有完成。这样,如果他们从未完成购买(这种情况经常发生),您就不会浪费任何资源写入发货表。如果您只为已完成的订单写入发货表,那么每条记录都是有效的。计费的想法相同。

关于java - 商店的简单数据库方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38151712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com