posted by 귀염둥이채원 2019. 3. 28. 16:05

# DAO(Data Access Object)
- Database의 data에 접근을 위한 객체
- Database에 접근을 하기위한 로직과 비즈니스 로직을 분리하기 위해서 사용
- DB를 사용해 데이터를 조화하거나 조작하는 기능을 전담하도록 만든 오브젝트
- Service와 DB를 연결하는 고리의 역할을 한다
- DAO의 경우는 DB와 연결할 Connection 까지 설정되어 있는 경우가 많음

# DAO 클래스

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestDao {
    public void add(TestDto dto) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");

        PreparedStatement preparedStatement = connection.prepareStatement("insert into users(id,name,password) value(?,?,?)");

        preparedStatement.setString(1, dto.getName());
        preparedStatement.setInt(2, dto.getValue());
        preparedStatement.setString(3, dto.getData());
        preparedStatement.executeUpdate();
        preparedStatement.close();
        
        connection.close();
    }
}


# DTO((Data Transfer Object)
- DB에서 데이터를 얻어 Service나 Controller 등으터 보낼 때 사용하는 객체
- 로직을 갖고 있지 않는 순수한 데이터 객체이며 속성과 그 속성에 접근하기 위한 getter, setter 메소드만 가진 클래스

# DTO 클래스

public class TestDto {

    private String name;
    private int value;
    private String data;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}


# VO(Value Object) vs DTO
- VO는 DTO와 동일한 개념이지만 read only 속성을 갖는다.
- VO는 특정한 비즈니스 값을 담는 객체이고, DTO는 Layer간의 통신 용도로 오고가는 객체를 말한다.

# 참고 사이트
https://jungwoon.github.io/common%20sense/2017/11/16/DAO-VO-DTO/
https://gmlwjd9405.github.io/2018/12/25/difference-dao-dto-entity.html
https://lemontia.tistory.com/591
https://lazymankook.tistory.com/30

'Spring Boot' 카테고리의 다른 글

스프링부트 로그 설정  (0) 2019.03.29
[Spring Boot] MyBatis + Mysql 샘플2  (0) 2019.03.29
[Spring Boot] MyBatis + Mysql 샘플  (0) 2019.03.29
스프링 MVC 패턴이란?  (0) 2019.03.28
WEB MVC HelloWorld  (0) 2019.03.28