관리 메뉴

개미 개발자

DAO, DTO, VO란? 본문

Spring

DAO, DTO, VO란?

Volc 2022. 1. 6. 19:51

DAO (Data Access Object)


  • 말 그대로 데이터에 접근하기 위해 Object이다.
  • DB의 데이터에 접근하기 위한 객체로 직접 DB에 접근하여 데이터를 삽입, 삭제, 조회 등 조작할 수 있는 기능을 수행한다.
  • Mybatis 처럼 커넥션풀까지 제공되고 있어 DAO를 별도로 만드는 경우는 드물다
  • 코드
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class MyConn01 {
    
    	public static void main(String[] args) {
    		// JDBC 드라이버 로드
    				// Database Connection 연결
    				// Statement 생성
    				// SQL 전송 --> executeQuery(), executeUpdate()
    				// ResultSet(결과) 받기
    				// Database Connection 해제
    		
    		String jdbcDriver = "com.mysql.cj.jdbc.Driver";
    		String jdbcUrl = "jdbc:mysql://localhost/empdb";
    		
    		try {
    			// JDBC 드라이버 로드
    			Class.forName(jdbcDriver);
    
    			// Database Connection 연결
    			Connection conn = DriverManager.getConnection(jdbcUrl,"root","12341234");
    			
    			// Statement 생성
    			String sql = "select * from dept";
    			Statement stmt = conn.createStatement();
    			// SQL 전송 --> executeQuery(), executeUpdate()
    			// ResultSet(결과) 받기
    			ResultSet rs = stmt.executeQuery(sql);
    			
    			while(rs.next()) {
    				System.out.printf("%d |", rs.getInt("deptno"));
    				System.out.printf("%-10s |", rs.getString(2));
    				System.out.printf("%-10s \n", rs.getString("loc"));
    			}
    			
    			
    			// Database Connection 해제
    			stmt.close();
    			conn.close();
    		}
    		catch (Exception e) {
    			System.out.println("연결 안됨");
    		}
    	}
    }​

DTO (Data Transfer Object)


  • 데이터 전송 객체이다.
  • DTO는 계층간(Controller, View, Business Layer) 데이터 교환을 위한 자바 빈즈(Java Beans)를 의미한다.
  • Java Beans는 계층간 데이터 교환을 위한 객체를 말한다.
  • DB의 데이터를 Service나 Controller 등으로 보낼 때 사용하는 객체를 말한다.
  • 로직을 갖고 있지 않는 순수한 데이터 객체이며, getter / setter 메서드만을 갖는다.
  • 코드
    package mybean.db;
    
    public class Article {
    	private int idx;
    	private String id;
    	private String name;
    	private String pwd;
    	private String gender;
    	
    	public Article() {}
    	
    	public void setIdx(int idx) {
    		this.idx =idx;
    	}
    	
    	public int getIdx() {
    		return idx;
    	}
    
    	/**
    	 * @return the id
    	 */
    	public String getId() {
    		return id;
    	}
    
    	/**
    	 * @param id the id to set
    	 */
    	public void setId(String id) {
    		this.id = id;
    	}
    
    	/**
    	 * @return the name
    	 */
    	public String getName() {
    		return name;
    	}
    
    	/**
    	 * @param name the name to set
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	/**
    	 * @return the pwd
    	 */
    	public String getPwd() {
    		return pwd;
    	}
    
    	/**
    	 * @param pwd the pwd to set
    	 */
    	public void setPwd(String pwd) {
    		this.pwd = pwd;
    	}
    	
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
    	
    	public String getGender() {
    		return gender;
    	}
    	
    }​
     

VO


  • DTO와 달리 VO는 Read-Only 속성을 갖는 값 오브젝트이다.
  • 단순히 값 타입을 표현하기 위해 불변 클래스(Read-Only)를 만들어 사용한다. 즉, getter만 존재한다.
  • Color 객체를 보면 Color.RED, Color.GREEN과 같이 값만 가져올 수 있다.

DTO vs VO


  • DTO는 인스턴스 개념이라면 VO는 리터럴 개념이다.
  • VO는 특정한 비즈니스 값을 담는 객체이고, DTO는 Layer간의 통신 용도로 오고가는 객체를 말한다.

참고 사이트

DAO, DTO, VO,Entity 차이
[JAVA] DAO, DTO, VO 차이

Comments