12๋ฒ์งธ ๊ณผ์
Entity
- ์ค์ DB์ ํ ์ด๋ธ๊ณผ ๋งคํ๋๋ ๊ฐ์ฒด
- id๋ฅผ ํตํด ๊ฐ๊ฐ์ Entity๋ฅผ ๊ตฌ๋ถํ๋ค
- ๋ก์ง์ ๊ฐ์ง ์ ์๋ค
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String email;
public User() {
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
// Getters for all fields
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getEmail() {
return email;
}
// Other getter methods for additional fields
}
DTO (Data Transfer Object)
- ๊ณ์ธต๊ฐ ๋ฐ์ดํฐ ๊ตํ์ ์ํด ์ฌ์ฉํ๋ ๊ฐ์ฒด
- ์ฃผ๋ก View์ Controller ์ฌ์ด์์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ๋ ํ์ฉํ๋ค
- getter/ setter ๋ฉ์๋๋ง ์ ์
public class UserDTO {
private Long id;
private String username;
private String email;
// Constructors, getters, and setters
public UserDTO() {
}
public UserDTO(Long id, String username, String email) {
this.id = id;
this.username = username;
this.email = email;
}
// Getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
VO (Value Object)
- ๊ฐ ๊ทธ ์์ฒด๋ฅผ ํํํ๋ ๊ฐ์ฒด
- ๋ก์ง์ ๊ฐ์ง ์ ์๊ณ , ๊ฐ์ฒด์ ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ๋ค
- ๊ฐ์ฒด๋ค์ ์ฃผ์๊ฐ ๋ฌ๋ผ๋ ๊ฐ์ด ๊ฐ์ผ๋ฉด ๋์ผํ ๊ฒ์ผ๋ก ์ฌ๊ธด๋ค
- ๊ฐ ๋น๊ต๋ฅผ ์ํด equals()์ hashCode() ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํด์ผํ๋ค
public class UserVO {
private String username;
private String email;
public UserVO(String username, String email) {
this.username = username;
this.email = email;
}
public String getUsername() {
return username;
}
public String getEmail() {
return email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserVO userVO = (UserVO) o;
if (username != null ? !username.equals(userVO.username) : userVO.username != null) return false;
return email != null ? email.equals(userVO.email) : userVO.email == null;
}
@Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
result = 31 * result + (email != null ? email.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "UserVO{" +
"username='" + username + '\'' +
", email='" + email + '\'' +
'}';
}
}
728x90