在開始開發餐廳後台點餐應用之前,我們需要建立一個適當的開發環境。這個環境將包括一個PostgreSQL資料庫,用於存儲應用程式的數據,以及PgAdmin,用於管理和查看資料庫內容。
使用Docker Compose搭建環境:
Docker Compose 是一個用於定義和運行多容器Docker應用程式的工具。我們將使用Docker Compose配置和運行PostgreSQL和PgAdmin容器。
在執行這個步驟之前,你需要確保本地環境包含Docker。
創建一個名為docker-compose.yml
的文件,我們將在其中定義我們的容器配置。
version: '3'
services:
postgres:
image: postgres
container_name: restaurant-postgres
environment:
POSTGRES_DB: restaurant
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5436:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
container_name: restaurant-pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: admin@example.com
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- "5437:80"
depends_on:
- postgres
volumes:
postgres-data:
在項目中的pom.xml
添加以下主要依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- [SQL]Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- postgres SQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<!-- 其他依賴 -->
</dependencies>
application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5436/restaurant
driver-class-name: org.postgresql.Driver
username: postgres
password: postgres
jpa:
database-platform: org.hibernate.dialect.SQLServerDialect
hibernate:
ddl-auto: update
thymeleaf:
cache: false
根據昨天的ERD圖
我們將進行簡化(如下圖所示),這是因為我們只要關注Spring Boot和前面的知識點進行練習,無須將所有實體類都創建:
@Entity(name = "_order")
@Data
public class OrderEntity {
@Id
@GeneratedValue
private Long id;
@Column(name = "date_time")
private LocalDateTime dateTime;
@Column(name = "total_price")
private Integer totalPrice;
@Column(name = "pay_method")
private String payMethod;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<OrderDetailEntity> orderDetails = new ArrayList<>(); // 初始化為空列表
public void addOrderDetail(OrderDetailEntity orderDetail) {
orderDetails.add(orderDetail);
orderDetail.setOrder(this);
}
public void removeOrderDetail(OrderDetailEntity orderDetail) {
orderDetails.remove(orderDetail);
orderDetail.setOrder(null);
}
}
@Entity(name = "order_detail")
@Data
public class OrderDetailEntity {
@EmbeddedId
private OrderDetailId id = new OrderDetailId();
@ManyToOne
@MapsId("orderId")
@JoinColumn(name = "order_id")
private OrderEntity order;
@ManyToOne
@JoinColumn(name = "dish_id")
private DishEntity dish;
private Integer count;
private Integer total;
@Embeddable
@Data
public static class OrderDetailId implements Serializable {
@Column(name = "_order_id")
private Long orderId;
@Column(name = "dno")
private Integer dno;
}
@Override
public String toString() {
return "OrderDetailEntity{";
}
}
@Entity(name = "dish")
@Data
public class DishEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String description;
private Integer price;
@ManyToOne
@JoinColumn(name = "type_id")
private DishTypeEntity dishType;
}
@Entity(name = "dish_type")
@Data
public class DishTypeEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "dishType")
private List<DishEntity> dishes;
}