Java 简单的商店数据库方案

Java 简单的商店数据库方案,java,database,spring,e-commerce,Java,Database,Spring,E Commerce,我在数据库架构方面有一些问题。我自己在做一个简单的网上商店。我不知道如何建立逻辑数据库。 我有用户、购物车、购物车项目、产品和订单表格 我的关系: 用户OneToOne购物车 购物车OneToManyCartItem CartItemManyTone产品 我不知道我应该为表顺序选择哪个关系。 用户可以将任意数量的产品添加到他的购物车中,在确认购买后,我希望将结果存储到订单表中。 请帮我弄清楚。我第一次做网上商店,这对我来说是一个挑战 致以最良好的问候。 购物车 Entity public cla

我在数据库架构方面有一些问题。我自己在做一个简单的网上商店。我不知道如何建立逻辑数据库。 我有用户购物车购物车项目产品订单表格

我的关系

用户OneToOne购物车
购物车OneToManyCartItem
CartItemManyTone产品

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

致以最良好的问候。 购物车

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 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;
}
}

产品

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 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;
}
@实体
公共类产品{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私有整数id;
私有字符串名称;
私有字符串描述;
私人浮动价格;
@manytone(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
私人类别;
@OneToMany(mappedBy=“product”,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
私人名单;
私有布尔值可用;
公共产品(){
}
公共产品(字符串名称、字符串描述、浮动价格、类别、布尔值可用){
this.name=名称;
this.description=描述;
这个价格=价格;
this.category=类别;
this.available=可用;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公开发行价格(){
退货价格;
}
公共定价(浮动价格){
这个价格=价格;
}
公共类别getCategory(){
退货类别;
}
公共无效集合类别(类别){
this.category=类别;
}
公共列表getCartItemList(){
返回项目列表;
}
公共无效setCartItemList(列表cartItemList){
this.cartItemList=cartItemList;
}
公共布尔值isAvailable(){
返回可用;
}
public void setAvailable(布尔值可用){
this.available=可用;
}

}

当用户完成购买时,将其写入订单表,然后将用户的购物车写入订单项目。订单记录中应包含账单地址和发货地址

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

这样,您需要保留用于订单历史目的的所有表格都与用于购物的表格完全分开

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

重要的是,购物车项目应定期检查产品表以确认价格,并确认产品仍在库存中。任何一种情况的改变都会给商店带来巨大的问题,尤其是当顾客订购的商品已经脱销时。如果他们能够说服客户购买不同的商品,那么在与客户联系所需的客户服务时间内,商店可能会失去所有利润。很明显,如果产品价格上涨,商店就会在销售中损失这笔钱

因此,这意味着购物车(购物车项目)的唯一责任是持有产品sku(项目编号)和数量。购物车可以保留价格以供展示,但不负责价格。购物车至少应在给出配送信息后,在最终点击生成交易之前,与products表核对价格和库存水平

很好,你有一个用户的想法,这将使事情变得更容易。因此,需要考虑的一件事是让用户持有不同的运行总数,而不是像您这样的“购物车”。因为会有一些事情影响正在处理的订单的运行总数,比如运输、销售、税收等等,这些都与“购物车”无关


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

产品和商品有什么区别?对我来说,他们似乎一定是identical@niceman,仅仅因为产品存在(即有一个“产品”记录)并不意味着它在购物车中(即有一个“CartItem”记录)。嗯,我明白了,遗漏了@JonathanM@iqiГöСöааа,这实际上是很多很多问题。你能不能把范围缩小到问一个问题,然后再问另一个问题?我会试试。想象一下,我已经选择了购物车上的物品。现在我需要在表中保存订单。我不知道我应该选择哪种关系。多人beetwin用户和订单表?还是在购物车和订单之间?非常感谢您的详细解释。我在搜索这个信息!现在我清楚地理解了采购逻辑。如果我们在现实生活中相遇,我会给你买杯啤酒)