์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ์ฝ๋ฉ
- ๋ฆฌ์กํธ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- JavaScript
- ๋ ์
- ์๋ฐ
- css
- ๋ผํ๋ผ์ค์๋ง๋
- database
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- Java
- ์ํ
- ๊ฐ๋ฐ
- ํ์ด์ฌ
- ComputerScience
- ํ๋ก๊ทธ๋๋ฐ
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์ค๋ธ์
- Python
- ์นํผ๋ธ๋ฆฌ์ฑ
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ์นํ์ด์ง๋ง๋ค๊ธฐ
- ์ฑ
- html
- K๋ฐฐํฐ๋ฆฌ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ๋ง์ผ๋ด๊ฐ์ธ์์๋ค์์ฐ๋ค๋ฉด
- ์ค๋ผํด
- ์ปดํจํฐ๊ณผํ
- ๋ฐ์ํ
- Today
- Total
JiYoung Dev ๐ฅ
[JAVA ๋ณต์ต] ๊ฒ์ ๋ง๋ค๊ธฐ : MVC ํจํด, ์ถ์ ํด๋์ค, ์ธํฐํ์ด์ค (2023.05.01) ๋ณธ๋ฌธ
[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();
}
}
}