
๊ณ์ธต(Layer)๊ฐ ๋ฐ์ดํฐ ๊ตํ์ ์ํด ์ฌ์ฉํ๋ ๊ฐ์ฒด

๋ก์ง์ ๊ฐ๊ณ ์์ง ์๋ ์์ํ ๋ฐ์ดํฐ ๊ฐ์ฒด, getter/setter ๋ฉ์๋๋ง์ ๊ฐ๊ณ ์์.
๋ก์ง ์์ด, ๋คํธ์ํฌ ๋๋ ๊ณ์ธต๊ฐ ๋ฐ์ดํฐ ์ ์ก์ด ๋ชฉ์ ์.
public class StudentInfoDto {
private final String name;
private final int age;
private final String address;
public StudentInfoDto(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
}
Value Object(๊ฐ ๊ฐ์ฒด)
๊ฐ ๊ทธ ์์ฒด๋ฅผ ํํํ๋ ๊ฐ์ฒด
์๋ก ๋ค๋ฅธ ์ด๋ฆ์ ๊ฐ์ง VO์ ์ธ์คํด์ค๊ฐ ๋ชจ๋ ์์ฑ ๊ฐ์ด ๊ฐ๋ค๋ฉด ๊ฐ์ ๊ฐ์ฒด
equals(), hashcode() ํจ์ ์ค๋ฒ๋ผ์ด๋ฉ๊ฐ์ฒด์ ๋ถ๋ณ์ฑ์ ๋ณด์ฅ
๋ก์ง์ ํฌํจํ ์ ์์.
public final class UserVO {
private final String name;
private final int age;
public UserVO(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserVO userVO = (UserVO) o;
return age == userVO.age && Objects.equals(name, userVO.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
JPA์ ORM์ด๋ JDBC ๋ฑ์ ํ์ฉํ์ฌ ๊ตฌํํจ.
JPA ๊ธฐ๋ฐ DAO ์์ .
@Repository
public interface UserDAO extends JpaRepository<UserEntity, Long> {
Optional<UserEntity> findByName(String name);
}
---------------------------------------------------------------------------
public interface JpaUserRepository extends JpaRepository<UserEntity, Long> {
}
DTO๊ฐ ์ ์กํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ๊ฐ ์ ์ก ๊ณผ์ ์ค ๋ณ์กฐ๋์ง ์์์ ๋ณด์ฅํ ์ ์์.
๋ฐ์ดํฐ ์ ๋ฌ ์ค ๋ฐ์ดํฐ ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ ์ ์์ด์ ์์ ์ฑ์ด ์ข์์ง.
ํ ์ ์๋๋ฐ.. ๋ถ๋ฆฌํ๋๊ฒ ์ ๋ฆฌํ๋ค๊ณ ํ๋จ๋จ.
View์์ ํํํ๋ ์์ฑ ๊ฐ๋ค์ด ์์ฒญ์ ๋ฐ๋ผ ๊ณ์ ๋ฌ๋ผ์ง ์ ์๋๋ฐ,
๊ทธ ๋๋ง๋ค Entity์ ์์ฑ๊ฐ์ ๋ณ๊ฒฝํ๋ฉด ์์์ฑ ๋ชจ๋ธ์ ํํํ Entity์ ์์์ฑ์ด ๋ชจํธํด์ง.
๋ฐ๋ผ์ Controller์์ ์ธ DTO์ Entityํด๋์ค๋ ๋ถ๋ฆฌํ๋๊ฒ ์ข์.