PlayData/MySQL

[Day 17 | ๊ณผ์ œ] DAO, DTO, VO ์ฐจ์ด(~ing)

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 12. 3. 17:57

1) DAO (Data Access Object) 

DAO๋Š” DB์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด.

์ง์ ‘ DB์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…, ์‚ญ์ œ, ์กฐํšŒ ๋“ฑ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์Œ 

 

DB ์ ‘๊ทผ ๋กœ์ง๊ณผ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ. 

 

์ดํ•˜ ๋‚˜์˜ DAO: 

๋”๋ณด๊ธฐ

DeptDAO.java

package com.my;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DeptDAO {

	// Singleton
	private static DeptDAO dao = new DeptDAO();
	private DeptDAO(){ }
	public static DeptDAO getInstance() { return dao; }
	/////////////////
	DeptVO selectDept(int deptno, Connection conn) {
		
		DeptVO dept = null;
		try {
			//String sql = "select * from dept where deptno = "+ deptno;
			String sql = "select * from dept10 where deptno = ?";
			//Statement stmt = conn.createStatement();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, deptno); 
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()) {
				dept = new DeptVO(rs.getInt(1),rs.getString(2),rs.getString("loc"));
				System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
			}
			pstmt.close();
			//stmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return dept;
	}
	
	int insertDept(DeptVO deptObj, Connection conn) {
		
		int resultCount = 0;
		DeptVO dept = null;

		try {
			String sql = "insert into dept10 values(?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, deptObj.getDeptno()); 
			pstmt.setString(2, deptObj.getDname());
			pstmt.setString(3, deptObj.getLoc());
			
			resultCount = pstmt.executeUpdate();
			
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return resultCount;
	}

	int updateDept(DeptVO deptObj,Connection conn) {
		
		int resultCount = 0;
		
		try {
			String sql = "update dept10 set loc = ? where deptno = ?"; ;
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(2, deptObj.getDeptno()); 
			pstmt.setString(1, deptObj.getLoc());
			resultCount = pstmt.executeUpdate();
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return resultCount;
	}
	
	int deleteDept(int  deptno,Connection conn ) { 
		int resultCount = 0;
		
		try {
			String sql = "delete from dept10 where deptno = ?"; ;
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, deptno); 
			resultCount = pstmt.executeUpdate();
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return resultCount;
	}
	
	List<DeptVO> listDept(Connection conn) {
		
		List<DeptVO> deptList = new ArrayList<DeptVO>();

		try {
			
			String sql = "select * from dept10";
			
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()) {
				System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
				deptList.add(new DeptVO(rs.getInt(1),rs.getString(2),rs.getString(3)));
			}
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return deptList;
	}
	
}

 

 

 

2) DTO (Data Transfer Object)

๊ณ„์ธต๊ฐ„(Controller, View, Business Layer) ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•œ ์ž๋ฐ”๋นˆ์ฆˆ(....??)๋ฅผ ์˜๋ฏธํ•จ

๋กœ์ง์„ ๊ฐ€์ง€์ง€ ์•Š๊ณ , getter / setter ๋ฉ”์†Œ๋“œ๋งŒ ๊ฐ€์ง„ ํด๋ž˜์Šค์ž„. 

- ์ž๋ฐ”๋นˆ์ฆˆ: ์ž๋ฐ”๋กœ ์ž‘์„ฑ๋œ ์†Œํ”„ํŠธ์›จ์–ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ง€์นญํ•˜๋Š” ๋‹จ์–ด 

 

 

 

 

3) VO (Value Object)

๋ฐ์ดํ„ฐ ๊ทธ ์ž์ฒด๋กœ ์˜๋ฏธ์žˆ๋Š” ๊ฒƒ์„ ๋‹ด๊ณ ์žˆ๋Š” ๊ฐ์ฒด 

Read only ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด

RDB์˜ ๋ ˆ์ฝ”๋“œ์— ๋Œ€์‘๋˜๋Š” ์ž๋ฐ” ํด๋ž˜์Šค

๊ฐ„๋‹จํ•œ ๋…๋ฆฝ์ฒด( Entity )๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ž‘์€ ๊ฐ์ฒด

VO์˜ ํ•ต์‹ฌ ์—ญํ• ์€ equals()์™€ hashcode() ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜๋Š” ๊ฒƒ. (....??)

 

 

 

์ดํ•˜ ๋‚˜์˜ VO:

๋”๋ณด๊ธฐ

DeptVO.java

package com.my;

// ๋ฐ์ดํ„ฐ ๊ฐ์ฒด 
public class DeptVO {
	
	//๋ฉค๋ฒ„ ํ•„๋“œ
	private int deptno;
	private String dname;
	private String loc;
	
	// ์ƒ์„ฑ์ž 
	DeptVO(){ } // ์€๋‹‰๋œ ๊ฒƒ
	DeptVO(int deptno, String dname, String loc){
		this.deptno = deptno; this.dname = dname; this.loc = loc;  }
	
	// ๋ฉค๋ฒ„ ๋ฉ”์†Œ๋“œ ( ๋ฉ”๋‰ด source์—์„œ --> Generate getters and setter์—์„œ ์ž๋™ ์ƒ์„ฑ)  
	public int getDeptno() {	return deptno; }
	public String getDname() {	return dname;  }
	public String getLoc() {	return loc;		}	
	@Override
	public String toString() {
		return "DeptVO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", toString()=" + super.toString()
				+ "]";
	}

}

 

 

Reference: 

https://genesis8.tistory.com/214

 

DAO / VO / DTO๋ž€?

์›๋ณธ ์ถœ์ฒ˜ : http://lbass.tistory.com/entry/DAO-%EB%9E%80 http://choijaehyuk.com/128 http://everyit.tistory.com/4 DAO๋ž€ Data Access Object์˜ ์•ฝ์–ด๋กœ์„œ ์‹ค์งˆ์ ์œผ๋กœ DB์— ์ ‘๊ทผํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค. DAO์˜..

genesis8.tistory.com

https://ijbgo.tistory.com/9

 

VO vs DTO

VO vs DTO VO(Value Object) -       ๋ฐ์ดํ„ฐ ๊ทธ ์ž์ฒด๋กœ ์˜๋ฏธ ์žˆ๋Š” ๊ฒƒ์„ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ด๋‹ค. -       DTO์™€ ๋™์ผํ•œ ๊ฐœ๋…์ด๋‚˜ ์ฐจ์ด์ ์€ Read–Only ์†์„ฑ ๊ฐ์ฒด์ด๋‹ค. -       ๊ฐ„๋‹จํ•œ ๋…๋ฆฝ์ฒด( Entit..

ijbgo.tistory.com

https://velog.io/@ha0kim/DAO-DTO-VO-%EC%B0%A8%EC%9D%B4

 

DAO, DTO, VO,Entity ์ฐจ์ด

์™„์ „ ๊ธฐ๋ณธ

velog.io