상품 Entity 설계
1. 상품 판매 상태 enum 클래스
- enum 클래스에 대한 개념(링크달기)
- com.shop.constant.ItemSellStatus.java 생성
상품의 현재 판매 상태 (판매, 품절)를 나타내는 상수값 (SELL, SOLD_OUT)
package com.shop.constant;
public enum ItemSellStatus {
SELL, SOLD_OUT
}
2. 상품 클래스 생성 _ Ver 1
- com.shop.entity.Item.java 생성
상품 판매 상태의 값은 ItemSellStatus 열거형의 값을 가짐
@Entity
@ToString
@Table(name = "item")
public class Item {
private Long id; // 상품코드
private String itemNm; // 상품명
private int price; // 가격
private int stockNumber; // 재고수량
private String itemDetail; // 상품 상세 설명
private ItemSellStatus itemSellstatus; // 상품 판매 상태
}
3. Entity 관련 Annotation
어노테이션설명
@Entity | 클래스를 엔티티로 선언 |
@Table | 엔티티와 매핑할 테이블을 지정 |
@Id | 테이블의 기본키에 사용할 속성을 지정 |
@GeneratedValue | 키 값을 생성하는 방법 지정 |
@Column | 필드와 컬럼 매핑 |
@Enumerated | enum 타입 매핑 |
4. 상품 클래스 생성 _ Ver 2
@Table 지정하지 않으면 클래스 이름과 동일한 것을 매핑
@Entity
@Table(name = "item")
@ToString
public class Item extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "item.id")
private Long id; // 상품 코드
@Column(nullable = false, length = 50)
private String itemNm; // 상품명
@Column(name = "price", nullable = false)
private int price; // 상품 가격
@Column(nullable = false)
private int stockNumber; // 재고 수량
@Lob
@Column(nullable = false)
private String itemDetail; // 상품 상세 설명
@Enumerated(EnumType.STRING)
private ItemSellStatus itemSellStatus; // 상품 판매 상태
private LocalDateTime regTime; // 상품 등록 시간
private LocalDateTime updateTime; // 상품 수정 시간
}
후에 Getter Setter 작업을 통해 클래스간 상속시 사용할 수 있게끔 합니다.
5. 상품 테이블 생성
- Web Application 실행 시 @Entity 지정된 클래스를 기반으로 테이블 매핑
- spring.jpa.hibernate.ddl-auto=create 설정으로 인해서 테이블 drop 후 create
- Hibernate 에서 자동으로 bigint 형식으로 sequence++
MySQL-WorkBench 에서 확인
'JAVA > SpringBoot Shoppingmall' 카테고리의 다른 글
[VSCODE] SpringBoot 쇼핑몰(MVN) @Query 어노테이션 (0) | 2022.06.22 |
---|---|
[VSCODE] SpringBoot 쇼핑몰(MVN) 쿼리 메서드 (Query Method) (0) | 2022.06.22 |
[VSCODE] SpringBoot 쇼핑몰(MVN) 상품 Repository 설계 (0) | 2022.06.22 |
[VSCODE] JPA 개념 및 원리 (0) | 2022.06.21 |
[VSCODE] SpringBoot 쇼핑몰(MVN) 개발 환경 설정 (0) | 2022.06.21 |