엔티티이다.import jakarta.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name="product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long number;
@Column(nullable=false)
private String name;
@Column(nullable=false)
private Integer price;
@Column(nullable=false)
private Integer stock;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
public Long getNumber() {
return number;
}
public void setNumber(Long number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public LocalDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}
import com.springboot.jpa.data.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product,Long> {
}
FindBy : SQL문의 where절 역할을 수행하는 구문. Ex) findByName(String name)AND, OR : 조건을 여러 개 설정하기 위해 사용. Ex) findByNameAndEmail(String name, String email)Like/NotLike: SQL문의 like와 동일한 기능을 수행.StartsWith/StartingWith: 특정 키워드로 시작하는 문자열 조건을 설정.EndsWith/EndingWith: 특정 키워드로 끝나는 문자열 조건을 설정.IsNull/IsNotNull: 레코드 값이 Null이거나 Null이 아닌 값을 검색.True/False: Boolean 타입의 레코드를 검색할 때 사용.Before/After: 시간을 기준으로 값을 검색.LessThan/GreaterThan: 특정 값을 기준으로 대소 비교를 할 때 사용.Between: 두 값 사이의 데이터를 조회.OrderBy: SQL문에서 order by와 동일한 기능 수행.countBy: SQL문에서 count와 동일한 기능을 수행.레포지토리 가 대체함.getById() : 내부적으로 EntityManager의 getReference() 메서드를 호출하여 프록시 객체를 반환받는다.findById() : 내부적으로 EntityManager의 find() 메서드 호출. @Transactional 어노테이션이 선언되어 있는데, 이는 메서드 내 작업을 마치면, 자동으로 flush() 메서드를 실행해준다.빌드 메서드
빌더 패턴을 따르는 메서드이다. 데이터 클래스를 사용할 때 생성자로 초기화할 경우 모든 필드에 값을 넣거나 null을 명시적으로 사용해야 함. 이러한 단점을 보완하기 위해 나온 패턴이 빌더 패턴이며, 이 패턴을 아용하면 데이터만 설정이 가능하여 유연성 확보가 가능함.
...
ProductDto(Long num, String name, int price, int stock){
this.num = num;
this.name = name;
this.price = price;
this.stock = stock;
}
-> 초기화 시, 모든 매개변수에 값을 넣어 초기화 해줘야 함.
...
ProductDto.buider()
.num(값)
.name("")
.price(값)
.stock(값)
.build();
이 방식은 초기화가 가능하다.
@Getter @Stter: 클래스에 선언되어 있는 필드에 대한 getter/setter 메서드를 생성함.
생성자 자동 어노테이션
-
@ToString : toString() 메서드 생성하는 어노테이션.
@EqualsAndHashCode : 객체의 동등성(equals)과 동일성(hashCode)을 비교하는 연산 메서드를 생성.
동등성과 동일성
동등성은 비교 대상의 두 객체가 가진 값이 같음을 의미하고, 동일성은 두 객체가 같은 객체임을 의미한다.
두 메서드는 일반적으로 클래스 단위의 객체를 비교하는데 사용하고 Object 클래스의 메서드를 오버라이딩하여 구현함.
@Data : 위에 설명한 모든 어노테이션을 포괄하는 어노테이션으로, 위에서 언급한 설정을 한번에 생성 가능하다.