๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[JAVA ๋ณต์Šต] ๊ฒŒ์ž„ ๋งŒ๋“ค๊ธฐ : MVC ํŒจํ„ด, ์ถ”์ƒ ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค (2023.05.01) ๋ณธ๋ฌธ

full stack/JAVA

[JAVA ๋ณต์Šต] ๊ฒŒ์ž„ ๋งŒ๋“ค๊ธฐ : MVC ํŒจํ„ด, ์ถ”์ƒ ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค (2023.05.01)

Shinjio 2023. 5. 2. 01:32

 

๐ŸŽˆ MVC ํŒจํ„ด 

Model. View, Controller๋กœ ์ด๋ฃจ์–ด์ง„ ์†Œํ”„ํŠธ์›จ์–ด ๋””์ž์ธ ํŒจํ„ด

 

 

๐ŸŽˆ ์ถ”์ƒ ํด๋ž˜์Šค 

๊ตฌ์ฒด์ ์ด์ง€ ์•Š์€ ์ถ”์ƒ์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค

์ž์‹ ์˜ ๊ธฐ๋Šฅ๋“ค์„ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ ํ™•์žฅ

๋น„์Šทํ•œ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •

์ค‘๋ณต ๋ฉค๋ฒ„ ํ†ตํ•ฉ์— ์‚ฌ์šฉ - ๊ฐ ํด๋ž˜์Šค์— ๋˜‘๊ฐ™์€ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ 

๋ช…ํ™•ํ•œ ๊ณ„์ธต ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ•  ๋•Œ

 

 

๐ŸŽˆ ์ธํ„ฐํŽ˜์ด์Šค

๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„์— ์ƒ๊ด€์—†์ด, ์›ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋งŒ ํ˜ธ์ถœํ•˜๊ณ  ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ƒํ˜ธ์ž‘์šฉ ๊ธฐ๋Šฅ 

 

 

๐ŸŽˆ ๊ฒŒ์ž„๋งŒ๋“ค๊ธฐ ํ”„๋กœ์ ํŠธ

 

 

DAO vs DTO vs VO ์ฐจ์ด์ 

1. DAO(Data Access Object)
DB์˜ data์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
์ง์ ‘ DB์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…, ์‚ญ์ œ, ์กฐํšŒ ๋“ฑ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ ์ˆ˜ํ–‰
MVCํŒจํ„ด์˜ Model์—์„œ ์ˆ˜ํ–‰

2. DTO(Data Transfer Object)
๊ณ„์ธต๊ฐ„(Controller, View, Business Layer) ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•œ ์ž๋ฐ” ๋นˆ์ฆˆ๋ฅผ ์˜๋ฏธ
๋กœ์ง์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ๊ฐ์ฒด์ด๊ณ , getter/setter๋ฉ”์„œ๋“œ๋งŒ ๊ฐ€์ง„ ํด๋ž˜์Šค
๊ฐ€๋ณ€์˜ ์„ฑ๊ฒฉ์„ ๊ฐ€์ง„ ํด๋ž˜์Šค์ด๋ฉฐ ๋ฐ์ดํ„ฐ ์ „์†ก์„ ์œ„ํ•ด ์กด์žฌ

3. VO(Value Object)
DTO์™€ ๋‹ฌ๋ฆฌ VO๋Š” read-only ์†์„ฑ์˜ ๊ฐ’ ์˜ค๋ธŒ์ ํŠธ
getter ๊ธฐ๋Šฅ๋งŒ ์กด์žฌ
๊ฐ’ ๊ทธ ์ž์ฒด์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ ๋ถˆ๋ณ€ ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธ

 

 

์ถ”์ƒ ๋ฉ”์„œ๋“œ

๋ชจ๋“  ํด๋ž˜์Šค์— ํ•„์š”ํ•˜์ง€๋งŒ, ๊ฐ๊ฐ์€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง

 

defensible ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌํ˜„ํ•˜์—ฌ ๋ผ์›Œ ๋„ฃ์–ด ์“ธ ์ˆ˜ ์žˆ๋„๋ก 

์ธํ„ฐํŽ˜์ด์Šค ์ด๋ฆ„์€ ~able์„ ๋งŽ์ด ๋ถ™์ธ๋‹ค. 

์„œ๋กœ ๋‹ค๋ฅธ ๋ถ€๋ชจ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ์—๋„ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ด์ฃผ๋Š” ๊ฒƒ

 

 

 

VO ๊ธฐ๋Šฅ์—†์ด ๊ฐ’๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์Œ

 

์˜ค๋ผํด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ‘œ ์ƒ์„ฑ ์ฝ”๋“œ

create table SwordVO (itemNum Number, name varchar2(30), attackPower Number);

select * from SwordVO;

create table BowVO (itemNum number, name varchar2(30), attackPower number, durability number);

select * from BowVO;

 

 

 

๋‹คํ˜•์„ฑ 

๊ฐ™์€ ์ด๋ฆ„์ด์ง€๋งŒ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. → ์˜ค๋ฒ„๋ผ์ด๋”ฉ 

 

 

Sword, Bow ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ธ์Šคํ„ด์Šค ์ถ”๊ฐ€

insert into SwordVO (
       itemNum
     , name
     , attackPower
) values (
      0
    , '๋ชฉ๋„'
    , 10
);

insert into SwordVO (
       itemNum
     , name
     , attackPower
) values (
      0
    , '๋ชฉ๊ฒ€'
    , 15
);

insert into BowVO (
       itemNum
     , name
     , attackPower
     , durability
) values (
      0
    , '๋ชฉ๋„'
    , 15
    , 10
);

commit;

update BowVO set name = '๊ธฐ๋ณธํ™œ';

commit;

 

 

package VIEW;

public class Main_display {

	public void render() {
		System.out.println("======================================");
		System.out.println("================ ๋ฉ”๋‰ด ์„ ํƒ ==============");
		System.out.println("==1.์•„์ดํ…œ ์กฐํšŒ 2.์บ๋ฆญํ„ฐ ์ƒ์„ฑ 3.์žฅ๋น„์žฅ์ฐฉ 4.์ข…๋ฃŒ==");
		
	}

}

 

 

DAO ์ฝ”๋“œ

package MODEL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DAO {
//	๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
//	๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ, ํ•ด์ œ, ์กฐํšŒ, ๋ฐ์ดํ„ฐ ์‚ฝ์ž…, ์‚ญ์ œ ๋“ฑ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰
	
	Connection conn;
	PreparedStatement psmt = null;
	ResultSet rs;
	
// 	๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ
	public void connect() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String db_id = "service";
			String db_pw = "12345";
			conn = DriverManager.getConnection(url, db_id, db_pw);
		} catch (ClassNotFoundException | SQLException e) {
			System.out.println("DB์—ฐ๊ฒฐ์‹คํŒจ");
			e.printStackTrace();
		}
	}
	
//	๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ํ•ด์ œ
	public void getClose() {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(psmt!=null) {
				psmt.close();
			}
			if(conn!=null) {
				conn.close();
			}
		}
			catch (SQLException e) {
				e.printStackTrace();
		}
		
	}


// Bow ์•„์ดํ…œ ์กฐํšŒ
	public ArrayList<ItemVO> bowList() {
		ArrayList<ItemVO> BowList = new ArrayList<ItemVO>();
		
		connect();

		try {
			String sql = "select * from BowVO";
			psmt = conn.prepareStatement(sql);
			rs = psmt.executeQuery();
			
//			rs์˜ ์ปค์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธ
			while(rs.next()) {
				BowVO bow = new BowVO(0, null, 0, 0);
				bow.setItemNum(rs.getInt(1));
				bow.setName(rs.getString(2));
				bow.setAttackPower(rs.getInt(3));
				bow.setDurability(4);
				BowList.add(bow);				
			}
			
			getClose();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return BowList;
		
	}
	
// Sword ์•„์ดํ…œ ์กฐํšŒ
	
	public ArrayList<ItemVO> swordList() {
		ArrayList<ItemVO> SwordList = new ArrayList<ItemVO>();
		
		connect();

		try {
			String sql = "select * from SwordVO";
			psmt = conn.prepareStatement(sql);
			rs = psmt.executeQuery();
			
//			rs์˜ ์ปค์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธ
			while(rs.next()) {
				SwordVO sword = new SwordVO(0, null, 0);
				sword.setItemNum(rs.getInt(1));
				sword.setName(rs.getString(2));
				sword.setAttackPower(rs.getInt(3));
				SwordList.add(sword);				
			}
			
			getClose();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return SwordList;
		
	}
	
}

 

AllSelectionItem ์ฝ”๋“œ

 

package CONTROLLER;

import java.util.ArrayList;

import MODEL.BowVO;
import MODEL.DAO;
import MODEL.ItemVO;

public class AllSelectionItem {
	
//	์ „์ฒด ์•„์ดํ…œ ์กฐํšŒ
	public void rangeAll(int select) {
		if(select == 1) {
//			์ „์ฒด ์•„์ดํ…œ ์กฐํšŒ
			allWeapon();
		}else if(select == 2) {
//			๊ฒ€ ์•„์ดํ…œ ์กฐํšŒ
			allSword();
		}else if(select == 3) {
//			ํ™œ ์•„์ดํ…œ ์กฐํšŒ
			allBow();
		}else {
			System.out.println("๋ฒˆํ˜ธ๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•˜์„ธ์š”.");
		}
	}
	
//	์ „์ฒด ์•„์ดํ…œ ์กฐํšŒ
	public void allWeapon() {
		DAO dao = new DAO();
		ArrayList<ItemVO> bowList = dao.bowList();
		ArrayList<ItemVO> swordList = dao.swordList();
		ArrayList<ItemVO> totalList = new ArrayList<ItemVO>();
		
		totalList.addAll(bowList); 
		totalList.addAll(swordList);
		
		System.out.println("num\tname\tpower\tdurablity");
		System.out.println("---------------------------");
		
		for(ItemVO item : totalList) {
			System.out.print(item.getItemNum() + "\t");
			System.out.print(item.getName() + "\t");
			System.out.print(item.getAttackPower() + "\t");
			
			try {
				System.out.println(((BowVO)item).getDurability());
			}catch(Exception classCaseException) {
				System.out.println("");
			
			}
			System.out.println();
		}
	}
	
//	๊ฒ€ ์•„์ดํ…œ ์กฐํšŒ
	public void allSword() {
		DAO dao = new DAO();
		ArrayList<ItemVO> swordList = dao.swordList();
		
		System.out.println("num\tname\tpower\tdurablity");
		System.out.println("---------------------------");
		
		for(ItemVO item : swordList) {
			System.out.print(item.getItemNum() + "\t");
			System.out.print(item.getName() + "\t");
			System.out.print(item.getAttackPower() + "\t");
			System.out.println();
		}
	}
	
//	Bow ์•„์ดํ…œ ์กฐํšŒ
	public void allBow() {
		DAO dao = new DAO();
		ArrayList<ItemVO> BowList = dao.bowList();
		
		System.out.println("num\tname\tpower\tdurablity");
		System.out.println("---------------------------");
		
		for(ItemVO item : BowList) {
			System.out.print(item.getItemNum() + "\t");
			System.out.print(item.getName() + "\t");
			System.out.print(item.getAttackPower() + "\t");
			System.out.println(((BowVO)item).getDurability());
			System.out.println();
		}
	}
}