kyongseo / cycle

cycle: Springboot를 활용한 중고거래 사이트
1 stars 0 forks source link

DAO와 DTO #1

Open kyongseo opened 6 months ago

kyongseo commented 6 months ago
  1. DAO(Data Access Object)
  1. DTO(Data Transfer Object)

DTO가 필요한 이유

  • Entity에 Setter를 두지 않는 것이 좋음
  • 하지만, Setter가 없으면 Controller에서 값의 할당이 되지 않기 때문에 DTO를 만들고 Setter를 두어 값을 처리
  • Entity에 Setter가 있더라도 이를 request / response에서 사용하지 않는 것이 좋음 -> Entity는 데이터 갱신에 신중해야 하지만 request / response는 수정할 일이 많음
  1. DTO 예시

Entity 클래스

@Getter
@NoArgsConstructor
@Entity
public class Order extends BaseTimeEntity {
    @Id
    @GeneratedValue
    private Long order_id;

    @Column(nullable = false)
    private Long user_id;

    @Column(nullable = false)
    private Long product_id;

    @Column(nullable = false)
    private Long purchase_check;

    @Builder
    public Order(Long user_id, Long product_id, Long purchase_check) {
        this.user_id = user_id;
        this.product_id = product_id;
        this.purchase_check = purchase_check;
    }
}

DTO 클래스

@Getter
@Setter
@NoArgsConstructor
public class OrderDto {

    private Long user_id;
    private Long product_id;
    private Long purchase_check;
}