2 ๋ถ„ ์†Œ์š”

DAO, DTO, VO๊ฐ€ ๋ญ˜๊นŒ?

DAO, DTO, VO๋Š” ๋ชจ๋‘ ๋ฐ์ดํ„ฐ์™€ ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ผ๋Š” ๊ณตํ†ต์ ์ด ์žˆ๋‹ค.

Spring์œผ๋กœ ๊ฐœ๋ฐœ์„ ํ•  ๋•Œ ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ง์ ‘ ์—ฐ๋™ํ•˜๋Š”์ง€๊ฐ€ ์–ด๋ ค์›Œ ๋งŽ์ด ํ•ด๋งธ๋‹ค.
์—ฌ๊ธฐ์— ๊ทธ๋Ÿฐ ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด๋“ค์ธ DAO, DTO, VO์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๊ฒ ๋‹ค.

DAO (Data Access Object)

DAO๋ž€, ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ๋‹ด๊ธด ํด๋ž˜์Šค์ด๋‹ค.
DB์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๋กœ์ง๊ณผ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
๊ทธ๋ž˜์„œ DB Connection๋กœ์ง๊นŒ์ง€ ์„ค์ •๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ณ , DB๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ CRUDํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ „๋‹ดํ•œ๋‹ค.

Spring์—์„œ @Repository์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๊ฐ€ DAO๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

CRUD๋ž€?

CRUD๋ž€, ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ, ์ฝ๊ธฐ, ์ˆ˜์ •, ์‚ญ์ œ ํ•˜๋Š” ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ๋งํ•œ๋‹ค.

CRUD ์„ค๋ช…
Create ๋ฐ์ดํ„ฐ๋ฅผ โ€œ์ƒ์„ฑโ€
Read ๋ฐ์ดํ„ฐ๋ฅผ โ€œ์ฝ๊ธฐโ€
Update ๋ฐ์ดํ„ฐ๋ฅผ โ€œ์ˆ˜์ •โ€
Delete ๋ฐ์ดํ„ฐ๋ฅผ โ€œ์‚ญ์ œโ€

์˜ˆ์ œ์ฝ”๋“œ

[MemberDAO]

package org.practice.di.persistence;

import org.practice.di.domain.StudentVO;

public interface MemberDAO {
	public StudentVO read(String id) throws Exception;
	public void add(StudentVO student) throws Exception;
}  


[MemberDAOImpl]

package org.practice.di.persistence;

import java.util.HashMap;
import java.util.Map;

import org.practice.di.domain.StudentVO;

public class MemberDAOImpl implements MemberDAO {
	
	private Map<String, StudentVO> storage = new HashMap<String, StudentVO>();
	
	public StudentVO read(String id) throws Exception {
		return storage.get(id);  
	}
	public void add(StudentVO student) throws Exception { 
		storage.put(student.getId(), student);
	}
}

DTO (Data Transfer Object)

DTO๋ž€, ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
Controller, Service, View ๋“ฑ์˜ ๊ณ„์ธต ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๋กœ์ง์„ ๊ฐ–๊ณ ์žˆ์ง€ ์•Š์€ ์ˆœ์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด -> getter/setter & toString() ๋ฉ”์†Œ๋“œ๋งŒ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋ณ€์ ์ธ ์†์„ฑ์„ ๋ˆ๋‹ค.

์œ ์ €๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ๋„ฃ๋Š” ๊ณผ์ •์„ ์ƒ๊ฐํ•ด๋ณด์ž.

  • ์œ ์ €๊ฐ€ ์ž์‹ ์˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ
  • form์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ DTO์— ๋‹ด์•„ ์ „์†ก
  • ํ•ด๋‹น DTO๋ฅผ ๋ฐ›์€ ์„œ๋ฒ„๊ฐ€ DAO๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…

์˜ˆ์ œ์ฝ”๋“œ

[Service]

package org.practice.di.service;

import org.practice.di.domain.StudentVO;

public interface MemberService {
		public StudentVO readMember(String id) throws Exception;
		public void addMember(StudentVO student) throws Exception;
}


[MemberServiceImpl]

package org.practice.di.service;

import org.practice.di.domain.StudentVO;
import org.practice.di.persistence.MemberDAO;

public class MemberServiceImpl implements MemberService {
  
  private MemberDAO memberDAO;
    
  public MemberServiceImpl(MemberDAO memberDAO) {
    this.memberDAO = memberDAO;
  }

  /*
  public void setMemberDAO(MemberDAO memberDAO) {
    this.memberDAO = memberDAO;
  }
  */
  
  public StudentVO readMember(String id) throws Exception {
    return memberDAO.read(id);
  }
  
  public void addMember(StudentVO student) throws Exception {
    memberDAO.add(student);
  }

}

VO (Value Object)

VO๋ž€, ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋กœ, DTO์™€ ๋น„์Šทํ•˜๋‹ค.
๊ทธ๋ž˜์„œ ์„œ๋กœ ํ˜ผ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, VO๋Š” ๊ฐ’์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋กœ, ๋ถˆ๋ณ€์ ์ธ ์†์„ฑ์„ ๊ฐ–๊ณ ์žˆ๋‹ค.

์˜ˆ์ œ์ฝ”๋“œ

[StudentVO]

package org.practice.di.domain;

public class StudentVO {
	private String id;
	private String passwd;
	private String username;
	private String snum;
	private String depart;
	private String mobile;
	private String email;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSnum() {
		return snum;
	}
	public void setSnum(String snum) {
		this.snum = snum;
	}
	public String getDepart() {
		return depart;
	}
	public void setDepart(String depart) {
		this.depart = depart;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "StudentVO [id=" + id + ", passwd=" + passwd + ", username=" + username + ", snum=" + snum + ", depart="
				+ depart + ", mobile=" + mobile + ", email=" + email + "]";
	}

}

DTO์™€ VO์˜ ์ฐจ์ด

์ข…๋ฅ˜ ์šฉ๋„ ๋™๋“ฑ ๊ฒฐ์ • ๊ฐ€๋ณ€ / ๋ถˆ๋ณ€ ๋กœ์ง
DTO - ๊ณ„์ธต ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ - ์†์„ฑ๊ฐ’์ด ๋ชจ๋‘ ๊ฐ™์•„๋„ ๊ฐ™์€ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ์Œ - setter ์กด์žฌ ์‹œ ๊ฐ€๋ณ€,- setter ๋น„ ์กด์žฌ ์‹œ ๋ถˆ๋ณ€ - getter/setter ์ด์™ธ์˜ ๋กœ์ง์ด ๋ถˆํ•„์š”ํ•จ
VO - ๊ฐ’ ์ž์ฒด๋ฅผ ํ‘œํ˜„ - ์†์„ฑ๊ฐ’์ด ๋ชจ๋‘ ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ๊ฐ์ฒด - ๋ถˆ๋ณ€ - getter/setter ์ด์™ธ์˜ ๋กœ์ง์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ

์ฐธ๊ณ ์ž๋ฃŒ

https://melonicedlatte.com/2021/07/24/231500.html
https://lemontia.tistory.com/591
https://dkswnkk.tistory.com/500

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ