VO,DAO,DTO??

momomoki·2023년 11월 5일
0

DAO??

DAO는 Data Access Object의 약자로, Database에 접근하는 역활을 하는 객체, 프로젝트의 서비스 모델에 해당하는 부분과 데이터베이스를 연결하는 역활 데이터의 CRUD작업을 시행하는 클래스 즉 데이터에 대한 CRUD기능을 전담하는 객체
예제코드를 보자
codyboard.dao

package com.kkj.codyboard.dao;


import org.apache.ibatis.session.SqlSession;

import com.kkj.codyboard.dto.CodyBoardDto;
import com.kkj.mybatis.MybatisConnectionFactory;

public class CodyBoardDao {

	public int insert(CodyBoardDto codyBoardDto) {
		// TODO Auto-generated method stub
		int result = 0;
		SqlSession sqlSession = MybatisConnectionFactory.getSqlSession();
		result = sqlSession.insert("insertCody", codyBoardDto);
		sqlSession.close();
		return result;
	}
}

codyboard.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
	<insert id="insertCody" parameterType="CodyBoardDto">
		INSERT INTO codyboard values(seq_cody.NEXTVAL, #{title}, #{content}, #{images}, 0, sysdate, #{categoryID}, #{userID})
	</insert>
</mapper>

dao부분의 코드를보면 일단 저는 mybatis를 사용하여 데이터베이스를 사용하였습니다. 그래서 sqlSession에서 데이터베이스를 접속하여 저장하는 방식입니다. 이런식으로 데이터베이스에 접근하는 것을 dao라고 합니다.

DTO

DTO는 Data transfer Object의 약자로, 데이터를 전달하기 위한 객체이며 DTO는 로직을 가지지 않은 순수한 데이터 객체(getter, setter만 가진 클래스)이며 주로 View와 Controller 사이에서 사용하고 DTO는 getter, setter메소르를 포함하며 다른 비지니스 로직을 포함하지 않는다.
DTO는 가변객체로도 사용가능하고 불변객체로도 사용이 가능하며 DTO는 데이터를 전달만을 위한 객체라는게 가장 중요하다.
예제코드

package com.kkj.codyboard.dto;

public class CodyBoardDto {
	private int no;
	private String title;
	private String content;
	private String images;
	private int hit;
	private String regDate; 
	private int categoryID;
	private String userID;

	public CodyBoardDto() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getImages() {
		return images;
	}
	public void setImages(String images) {
		this.images = images;
	}
	public int getHit() {
		return hit;
	}
	public void setHit(int hit) {
		this.hit = hit;
	}
	public String getRegDate() {
		return regDate;
	}
	public void setRegDate(String regDate) {
		this.regDate = regDate;
	}
	public int getCategoryID() {
		return categoryID;
	}
	public void setCategoryID(int categoryID) {
		this.categoryID = categoryID;
	}
}

위 코드처럼 데이터를 넣기위한 setter메서드와 데이터를 보기 위한 getter메소드만 존재한다. DTO는 이렇게 기본 생성자에 setter, getter메서드만 들어가야한다.
불변객체는 private final 멤버변수 그리고 setter메서드는 없고 처음에 선언할때 멤버변수들을 같이 매게변수로 추가하여 선언해서 바꿀수없게 사용하는것을 불변객체라고한다.

VO

vo는 value OBject의 약자로 값 자체를 표현하는 객체 DTO와 비슷하지만 DTO는 setter 메서드를 가지고 있어 데이터 값을 변경할 수 있지만 VO는 setter메서드가 없고 getter메서드와 비지니스 로직을 포함할 수 있다. 그래서 setter메서드가 없어 오직 읽기전용(read only)만 가능하다. VO는 두 객체의 모든 필드 값들이 동일하면 두 객체는 같다. 이것이 VO의 핵심 정의이며 두 객체가 같지 떄문에 equals()와 hashCode()의 오버라이딩이 중요하며 메소드는 어떤 메소드가 있는지는 상관이 없다.

public class VoEx {
    static class Money{
        private final int value;

        Money(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
				
	// 사용자 생성 메소드이지만 setter형태의 메소드가 아니므로 무관함
        public String printMoney(){
            return this.value + "원";
        }
					
	// 두 객체의 모든 필드 값들이 모두 같으면 같은 객체이도록 만들기 위한 오버라이딩
		@Override
        public int hashCode() {
            return Objects.hash(value);
        }

        @Override
        public boolean equals(Object obj) {
            if(this == obj)
                return true;
            if(obj == null || getClass() != obj.getClass())
                return false;
            Money money = (Money) obj;
            if(value == money.value)
                return true;
            else
                return false;
        }
    }
}
profile
얍얍엽엽욥욥

0개의 댓글