데이터 접근 객체

Jmnote (토론 | 기여)님의 2018년 10월 14일 (일) 18:47 판 (→‎개요)

1 개요

data access object (DAO)
데이터 접근 객체, 데이터 액세스 객체

2 구현

  • 데이터 요청시: DB커넥션 클래스(싱글턴 패턴 응용)로부터 getConnection[4]
  • 생성시 데이터소스를[5]입력받는 경우도 있음
인스턴스 생성시: 실제 커넥션을 맺는 것은 아니고, 데이터소스[5]만 저장

3 그림 예시

패턴 구조[7]

 

객체간 상호작용[8]

 

4 구현 예시 1

public class Person {
  ... (생략)
}
public class PersonDao {
  protected DataSource dataSource = null;

  public PersonDao(DataSource dataSource){
    this.dataSource = dataSource;
  }

  public Person readPerson(long personId){
    Connection connection = this.dataSource.getConnection();
    Person person = ... (생략)
    return person;
  }
}
Person person = personDao.readPerson(123);

5 구현 예시 2

class Employee {
  protected $id;  
  protected $name;  
  protected $birth;  
  function __contruct($row) {
    $this->id = $row['id'];
    $this->name = $row['name'];
    $this->birth = $row['birth'];
  }
}
class EmployeeDao {
  protected $dataSource = null;

  function __construct(DataSource $dataSource) {
    $this->dataSource = $dataSource;
  }
  function readEmployee($employeeId) {
    $connection = $this->dataSource->getConnection();
    $row= $connection->query_row("SELECT * FROM employee WHERE id=?", $employeeId);
    return new Employee($row);
  }
}

6 같이 보기

7 주석

  1. 주로 DB
  2. 데이터베이스 또는 파일시스템
  3. J2EE 패턴에서는 통합 티어
  4. 커넥션 재사용함으로써 커넥션 비용 감소
  5. 5.0 5.1 어떤 DB에 접속해야 하는지에 대한 정보
  6. http://www.oracle.com/technetwork/java/dataaccessobject-138824.html
  7. 클래스 다이어그램
  8. 시퀀스 다이어그램

8 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}