1 Entity

๐Ÿ“Œ Entity ํด๋ž˜์Šค : DB ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•˜๋Š” Column(์—ด)๋“ค์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด

  • Field(ํ•„๋“œ) : class ์ •์˜ ์‹œ, ํด๋ž˜์Šค ์†์„ฑ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธํ•˜๋Š” ๋ณ€์ˆ˜
@Entity
public class Post {
	private String title;
    private String content;
    private String author;
}

๐Ÿ“ Post DB ํ…Œ์ด๋ธ” โ†’ title, content, author ์ปฌ๋Ÿผ ์žˆ์Œ
๐Ÿ“ Post ํ…Œ์ด๋ธ”์— ๋Œ€์‘ํ•˜๋Š” Entity class Post โ†’ Post ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๋“ค๋งŒ ํ•„๋“œ๋กœ ๊ฐ€์ง

1๏ธโƒฃ Entity ๊ทœ์น™

  • DB ํ…Œ์ด๋ธ”๊ณผ 1๋Œ€ 1๋กœ ๋Œ€์‘ โญ๏ธ

  • DB ํ…Œ์ด๋ธ”์— ์—†๋Š” Column์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๋ฉด โ›”๏ธ

  • ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ์ƒ์† or ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด โ›”๏ธ

  • ๋‚ด๋ถ€์˜ ํ•„๋“œ โ†’ @Column, @Id ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ

2๏ธโƒฃ Entity์˜ Getter์™€ Setter

  • ์™ธ๋ถ€์—์„œ ์ตœ๋Œ€ํ•œ Entity์˜ Getter๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ๋กœ์ง๊ตฌํ˜„ ๐Ÿšจ

    • Domain๋งŒ ๋กœ์ง ๊ตฌํ˜„
    • Presentation ๋กœ์ง ๊ตฌํ˜„ โŒ
  • Entity์˜ Setter ๋งŒ๋“œ๋Š”๊ฒƒ ์ตœ๋Œ€ํ•œ ์ง€์–‘ ๐Ÿšจ

    • Setter ๋ฌด๋ถ„๋ณ„ ์‚ฌ์šฉ โ†’ Entity์˜ ์ธ์Šคํ„ด์Šค ๊ฐ’๋“ค์ด ์–ธ์ œ ์–ด๋””์„œ ๋ณ€ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Œ
    • ์ธ์Šคํ„ด์Šค(๊ฐ์ฒด) ์ƒ์„ฑ์‹œ์ ์— ์ƒ์„ฑ์ž๋กœ ํ•„๋“œ์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋˜ํ•œ ๋ณ„๋กœ ...
    • Builder ํŒจํ„ด ์‚ฌ์šฉ ๊ถŒ์žฅ

3๏ธโƒฃ Builder ํŒจํ„ด

@Getter // Lombok ์–ด๋…ธํ…Œ์ด์…˜ (class ๋ชจ๋“ ํ•„๋“œ -> getter method ์ƒ์„ฑ)
@Entity
@NoArgsConstructor // Lombok ์–ด๋…ธํ…Œ์ด์…˜ (class ๊ธฐ๋ณธ์ƒ์„ฑ์ž ์ž๋™์ถ”๊ฐ€)
public class Member {
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id; // PK
    
    private String name;
    private String email;
    
    @Column(length = 13, nullable = false)
    private String phoneNum;
    
    // ์ƒ์„ฑ์ž
    @Builder
    public Member(long id, String name, String email, String phoneNum) {
    	this.id = id;
        this.name = name;
        this.email = email;
        this.phoneNum = phoneNum;
    }
}
// Builder ํŒจํ„ด ์‚ฌ์šฉ๋ฒ•
Member member = new member.builder() // ๊ฐ์ฒด์— ๋นŒ๋“œํ•˜๋Š” ๊ฐœ๋…
	.name("๋ง๋ž‘์ด")
    .email("mallang@mallang.com")
    .phoneNum("010-1234-5678")
    .build();

2 DAO

๐Ÿ“Œ DAO(Data Access Object) : ์‹ค์ œ๋กœ DB์— ์ ‘๊ทผํ•˜๋Š” ๊ฐ์ฒด

  • ํ”„๋กœ์ ํŠธ์˜ Service ๋ชจ๋ธ๊ณผ, ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• 

  • JPA์—์„œ DB์— ๋ฐ์ดํ„ฐ๋ฅผ CRUDํ•˜๋Š” Repository ๊ฐ์ฒด โ†’ DAO


3 DTO

๐Ÿ“Œ DTO(Data Transfer Object) : ๊ณ„์ธต๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด

1๏ธโƒฃ DTO ํŠน์ง•

  • DB์—์„œ ๊บผ๋‚ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” Entity๋ฅผ ๊ฐ€์ง€๊ณ  ๋งŒ๋“œ๋Š” ์ผ์ข…์˜ Wrapper

  • Entity๋ฅผ Controller์™€ ๊ฐ™์€ ํด๋ผ์ด์–ธํŠธ๋‹จ๊ณผ ์ง์ ‘ ๋งˆ์ฃผํ•˜๋Š” ๊ณ„์ธต์— ์ „๋‹ฌ โŒ
    โžก๏ธ DTO๋ฅผ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๊ตํ™˜ โœ…

  • ํŠน๋ณ„ํ•œ ๋กœ์ง์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ์ˆœ์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ๊ฐ์ฒด

  • DB์—์„œ ๊บผ๋‚ธ ๊ฐ’ โ†’ DTO์—์„œ ์ž„์˜๋กœ ์กฐ์ž‘ํ•  ํ•„์š”๊ฐ€ ์—†์Œ

  • Setter โŒ โ†’ ์ƒ์„ฑ์ž์—์„œ ๊ฐ’์„ ํ• ๋‹นํ•จ

  • Entity ํด๋ž˜์Šค์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์„ ํƒ์ ์œผ๋กœ DTO์— ๋‹ด์•„ ์ƒ์„ฑํ•ด ์‚ฌ์šฉ
    โžก๏ธ Entity ํด๋ž˜์Šค ๊ฐ์ถ”๋ฉฐ ๋ณดํ˜ธํ•  ์ˆ˜ ์žˆ์Œ

2๏ธโƒฃ DTO์™€ Entity์˜ ๋ณ€ํ™˜

@Getter
@NoArgsConstructor
public class AccountSignUpRequest { // User ์š”์ฒญ์— ๋Œ€ํ•œ DTO
	@NotBlank
    @Email(message = "์ด๋ฉ”์ผ ์–‘์‹์„ ์ง€์ผœ์ฃผ์„ธ์š”")
    private String email;
    
    @NotBlank
    private String name;
    
    @NotBlank
    private String password;
    
    // Account Entity ์ปฌ๋Ÿผ ๊ฐ’ : email, name, password
    
    @Builder // Entity์˜ Setter ์ƒ์„ฑ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ (์ƒ์„ฑ์ž์—ญํ• )
    public AccountSignUpRequest(String email, String name, String password) {
    	this.email = email;
        this.name = name;
        this.password = password;
    }
    
    public Account toEntity() { // Entity์— DTO ๋ณด๋ƒ„
    	return Account.builder()
        	.email(email)
            .name(name)
            .password(Password.builder().password(this.password).build())
            .build()
    }
}
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED) // ๋ถˆ์™„์ „ํ•œ ๊ฐ์ฒด์ƒ์„ฑ ๋ง‰์Œ
public class AccountSignUpResponse {
	private String email;
    private String name;
    
    @Builder
    public AccountSignUpResponse(Account account) {
    	// ํŒŒ๋ผ๋ฏธํ„ฐ account -> Entity to DTO
        this.email = account.getEmail();
        this.password = account.getPassword();
    }
}

๐Ÿ“ AccountSignUp DTO : Client์˜ Account save ์š”์ฒญ์— ๋Œ€ํ•œ DTO
๐Ÿ“ AccountSignUpResponse : JPA๋ฅผ ํ†ตํ•ด Account Save ์ž‘์—… ํ›„ โ†’ Client๋กœ return๋˜๋Š” DTO

profile
๐ŸฑSunyeon-Jeong, mallang developer๐Ÿฐ

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 19์ผ

์•„์ฃผ ์œ ์ตํ•œ ๋‚ด์šฉ์ด๋„ค์š”!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ