Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- jdk
- Linux
- Immutable
- centos8
- Guava
- DBeaver
- CentOS
- vm
- DTO
- DOM
- dao
- STS
- Mutable
- Vo
- PreparedStatement
- Big Sur
- ssh
- datanodes
- Hive
- react
- hive-site.xml
- JPS
- hadoop
- 방화벽
- bom
- jre
- Statement
- variable
- Vue.js
- Angular
Archives
- Today
- Total
개미 개발자
DAO, DTO, VO란? 본문
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간의 통신 용도로 오고가는 객체를 말한다.
참고 사이트
Comments