DAO(Data Access Object)

Joy๐ŸŒฑยท2023๋…„ 1์›” 23์ผ
0

๐Ÿ˜ JDBC

๋ชฉ๋ก ๋ณด๊ธฐ
3/3
post-thumbnail

๐Ÿ’โ€โ™€๏ธ DAO(Data Access Object)๋ž€,
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ์šฉ ๊ฐ์ฒด. CRUD ์—ฐ์‚ฐ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํด๋ž˜์Šค

  • ์–ด๋– ํ•œ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ„๋‹จํžˆ ํ˜ธ์ถœ๋งŒํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ๋ฉ”์†Œ๋“œ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ DAO์— ๋‹ด์•„ ์‚ฌ์šฉ

๐Ÿ‘€ DAO์˜ ์‚ฌ์šฉ

๐Ÿ“Œ ์ดˆ๊ธฐ Setting

โ—ผ Setting

โ—ผ menu-query.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<!-- '1. ์นดํ…Œ๊ณ ๋ฆฌ ์ „์ฒด ์กฐํšŒ'๋ฅผ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="selectAllCategory">
		SELECT
				CATEGORY_CODE
			,	CATEGORY_NAME
			,	REF_CATEGORY_CODE
			FROM TBL_CATEGORY
	</entry>

	<!-- '2. ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก'์„ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="insertMenu">
		INSERT
			INTO TBL_MENU
		(
			MENU_CODE
		,	MENU_NAME
		, 	MENU_PRICE
		,	CATEGORY_CODE
		,	ORDERABLE_STATUS
		)
		VALUES
		(
			SEQ_MENU_CODE.NEXTVAL <!-- ์‹œํ€€์Šค๋กœ MENU_CODE ์ปฌ๋Ÿผ ๊ฐ’ ๊ณ„์† ์ƒ์„ฑ -->
		,	?
		,	?
		,	?
		,	?		
		)
	</entry>
</properties>

โ—ผ JDBCTemplate

public class JDBCTemplate { 
	
	/*  Connection์„ return์‹œํ‚ฌ ๋ฉ”์†Œ๋“œ */ 
	public static Connection getConnection() {
		
		Connection conn = null;
		Properties prop = new Properties();
		
		try {
			prop.load(new FileReader("config/jdbc-config.properties")); // config๋ถ€ํ„ฐ ์ž‘์„ฑ
			
			String driver = prop.getProperty("driver");
			String url = prop.getProperty("url");
			
			Class.forName(driver);
			
			conn = DriverManager.getConnection(url, prop);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		
		return conn;
	}
	/* Connection์„ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(Connection conn) {
		try {
			if(conn != null && !conn.isClosed()) {
				conn.close(); 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* Statement๋ฅผ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(Statement stmt) {
		try {
			if(stmt != null && !stmt.isClosed()) {
				stmt.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* ResultSet์„ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(ResultSet rset) {
		try {
			if(rset != null && !rset.isClosed()) {
				rset.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

๐Ÿ‘‰ DTO

โ—ผ CategoryDTO Class

public class CategoryDTO {

	/*
	CATEGORY_CODE	NUMBER
	CATEGORY_NAME	VARCHAR2(30 BYTE)
	REF_CATEGORY_CODE	NUMBER
	*/

	/* ํ•„๋“œ */
	private int categoryCode;
	private String categoryName;
	private int refCategoryCode;
	
	/* ์ƒ์„ฑ์ž */
	public CategoryDTO() {}
	
	public CategoryDTO(int categoryCode, String categoryName, int refCategoryCode) {
		super();
		this.categoryCode = categoryCode;
		this.categoryName = categoryName;
		this.refCategoryCode = refCategoryCode;
	}

	/* getter & setter */
	public int getCategoryCode() {
		return categoryCode;
	}

	public void setCategoryCode(int categoryCode) {
		this.categoryCode = categoryCode;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public int getRefCategoryCode() {
		return refCategoryCode;
	}

	public void setRefCategoryCode(int refCategoryCode) {
		this.refCategoryCode = refCategoryCode;
	}

	/* toString */
	@Override
	public String toString() {
		return "CategoryDTO [categoryCode=" + categoryCode + ", categoryName=" + categoryName + ", refCategoryCode="
				+ refCategoryCode + "]";
	}
}

โ—ผ MenuDTO Class

public class MenuDTO {

	/* 
	MENU_CODE	NUMBER
	MENU_NAME	VARCHAR2(30 BYTE)
	MENU_PRICE	NUMBER
	CATEGORY_CODE	NUMBER
	ORDERABLE_STATUS	CHAR(1 BYTE)
	*/
	
	private int menuCode;
	private String menuName;
	private int menuPrice;
	private int categoryCode;
	private String orderableStatus;
	
	/* ์ƒ์„ฑ์ž */
	public MenuDTO() {}
	
	public MenuDTO(int menuCode, String menuName, int menuPrice, int categoryCode, String orderableStatus) {
		super();
		this.menuCode = menuCode;
		this.menuName = menuName;
		this.menuPrice = menuPrice;
		this.categoryCode = categoryCode;
		this.orderableStatus = orderableStatus;
	}

	
	/* getter & setter */

	public int getMenuCode() {
		return menuCode;
	}

	public void setMenuCode(int menuCode) {
		this.menuCode = menuCode;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public int getMenuPrice() {
		return menuPrice;
	}

	public void setMenuPrice(int menuPrice) {
		this.menuPrice = menuPrice;
	}

	public int getCategoryCode() {
		return categoryCode;
	}

	public void setCategoryCode(int categoryCode) {
		this.categoryCode = categoryCode;
	}

	public String getOrderableStatus() {
		return orderableStatus;
	}

	public void setOrderableStatus(String orderableStatus) {
		this.orderableStatus = orderableStatus;
	}
	
	@Override
	public String toString() {
		return "MenuDTO [menuCode=" + menuCode + ", menuName=" + menuName + ", menuPrice=" + menuPrice
				+ ", categoryCode=" + categoryCode + ", orderableStatus=" + orderableStatus + "]";
	}
}

๐Ÿ‘‰ DAO

โ—ผ MenuDAO Class

public class MenuDAO {

	>>> ๋ฉ”์†Œ๋“œ ์•ˆ์ด ์•„๋‹Œ, ํ•„๋“œ์— Properties ์„ ์–ธ 
    >>> (๋ชจ๋“  ๋ฉ”์†Œ๋“œ์—์„œ xml ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ)
    /* ํ•„๋“œ */
	private Properties prop; 
	
	/* ๊ธฐ๋ณธ ์ƒ์„ฑ์ž */ >>> ํ•„๋“œ์— ๋Œ€ํ•ด ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ๊ฐ์ฒด์ชฝ์œผ๋กœ xml ํŒŒ์ผ์„ ๋กœ๋“œํ•ด์˜ค๊ฒ ๋‹ค๋Š” ๊ตฌ๋ฌธ ์ž‘์„ฑ
	public MenuDAO() { 
		try {
			prop = new Properties();
			prop.loadFromXML(new FileInputStream("mapper/menu-query.xml"));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
โœ… '1. ์นดํ…Œ๊ณ ๋ฆฌ ์ „์ฒด ์กฐํšŒ'๋ฅผ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
	public List<CategoryDTO> selectAllcategory(Connection conn) {
		
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		
		List<CategoryDTO> categoryList = null; // ์ตœ์ข… ๋ฐ˜ํ™˜ํ•  ๋ฆฌ์ŠคํŠธ
		
		String query = prop.getProperty("selectAllCategory");
		
		try {
			pstmt = conn.prepareStatement(query);
			rset = pstmt.executeQuery();
			
            // while๋ฌธ ์ „์— ArrayList ๊ฐ์ฒด ์ƒ์„ฑ
			categoryList = new ArrayList<>(); 
			
			while(rset.next()) {
				CategoryDTO category = new CategoryDTO();
				category.setCategoryCode(rset.getInt("CATEGORY_CODE"));
				category.setCategoryName(rset.getString("CATEGORY_NAME"));
				category.setRefCategoryCode(rset.getInt("REF_CATEGORY_CODE"));
			
				categoryList.add(category); 
			}
	
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rset);
			close(pstmt);
			>>> Connection์€ Applicaiton ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœํ•˜๊ณ  ๋‹ค ์‚ฌ์šฉํ•œ ํ›„ ๋ฐ˜๋‚ฉํ•  ์˜ˆ์ •
		}	
		return categoryList;
	}
โœ… '2. ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก'์„ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
	public int insertNewMenu(Connection conn, MenuDTO newMenu) { 
    >>> ์—ฐ๊ฒฐ ๊ฐ์ฒด(conn)์™€ ์‚ฝ์ž…ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฉ”๋‰ด ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ๋ฉ”๋‰ด ๊ฐ์ฒด(newMenu)
		
		PreparedStatement pstmt = null;
		int result = 0;
		String query = prop.getProperty("insertMenu");
		
		try {
			pstmt = conn.prepareStatement(query);
            // ์ „๋‹ฌ ๋ฐ›์€ ์ธ์ž(newMenu)๋กœ DTO์™€ ์—ฐ๊ฒฐ
			pstmt.setString(1, newMenu.getMenuName()); 
			pstmt.setInt(2, newMenu.getMenuPrice());
			pstmt.setInt(3, newMenu.getCategoryCode());
			pstmt.setString(4, newMenu.getOrderableStatus());
			
			result = pstmt.executeUpdate(); // ์‹คํ–‰
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(pstmt);
		}
		
		return result;
	}
}

๐Ÿ‘‰ RUN

โ—ผ Application Class

>>> ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก ์ „ ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•ด์„œ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก์„ ์ง„ํ–‰ํ•˜๋Š” ํ๋ฆ„
		
MenuDAO menuDAO = new MenuDAO();
		
>>> ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹Œ Application์—์„œ Connection ๊ฐ์ฒด ์ƒ์„ฑ
Connection conn = getConnection();
โœ… 1. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํšŒ
>>> ์ด ์ˆ˜ํ–‰์ด ๋๋‚˜๊ณ  ๋‚˜์„œ ๋ฐ›์•„์•ผํ•˜๋Š” ๊ฐ’์ด List์ด๊ธฐ ๋•Œ๋ฌธ์— List ์‚ฌ์šฉ
List<CategoryDTO> categoryList = menuDAO.selectAllcategory(conn); 
   // ๋ฐ˜ํ™˜ ํƒ€์ž…                                            // ์ธ์ž
		
for(CategoryDTO category : categoryList) {
	System.out.println(category);
}
โœ… 2. ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก
Scanner sc = new Scanner(System.in);
System.out.print("๋ฉ”๋‰ด ์ด๋ฆ„ : ");
String menuName = sc.nextLine();
System.out.print("๋ฉ”๋‰ด ๊ฐ€๊ฒฉ : ");
int menuPrice = sc.nextInt();
System.out.print("์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ : ");
int categoryCode = sc.nextInt();
sc.nextLine();
System.out.print("ํŒ๋งค ์—ฌ๋ถ€(Y/N) : ");
String orderableStatus = sc.nextLine().toUpperCase();
		
MenuDTO newMenu = new MenuDTO();
newMenu.setMenuName(menuName);
newMenu.setMenuPrice(menuPrice);
newMenu.setCategoryCode(categoryCode);
newMenu.setOrderableStatus(orderableStatus);

// MenuDAO์˜ insertNewMenu ๋ฉ”์†Œ๋“œ๋กœ conn, newMenu์ธ์ž ์ „๋‹ฌ
int result = menuDAO.insertNewMenu(conn, newMenu); 
		
if(result > 0) {
	System.out.println("์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก์ด ์™„๋ฃŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค :) ");
} else {
	System.out.println("์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก์— ์‹คํŒจ ํ•˜์˜€์Šต๋‹ˆ๋‹ค :( ");
}
		
close(conn); 
>>> Connection์€ ์—ฌ๊ธฐ์„œ ๋‹ซ์•„์ฃผ๊ธฐ

๐Ÿ‘€ DAO๋ฅผ ํ™œ์šฉํ•œ Service < ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก ํ”„๋กœ๊ทธ๋žจ >

๐Ÿ“Œ ์ดˆ๊ธฐ Setting

โ—ผ Setting

โ—ผ menu-query.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<!-- '1. ์‹ ๊ทœ ์นดํ…Œ๊ณ ๋ฆฌ ๋“ฑ๋ก'์„ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="insertCategory">
		INSERT
			INTO TBL_CATEGORY
		(
			CATEGORY_CODE
		,	CATEGORY_NAME
		, 	REF_CATEGORY_CODE
		)
		VALUES
		(
			SEQ_CATEGORY_CODE.NEXTVAL <!-- ์‹œํ€€์Šค๋กœ CATEGORY_CODE ์ปฌ๋Ÿผ ๊ฐ’ ๊ณ„์† ์ƒ์„ฑ -->
		,	?
		,	?	
		)
	</entry>
	
	<!-- '2. ๋งˆ์ง€๋ง‰ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ ์กฐํšŒ'๋ฅผ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="getCurrentSequence">
		SELECT
				SEQ_CATEGORY_CODE.CURRVAL
			FROM DUAL
	</entry>
    
    <!-- '3. ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก'์„ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="insertMenu">
		INSERT
			INTO TBL_MENU
		(
			MENU_CODE
		,	MENU_NAME
		, 	MENU_PRICE
		,	CATEGORY_CODE
		,	ORDERABLE_STATUS
		)
		VALUES
		(
			SEQ_MENU_CODE.NEXTVAL <!-- ์‹œํ€€์Šค๋กœ MENU_CODE ์ปฌ๋Ÿผ ๊ฐ’ ๊ณ„์† ์ƒ์„ฑ -->
		,	?
		,	?
		,	?
		,	?		
		)
	</entry>
</properties>

โ—ผ JDBCTemplate (transaction ๊ตฌ๋ฌธ ์ถ”๊ฐ€)

public class JDBCTemplate { 
	
	/* Connection์„ returnํ•˜๋Š” ๋ฉ”์†Œ๋“œ */
	public static Connection getConnection() {
		
		Connection conn = null;
		Properties prop = new Properties();
		
		try {
			prop.load(new FileReader("config/jdbc-config.properties"));
			
			String driver = prop.getProperty("driver");
			String url = prop.getProperty("url");
			
			Class.forName(driver);
			
			conn = DriverManager.getConnection(url, prop);
			
			>>> auto commit์ด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋‚˜,
			>>> ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ์ž‘์—… ๋‹จ์œ„๋ณ„๋กœ commit๊ณผ rollback์„ ํŒ๋‹จํ•˜์—ฌ ์ˆ˜ํ–‰ํ•˜๊ณ ์žํ•˜๋ฏ€๋กœ
			>>> auto commit์„ false๋กœ ์ˆ˜๋™ ์„ค์ •
			conn.setAutoCommit(false);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		
		return conn;
		
	}
	
	/* Connection์„ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(Connection conn) {
		try {
			if(conn != null && !conn.isClosed()) {
				conn.close();
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* Statement๋ฅผ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(Statement stmt) {
		try {
			if(stmt != null && !stmt.isClosed()) {
				stmt.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* ResultSet์„ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(ResultSet rset) {
		try {
			if(rset != null && !rset.isClosed()) {
				rset.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* ---------์ž๋™ commit์„ ์ˆ˜๋™ commit์œผ๋กœ ๋ณ€๊ฒฝ--------- */
	
	/* ๋ชจ๋‘ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋ฉด, commitํ•  ๋ฉ”์†Œ๋“œ */
	public static void commit(Connection conn) {
		try {
			if(conn != null && !conn.isClosed()) { // :conn ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, commmit
				conn.commit();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* ํ•˜๋‚˜๋ผ๋„ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, rollbackํ•  ๋ฉ”์†Œ๋“œ */
	public static void rollback(Connection conn) {
		try {
			if(conn != null && !conn.isClosed()) { // :conn ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, rollback
				conn.rollback();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

๐Ÿ‘‰ DTO

โ—ผ CategoryDTO Class

public class CategoryDTO {

	/*
	CATEGORY_CODE	NUMBER
	CATEGORY_NAME	VARCHAR2(30 BYTE)
	REF_CATEGORY_CODE	NUMBER
	*/

	/* ํ•„๋“œ */
	private int categoryCode;
	private String categoryName;
	private Integer refCategoryCode;
    >>> refCategoryCode์— null๊ฐ’์„ ๋‹ด๊ธฐ ์œ„ํ•ด int๊ฐ€ ์•„๋‹Œ Integer๋กœ ์„ค์ • !!
	
	/* ์ƒ์„ฑ์ž */
	public CategoryDTO() {}
	
	public CategoryDTO(int categoryCode, String categoryName, Integer refCategoryCode) {
		super();
		this.categoryCode = categoryCode;
		this.categoryName = categoryName;
		this.refCategoryCode = refCategoryCode;
	}

	/* getter & setter */
	public int getCategoryCode() {
		return categoryCode;
	}

	public void setCategoryCode(int categoryCode) {
		this.categoryCode = categoryCode;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public Integer getRefCategoryCode() {
		return refCategoryCode;
	}

	public void setRefCategoryCode(Integer refCategoryCode) {
		this.refCategoryCode = refCategoryCode;
	}

	/* toString */
	@Override
	public String toString() {
		return "CategoryDTO [categoryCode=" + categoryCode + ", categoryName=" + categoryName + ", refCategoryCode="
				+ refCategoryCode + "]";
	}
}

โ—ผ MenuDTO Class

* 'DAO์˜ ์‚ฌ์šฉ'์˜ MenuDTO Class์™€ ๋™์ผ *

๐Ÿ‘‰ DAO

โ—ผ MenuDAO Class

public class MenuDAO {

	>>> ๋ฉ”์†Œ๋“œ ์•ˆ์ด ์•„๋‹Œ, ํ•„๋“œ์— Properties ์„ ์–ธ 
    >>> (๋ชจ๋“  ๋ฉ”์†Œ๋“œ์—์„œ xml ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ)
    /* ํ•„๋“œ */
	private Properties prop; 
	
	/* ๊ธฐ๋ณธ ์ƒ์„ฑ์ž */ >>> ํ•„๋“œ์— ๋Œ€ํ•ด ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ๊ฐ์ฒด์ชฝ์œผ๋กœ xml ํŒŒ์ผ์„ ๋กœ๋“œํ•ด์˜ค๊ฒ ๋‹ค๋Š” ๊ตฌ๋ฌธ ์ž‘์„ฑ
	public MenuDAO() { 
		try {
			prop = new Properties();
			prop.loadFromXML(new FileInputStream("mapper/menu-query.xml"));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
โœ… '1. ์‹ ๊ทœ ์นดํ…Œ๊ณ ๋ฆฌ ๋“ฑ๋ก'์„ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
	public int insertNewCategory(Connection conn, CategoryDTO newCategory) {
		
		PreparedStatement pstmt = null;
		int result = 0;
		String query = prop.getProperty("insertCategory");
		
		try {
			pstmt = conn.prepareStatement(query);
			pstmt.setString(1, newCategory.getCategoryName()); // CATEGORY_NAME
			pstmt.setObject(2, newCategory.getRefCategoryCode()); // REF_CATEGORY_CODE 
			>>> RefCategoryCode๋Š” int๊ฐ€ ์•„๋‹Œ Integer๋‹ˆ setObject ์‚ฌ์šฉ !!

			result = pstmt.executeUpdate(); // ์‹คํ–‰
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(pstmt);
		}
		
		return result;
	}
โœ… '2. ๋งˆ์ง€๋ง‰ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ ์กฐํšŒ'๋ฅผ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
	public int selectLastCategoryCode(Connection conn) {

		PreparedStatement pstmt = null;
		ResultSet rset = null;
		int newCategoryCode = 0;
		String query = prop.getProperty("getCurrentSequence");
		
		try {
			pstmt = conn.prepareStatement(query);
			rset = pstmt.executeQuery();
			
			if(rset.next()) {
				newCategoryCode = rset.getInt("CURRVAL"); 
                >>> CURRVAL์„ int๊ฐ’์œผ๋กœ ๋ฝ‘์•„ ์ƒˆ๋กœ์šด ์นดํ…Œ๊ณ ๋ฆฌ ๊ฐ’์ด ๋ญ”์ง€ ์ „๋‹ฌ
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rset);
			close(pstmt);
		}
	
		return newCategoryCode;	>>> ๊ทธ๋ฆฌ๊ณ  ๋ฆฌํ„ด
	}
โœ… '3. ์‹ ๊ทœ ๋ฉ”๋‰ด ๋“ฑ๋ก'์„ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
	public int insertNewMenu(Connection conn, MenuDTO newMen u) { 
    >>> ์—ฐ๊ฒฐ ๊ฐ์ฒด(conn)์™€ ์‚ฝ์ž…ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฉ”๋‰ด ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ๋ฉ”๋‰ด ๊ฐ์ฒด(newMenu)
		
		PreparedStatement pstmt = null;
		int result = 0;
		String query = prop.getProperty("insertMenu");
		
		try {
			pstmt = conn.prepareStatement(query);
            // ์ „๋‹ฌ ๋ฐ›์€ ์ธ์ž(newMenu)๋กœ DTO์™€ ์—ฐ๊ฒฐ
			pstmt.setString(1, newMenu.getMenuName());
			pstmt.setInt(2, newMenu.getMenuPrice());
			pstmt.setInt(3, newMenu.getCategoryCode());
			pstmt.setString(4, newMenu.getOrderableStatus());
			
			result = pstmt.executeUpdate(); // ์‹คํ–‰
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(pstmt);
		}
		return result;
	}
}

๐Ÿ‘‰ Transaction

โ—ผ Application Class

Connection conn = getConnection();
		
/* COMMIT ์ƒํƒœ ํ™•์ธ */
try {
	System.out.println("AutoCommit์˜ ํ˜„์žฌ ์„ค์ • ๊ฐ’ : " + conn.getAutoCommit()); 
    // true ์ถœ๋ ฅ (Insertํ•  ๋•Œ๋งˆ๋‹ค, ์ž๋™์œผ๋กœ Commit๋˜๋Š” ์ค‘์ด๋ผ๋Š” ์˜๋ฏธ)
	// JDBCTemplate์—์„œ ์ž๋™์ปค๋ฐ‹ ํ•ด์ œ ๊ตฌ๋ฌธ ์ž‘์„ฑํ•œ ๋’ค, ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ฉด ์ถœ๋ ฅ๋ฌธ์ด false๋กœ ๋ฐ”๋€œ
			
} catch (SQLException e1) {
	e1.printStackTrace();
}
		
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
		
int result1 = 0;
int result2 = 0;

Properties prop = new Properties();

try {
	prop.loadFromXML(new FileInputStream("mapper/menu-query.xml"));
	String query1 = prop.getProperty("insertCategory");
	String query2 = prop.getProperty("insertMenu");
			
	/* TBL_CATEGORY ํ…Œ์ด๋ธ”์— 1ํ–‰ ์‚ฝ์ž… */
	pstmt1 = conn.prepareStatement(query1);
	pstmt1.setString(1, "ํ…Œ์ŠคํŠธ์นดํ…Œ๊ณ ๋ฆฌ");
	pstmt1.setInt(2, 1);
			
	result1= pstmt1.executeUpdate();
			
	/* TBL_MENU ํ…Œ์ด๋ธ”์— 1ํ–‰ ์‚ฝ์ž… */
	pstmt2 = conn.prepareStatement(query2);
	pstmt2.setString(1, "ํ…Œ์ŠคํŠธ๋ฉ”๋‰ด");
	pstmt2.setInt(2, 10000);
    
	>>> TBL_CATEGORY์— ์กด์žฌํ•˜์ง€ ์•Š์€ CATEGORY_CODE๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋ฉด
	>>> ๋ถ€๋ชจํ‚ค๋ฅผ ์ฐพ์ง€ ๋ชป ํ•˜๋Š” ์™ธ๋ž˜ํ‚ค ์ œ์•ฝ์กฐ๊ฑด ์œ„๋ฐ˜ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
	pstmt2.setInt(3, 15); // ์ผ๋ถ€๋Ÿฌ ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œํ‚ด 
    					  >>> ์œ„์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋Š” ์‚ฝ์ž…๋˜๊ณ  ์˜ค๋ฅ˜์žˆ๋Š” ๋ฉ”๋‰ด๋งŒ ์‚ฝ์ž…์ด ์•ˆ๋จ 
                          >>> (์›๋ž˜๋Š” ํ•˜๋‚˜๋ผ๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ ์‹œ, ์•„์˜ˆ ์‹คํ–‰์ด ๋˜๋ฉด ์•ˆ๋จ)
	pstmt2.setString(4, "Y");
			
	result2 = pstmt2.executeUpdate();
			
	System.out.println("result1 : " + result1); // 1 ์ถœ๋ ฅ (์ •์ƒ ์‚ฝ์ž…๋˜์—ˆ๋‹ค๋Š” ์˜๋ฏธ)
	System.out.println("result2 : " + result2); // 1 ์ถœ๋ ฅ
			
} catch (IOException e) {
	e.printStackTrace();
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	close(pstmt1);
	close(pstmt2);
โœ… Transaction ์ˆ˜ํ–‰ ๐Ÿ”ฅ์ค‘์š”๐Ÿ”ฅ
>>> ํŠธ๋žœ์žญ์…˜(๋…ผ๋ฆฌ์ ์ธ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ ๋‹จ์œ„) ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด 2๊ฐœ์˜ insert๊ฐ€ ๋ชจ๋‘ ์ž˜ ๋™์ž‘ํ–ˆ๋Š”์ง€ ํŒ๋‹จํ•˜์—ฌ 
>>> ์ž˜ ๋™์ž‘ํ–ˆ์„๊ฒฝ์šฐ commit, ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ rollback์„ ์ˆ˜ํ–‰
			
>>> ์ด๊ณณ์— ์ง์ ‘ conn.commit๊ณผ conn.rollback์„ ์ž…๋ ฅํ•ด๋„ ๋˜์ง€๋งŒ 
>>> ๋‘˜ ๋‹ค try-catch ๋ธ”๋Ÿญ์„ ์‚ฌ์šฉํ•˜์—ฌ Exception์„ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผํ•˜๋ฏ€๋กœ,
>>> ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ ๋˜ํ•œ ๋”ฐ๋กœ JDBCTemplate์— ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ

	if(result1 > 0 && result2 > 0) { // : ๋‘˜ ๋‹ค ๋ชจ๋‘ ์ž˜ ์ž‘๋™ํ–ˆ๋‹ค๋ฉด,
		commit(conn); // commit
	} else { // ํ•˜๋‚˜๋ผ๋„ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด,
		rollback(conn); // rollbackํ•˜์—ฌ ์•„๋ฌด๊ฒƒ๋„ ์‚ฝ์ž… X
	}
			
	close(conn);
}

๐Ÿ‘‰ Service

๐Ÿ“Œ Ref. Service์˜ ์—ญํ• 

1. Connection ์ƒ์„ฑ
2. DAO์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ (๋…ผ๋ฆฌ์ ์ธ ๊ธฐ๋Šฅ ๋‹จ์œ„๋กœ ๋ฌถ์ธ ๋ฉ”์†Œ๋“œ๋“ค์„ ๋ชจ๋‘ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ ํ•  ์ˆ˜๋„ ์žˆ์Œ) 
3. ํŠธ๋žœ์žญ์…˜ ์ œ์–ด(commit, rollback)
4. Connection ์ข…๋ฃŒ

โ—ผ MenuService Class

public class MenuService {

	public void registNewMenu() {
		
		/* 1. Connection ์ƒ์„ฑ */
		Connection conn = getConnection();
		
		/* 2. DAO ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ */
		MenuDAO menuDAO = new MenuDAO();
		
		/* 2-1. ์‹ ๊ทœ ์นดํ…Œ๊ณ ๋ฆฌ ๋“ฑ๋ก */
		CategoryDTO newCategory = new CategoryDTO();
		newCategory.setCategoryName("๊ธฐํƒ€");
		newCategory.setRefCategoryCode(null); 
        >>> ํ…Œ์ด๋ธ”์— null๊ฐ’์„ ๋„ฃ๊ณ  ์‹ถ๋‹ค๊ณ ํ•ด์„œ null์„ ๋„ฃ์œผ๋ฉด ์˜ค๋ฅ˜ 
		>>> ํ•„๋“œ์— ์„ ์–ธํ–ˆ๋˜ intํ˜•์„ ๊ฐ’์œผ๋กœ ๋‹ด์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ, 
        >>> ๋”ฐ๋ผ์„œ Integer๋กœ ์ž๋ฃŒํ˜• ๋ฐ”๊ฟ”์ฃผ๊ธฐ
		
		int result1 = menuDAO.insertNewCategory(conn, newCategory); 
        // ์ •์ƒ ์ˆ˜ํ–‰๋˜์—ˆ์„ ๋•Œ, ํ•œ ๊ฐœ์˜ ํ–‰์ด ์ž˜ ์‚ฝ์ž…๋˜์—ˆ๋‹ค๋Š” ์ถœ๋ ฅ
		
		/* 2-2. ์‹ ๊ทœ ์นดํ…Œ๊ณ ๋ฆฌ ๋“ฑ๋ก ์‹œ ๋ฐœ์ƒํ•œ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ ๋ฒˆํ˜ธ ์กฐํšŒ */
		int newCategoryCode = menuDAO.selectLastCategoryCode(conn);
		
		/* 2-3. ๋“ฑ๋ก ๋œ ์‹ ๊ทœ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋ฉ”๋‰ด ๋“ฑ๋ก */
		MenuDTO newMenu = new MenuDTO();
		newMenu.setMenuName("๋ฉ”๋กฑ๋ฉ”๋กฑ์ŠคํŠœ");
		newMenu.setMenuPrice(40000);
		newMenu.setCategoryCode(newCategoryCode);
		newMenu.setOrderableStatus("Y");
		
		int result2 = menuDAO.insertNewMenu(conn, newMenu);
		
		/* 3. ํŠธ๋žœ์žญ์…˜ ์ œ์–ด */
		if(result1 > 0 && result2 > 0) {
			System.out.println("์‹ ๊ทœ ์นดํ…Œ๊ณ ๋ฆฌ์™€ ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์‹ ๊ทœ ๋ฉ”๋‰ด๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค :) ");
			commit(conn);
		} else {
			System.out.println("์‹ ๊ทœ ์นดํ…Œ๊ณ ๋ฆฌ์™€ ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์‹ ๊ทœ ๋ฉ”๋‰ด๊ฐ€ ์ถ”๊ฐ€์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค :( ");
			rollback(conn);
		}
		
		/* 4. Connection ์ข…๋ฃŒ */
		close(conn);
	}
}

๐Ÿ‘‰ Run

โ—ผ Application Class

public class Application {

	public static void main(String[] args) {
		
		new MenuService().registNewMenu();
	}
}

๐Ÿ‘€ DAO๋ฅผ ํ™œ์šฉํ•œ View < ์Œ์‹ ์ฃผ๋ฌธ ํ”„๋กœ๊ทธ๋žจ >

๐Ÿ“Œ ์ดˆ๊ธฐ Setting

โ—ผ Setting

โ—ผ order-query.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<!-- '1. ์นดํ…Œ๊ณ ๋ฆฌ ์ „์ฒด ์กฐํšŒ'๋ฅผ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="selectAllCategory">
		SELECT
				CATEGORY_CODE
			,	CATEGORY_NAME
			,	REF_CATEGORY_CODE
			FROM TBL_CATEGORY
	</entry>

	<!-- '2. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก์—์„œ ๋ฉ”๋‰ด ๊ณ ๋ฅด๊ธฐ'๋ฅผ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="selectMenuByCategory">
		SELECT
				MENU_CODE
			,	MENU_NAME
			,	MENU_PRICE
			,	CATEGORY_CODE
			,	ORDERABLE_STATUS
			FROM TBL_MENU
			WHERE ORDERABLE_STATUS = 'Y'
			AND CATEGORY_CODE = ? <!-- ์ž…๋ ฅ๋œ ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ์˜ ๋ฉ”๋‰ด๋“ค๋งŒ ์กฐํšŒํ•˜๋Š” ์กฐ๊ฑด -->
	</entry>
	
	<!-- '3. ์ฃผ๋ฌธ ์ •๋ณด ๋„ฃ๊ธฐ'๋ฅผ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="insertOrder">
		INSERT
			INTO TBL_ORDER
		(
			ORDER_CODE
		,	ORDER_DATE
		,	ORDER_TIME
		,	TOTAL_ORDER_PRICE
		)
		VALUES
		(
			SEQ_ORDER_CODE.NEXTVAL
		,	?
		,	?
		,	?
		)
	</entry>

	<!-- '4. ๊ณ ๋ฅธ ๋ฉ”๋‰ด์˜ ์ •๋ณด ๋„ฃ๊ธฐ'๋ฅผ ์œ„ํ•œ ์ฟผ๋ฆฌ -->
	<entry key="insertOrderMenu">
		INSERT
			INTO TBL_ORDER_MENU
		(
			ORDER_CODE
		,	MENU_CODE
		,	ORDER_AMOUNT
		)
		VALUES
		(
			SEQ_ORDER_CODE.CURRVAL <!-- ๋ฐœ์ƒ์‹œ์ผฐ๋˜ ๋„ฅ์ŠคํŠธ๋ฐธ๋ฅ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ -->
		,	?
		,	?
		)
	</entry>
</properties>

โ—ผ JDBCTemplate (transaction ๊ตฌ๋ฌธ ์ถ”๊ฐ€)

* DAO๋ฅผ ํ™œ์šฉํ•œ Service์˜ JDBCTemplate๊ณผ ๋™์ผ *

๐Ÿ‘‰ DTO

โ—ผ CategoryDTO Class

* 'DAO๋ฅผ ํ™œ์šฉํ•œ Service'์˜ CategoryDTO Class์™€ ๋™์ผ *

โ—ผ MenuDTO Class

* 'DAO์˜ ์‚ฌ์šฉ'์˜ MenuDTO Class์™€ ๋™์ผ *

โ—ผ OrderDTO Class

public class OrderDTO {

	/*
	ORDER_CODE	NUMBER
	ORDER_DATE	VARCHAR2(8 BYTE)
	ORDER_TIME	VARCHAR2(8 BYTE)
	TOTAL_ORDER_PRICE	NUMBER
	*/
	
	private int orderCode;
	private String orderDate;
	private String orderTime;
	private int totalOrderPrice;
	private List<OrderMenuDTO> orderMenuList; 
    >>> ์˜ค๋” ๋ฉ”๋‰ด๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ List๋ฅผ orderDTO์—์„œ๋„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•„๋“œ์— ์ž‘์„ฑ

	public OrderDTO() {}

	public OrderDTO(int orderCode, String orderDate, String orderTime, int totalOrderPrice,
			List<OrderMenuDTO> orderMenuList) {
		super();
		this.orderCode = orderCode;
		this.orderDate = orderDate;
		this.orderTime = orderTime;
		this.totalOrderPrice = totalOrderPrice;
		this.orderMenuList = orderMenuList;
	}

	public int getOrderCode() {
		return orderCode;
	}

	public void setOrderCode(int orderCode) {
		this.orderCode = orderCode;
	}

	public String getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}

	public String getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(String orderTime) {
		this.orderTime = orderTime;
	}

	public int getTotalOrderPrice() {
		return totalOrderPrice;
	}

	public void setTotalOrderPrice(int totalOrderPrice) {
		this.totalOrderPrice = totalOrderPrice;
	}

	public List<OrderMenuDTO> getOrderMenuList() {
		return orderMenuList;
	}

	public void setOrderMenuList(List<OrderMenuDTO> orderMenuList) {
		this.orderMenuList = orderMenuList;
	}

	@Override
	public String toString() {
		return "OrderDTO [orderCode=" + orderCode + ", orderDate=" + orderDate + ", orderTime=" + orderTime
				+ ", totalOrderPrice=" + totalOrderPrice + ", orderMenuList=" + orderMenuList + "]";
	}
}

โ—ผ OrderMenuDTO Class

public class OrderMenuDTO {

	private int orderCode;
	private int menuCode;
	private int orderAmount;
	
	public OrderMenuDTO() {}
	
	public OrderMenuDTO(int orderCode, int menuCode, int orderAmount) {
		super();
		this.orderCode = orderCode;
		this.menuCode = menuCode;
		this.orderAmount = orderAmount;
	}

	public int getOrderCode() {
		return orderCode;
	}

	public void setOrderCode(int orderCode) {
		this.orderCode = orderCode;
	}

	public int getMenuCode() {
		return menuCode;
	}

	public void setMenuCode(int menuCode) {
		this.menuCode = menuCode;
	}

	public int getOrderAmount() {
		return orderAmount;
	}

	public void setOrderAmount(int orderAmount) {
		this.orderAmount = orderAmount;
	}

	@Override
	public String toString() {
		return "OrderMenuDTO [orderCode=" + orderCode + ", menuCode=" + menuCode + ", orderAmount=" + orderAmount + "]";
	}
}

๐Ÿ‘‰ DAO

โ—ผ OrderDAO Class

public class OrderDAO {

		>>> ๋ฉ”์†Œ๋“œ ์•ˆ์ด ์•„๋‹Œ, ํ•„๋“œ์— Properties ์„ ์–ธ 
   		>>> (๋ชจ๋“  ๋ฉ”์†Œ๋“œ์—์„œ xml ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ)
    	/* ํ•„๋“œ */
		private Properties prop; 
		
		/* ๊ธฐ๋ณธ ์ƒ์„ฑ์ž */ >>> ํ•„๋“œ์— ๋Œ€ํ•ด ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ๊ฐ์ฒด์ชฝ์œผ๋กœ xml ํŒŒ์ผ์„ ๋กœ๋“œํ•ด์˜ค๊ฒ ๋‹ค๋Š” ๊ตฌ๋ฌธ ์ž‘์„ฑ
		public OrderDAO() { 
			try {
				prop = new Properties();
				prop.loadFromXML(new FileInputStream("mapper/order-query.xml"));
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
โœ… '1. ์นดํ…Œ๊ณ ๋ฆฌ ์ „์ฒด ์กฐํšŒ'๋ฅผ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
public List<CategoryDTO> selectAllCategory(Connection conn) {
			
	PreparedStatement pstmt = null;
	ResultSet rset = null;
			
	List<CategoryDTO> categoryList = null; // ์ตœ์ข… ๋ฐ˜ํ™˜ํ•  ๋ฆฌ์ŠคํŠธ
			
	String query = prop.getProperty("selectAllCategory");
			
	try {
		pstmt = conn.prepareStatement(query);
		rset = pstmt.executeQuery();
				
        // while๋ฌธ ์ „์— ArrayList ๊ฐ์ฒด ์ƒ์„ฑ
		categoryList = new ArrayList<>();
				
		while(rset.next()) {
			CategoryDTO category = new CategoryDTO();
			category.setCategoryCode(rset.getInt("CATEGORY_CODE"));
			category.setCategoryName(rset.getString("CATEGORY_NAME"));
			category.setRefCategoryCode(rset.getInt("REF_CATEGORY_CODE"));
				
			categoryList.add(category); 
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		close(rset);
		close(pstmt);
		>>> Connection์€ Applicaiton ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœํ•˜๊ณ  ๋‹ค ์‚ฌ์šฉํ•œ ํ›„ ๋ฐ˜๋‚ฉํ•  ์˜ˆ์ •
	}	
	return categoryList; 
    >>> ๋ฐ˜ํ™˜๊ฐ’ ๋ˆ„๋ฝํ•˜์ง€์•Š๊ฒŒ ์กฐ์‹ฌ !
}
โœ… '2. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก์—์„œ ๋ฉ”๋‰ด ๊ณ ๋ฅด๊ธฐ'๋ฅผ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
public List<MenuDTO> selectMenuByCategory(Connection conn, int categoryCode) {

	PreparedStatement pstmt = null;
	ResultSet rset = null;
	List<MenuDTO> menuList = null; // ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ
	String query = prop.getProperty("selectMenuByCategory"); // ํ‚ค๊ฐ’ ๋„ฃ๊ธฐ
			
	try {
		pstmt = conn.prepareStatement(query); // ์ฟผ๋ฆฌ๋ฌธ ์ค€๋น„
		pstmt.setInt(1, categoryCode);
				
		rset = pstmt.executeQuery();
				
		menuList = new ArrayList<>();
				
		while(rset.next()) {
					
			MenuDTO menu = new MenuDTO();
			menu.setMenuCode(rset.getInt("MENU_CODE"));
			menu.setMenuName(rset.getString("MENU_NAME"));
			menu.setMenuPrice(rset.getInt("MENU_PRICE"));
			menu.setCategoryCode(rset.getInt("CATEGORY_CODE"));
			menu.setOrderableStatus(rset.getString("ORDERABLE_STATUS"));
					
			menuList.add(menu);
		}
				
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		close(rset);
		close(pstmt);
	}
			
	return menuList;
    >>> ๋ฐ˜ํ™˜๊ฐ’ ๋ˆ„๋ฝํ•˜์ง€์•Š๊ฒŒ ์กฐ์‹ฌ !
}
โœ… '3. ์ฃผ๋ฌธ ์ •๋ณด ๋„ฃ๊ธฐ'๋ฅผ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
public int insertOrder(Connection conn, OrderDTO order) {
			
	PreparedStatement pstmt = null;
	int result = 0;
	String query = prop.getProperty("insertOrder");
			
	try {
		pstmt = conn.prepareStatement(query);
		pstmt.setString(1, order.getOrderDate());
		pstmt.setString(2, order.getOrderTime());
		pstmt.setInt(3, order.getTotalOrderPrice());
				
		result = pstmt.executeUpdate();
						
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		close(pstmt);
	}
			
	return result;
}
โœ… '4. ๊ณ ๋ฅธ ๋ฉ”๋‰ด์˜ ์ •๋ณด ๋„ฃ๊ธฐ'๋ฅผ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
public int insertOrderMenu(Connection conn, OrderMenuDTO orderMenu) {
			
	PreparedStatement pstmt = null;
	int result = 0;
	String query = prop.getProperty("insertOrderMenu");
			
	try {
		pstmt = conn.prepareStatement(query);
		pstmt.setInt(1, orderMenu.getMenuCode());
		pstmt.setInt(2, orderMenu.getOrderAmount());
				
		result = pstmt.executeUpdate();
						
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		close(pstmt);
	}
			
	return result;
}

๐Ÿ‘‰ Service

โ—ผ OrderService Class

public class OrderService {
	
    // OrderDAO ๊ฐ์ฒด ์ƒ์„ฑ
	private OrderDAO orderDAO = new OrderDAO(); 
โœ… 1. ์นดํ…Œ๊ณ ๋ฆฌ ์ „์ฒด ์กฐํšŒ
	public List<CategoryDTO> selectAllcategory() {
		
		/* 1. Connection ์ƒ์„ฑ */
		Connection conn = getConnection();
		
		/* 2. DAO์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ฆฌํ„ด ๋ฐ›๊ธฐ */
		List<CategoryDTO> categoryList = orderDAO.selectAllCategory(conn);
		
		/* 3. ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ (select์˜ ๊ฒฝ์šฐ commit, rollback์ด ํ•„์š”์—†์œผ๋ฏ€๋กœ ์ƒ๋žต) */
		/* 4. Connection ๋ฐ˜๋‚ฉ */
		close(conn);
		
		return categoryList; // ์ตœ์ข… ๋ฐ˜ํ™˜
	}
โœ… 2. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก์—์„œ ๋ฉ”๋‰ด ๊ณ ๋ฅด๊ธฐ
	public List<MenuDTO> selectMenuByCategory(int categoryCode) {
		
		/* 1. Connection ์ƒ์„ฑ */
		Connection conn = getConnection();
		
		/* 2. DAO์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ฆฌํ„ด ๋ฐ›๊ธฐ */
		List<MenuDTO> menuList = orderDAO.selectMenuByCategory(conn, categoryCode);
		
		/* 3. ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ (select์˜ ๊ฒฝ์šฐ commit, rollback์ด ํ•„์š”์—†์œผ๋ฏ€๋กœ ์ƒ๋žต) */
		/* 4. Connection ๋ฐ˜๋‚ฉ */
		close(conn);
		
		return menuList;
	}
โœ… 3. ์ฃผ๋ฌธ ์ •๋ณด ๋„ฃ๊ธฐ / 4. ๊ณ ๋ฅธ ๋ฉ”๋‰ด์˜ ์ •๋ณด ๋„ฃ๊ธฐ
	public int registOrder(OrderDTO order) {
		
		/* 1. Connection ์ƒ์„ฑ */
		Connection conn = getConnection();
		
		/* 2. DAO์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ฆฌํ„ด ๋ฐ›๊ธฐ */
		/* 2-1. ORDER TABLE INSERT */
		int orderResult = orderDAO.insertOrder(conn, order);
		
		/* 2-2. ORDER MENU TABLE INSERT */
		List<OrderMenuDTO> orderMenuList = order.getOrderMenuList();
		int orderMenuResult = 0;
		for(OrderMenuDTO orderMenu : orderMenuList) {
			orderMenuResult += orderDAO.insertOrderMenu(conn, orderMenu);
		}
		
		int result = 0;
        >>> ์ „์ฒด ํ”„๋กœ์„ธ์Šค ์„ฑ๊ณต, ์‹คํŒจ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜
		
		/* 3. ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ */
		if(orderResult > 0 && orderMenuResult == orderMenuList.size()) { // : ์ฃผ๋ฌธ ๊ฐฏ์ˆ˜์™€ ๊ฐ™๋‹ˆ?
			commit(conn);
			result = 1; // : ๊ฐ™์œผ๋ฉด result๋Š” 1์ด ๋˜์–ด ๋ฐ˜ํ™˜ (์„ฑ๊ณต)
		} else {
			rollback(conn);
		}
		/* 4. Connection ๋ฐ˜๋‚ฉ */
		close(conn);
		
		return result; 
		>>> orderResult ์™€ orderMenuResult ๋‘˜ ์ค‘์— ํ•˜๋‚˜๋ฅผ ์“ฐ๊ธฐ์—” 
        >>> ๋‹ค๋ฅธ ํ•˜๋‚˜๊ฐ€ ์ž˜๋ชป๋œ ์ƒํƒœ๋กœ ๋ฐ˜ํ™˜์ด ๋˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— 
        >>> ์ƒˆ๋กœ์šด result๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋„ฃ์–ด์คŒ
		// ์„ฑ๊ณต์‹œ์—๋Š” 1, ์‹คํŒจ์‹œ์—๋Š” 0 ๋ฐ˜ํ™˜
	}
}

๐Ÿ‘‰ View

โ—ผ OrderMenu Class

public class OrderMenu {
	
    // OrderService ํด๋ž˜์Šค๋ฅผ ํ•„๋“œ์ชฝ์— ์„ ์–ธํ•˜๊ณ  ๊ฐ์ฒด๊นŒ์ง€ ์ƒ์„ฑ
	private OrderService orderService = new OrderService(); 
   
	public void displayMenu() {
		
		/* ๋ฐ˜๋ณต---------------------------
		 * 1. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํšŒ -> ์„ ํƒ
		 * 2. ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ฉ”๋‰ด ์กฐํšŒ -> ์„ ํƒ
		 * 3. ๋ฉ”๋‰ด ์„ ํƒ ํ›„ ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰ ์ž…๋ ฅ 
		 * -------------------------------
		 * 4. ์ฃผ๋ฌธ
		 * */
         
		Scanner sc = new Scanner(System.in);
		
		List<OrderMenuDTO> orderMenuList = new ArrayList<>(); 
        >>>  ์ฃผ๋ฌธ์„ ์—ฌ๋Ÿฌ ๊ฐœ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ
        
		int totalOrderPrice = 0; 
		
		do {
			/* 1. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํšŒ -> ์„ ํƒ */
			System.out.println("====================== ์Œ์‹ ์ฃผ๋ฌธ ํ”„๋กœ๊ทธ๋žจ ======================");
			List<CategoryDTO> categoryList = orderService.selectAllcategory(); // '์ด๋Ÿฐ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•„์š”ํ•˜๊ฒ ๋‹ค' ๋ผ๊ณ  ์˜ˆ์ƒํ•ด์„œ ์ž‘์„ฑํ•œ ๊ฒƒ
			for(CategoryDTO category : categoryList) {
				System.out.println(category.getCategoryName());
			}
			
			System.out.println("===========================================================");
			System.out.print("์ฃผ๋ฌธํ•˜์‹ค ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š” : ");
			String inputCategoryName = sc.nextLine();
			
			// ์นดํ…Œ์ฝ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ์•Œ์•„์˜ค๋Š” ๋ฐ˜๋ณต๋ฌธ
			int categoryCode = 0;
            
            >>> ์นดํ…Œ๊ณ ๋ฆฌ๋ช…์„ ํ•˜๋‚˜์”ฉ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€์ง€ ํ™•์ธ 
			for(CategoryDTO category : categoryList) {
				if(category.getCategoryName().equals(inputCategoryName)) { 
                
					categoryCode = category.getCategoryCode();
				}
			}
            
            >>> ์นดํ…Œ๊ณ ๋ฆฌ๋ช…์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์ž˜ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธ
//			System.out.println(categoryCode); 

			/* 2. ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ฉ”๋‰ด ์กฐํšŒ -> ์„ ํƒ */
			System.out.println("===================== " + inputCategoryName + " ์ฃผ๋ฌธ ๊ฐ€๋Šฅ ๋ฉ”๋‰ด =====================");
			List<MenuDTO> menuList = orderService.selectMenuByCategory(categoryCode); 
            >>> ์—ฌ๊ธฐ์„œ selectMenuByCategory ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ
			
			for(MenuDTO menu : menuList) { // menuList ๊ฐ’ ๋ฝ‘์•„์˜ค๊ธฐ
				System.out.println(menu);
			}
			
			/* 3. ๋ฉ”๋‰ด ์„ ํƒ ํ›„ ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰ ์ž…๋ ฅ ๋ฐ 4. ์ฃผ๋ฌธํ•˜๊ธฐ */
			System.out.print("์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š” : ");
			String inputMenu = sc.nextLine();
			System.out.print("์ฃผ๋ฌธํ•˜์‹ค ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
			int orderAmount = sc.nextInt();
			sc.nextLine();
			
			int menuCode = 0;
			int menuPrice = 0;
			for(MenuDTO menu : menuList) {
				if(menu.getMenuName().equals(inputMenu)) {	// : ๋ฉ”๋‰ด ์ด๋ฆ„์ด ์ž…๋ ฅ๋ฐ›์€ ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค๋ฉด,
					menuCode = menu.getMenuCode();			// menuCode ๋ณ€์ˆ˜์— ์ €์žฅ 
					menuPrice = menu.getMenuPrice();		// menuPrice ๋ณ€์ˆ˜์— ์ €์žฅ
				}
			}
			
			OrderMenuDTO orderMenu = new OrderMenuDTO();
			orderMenu.setMenuCode(menuCode);
			orderMenu.setOrderAmount(orderAmount);
//			orderMenu.setOrderCode();
			>>> OrderCode๋Š” ์‹œํ€€์Šค๋ผ ์•„์ง ์ž…๋ ฅ ํ•  ์ˆ˜ ์—†์Œ
			
			orderMenuList.add(orderMenu); // orderMenuList์— ์ฃผ๋ฌธํ•œ๊ฑฐ ์ถ”๊ฐ€๋จ
			totalOrderPrice += (menuPrice * orderAmount); // ์ด ์•ก์ˆ˜ ๊ณ„์‚ฐ๋จ
			
			System.out.print("๊ณ„์† ์ฃผ๋ฌธํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (์˜ˆ/์•„๋‹ˆ์˜ค) : ");
			boolean isContinue = sc.nextLine().equals("์˜ˆ");
			
			if(!isContinue) break;
			
		} while(true);
		
		for(OrderMenuDTO orderMenu : orderMenuList) {
			System.out.println("์ฃผ๋ฌธ ๋ฉ”๋‰ด : " + orderMenu);
			
		}
		System.out.println("์ฃผ๋ฌธ ์ด์•ก : " + totalOrderPrice);
		
		/* ์˜์ˆ˜์ฆ */
		java.util.Date orderTime = new java.util.Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd"); 
		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
		String date = dateFormat.format(orderTime);
		String time = timeFormat.format(orderTime);
        >>> SimpleDateFormat : ์ƒ์„ฑ์ž์ชฝ์œผ๋กœ ์ „๋‹ฌ๋œ ์ธ์ž์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด ํฌ๋งท์œผ๋กœ ๋งŒ๋“ค์–ด์คŒ
        
		OrderDTO order = new OrderDTO();
		order.setOrderDate(date);
		order.setOrderTime(time);
		order.setTotalOrderPrice(totalOrderPrice);
		order.setOrderMenuList(orderMenuList); // ๋ฆฌ์ŠคํŠธ๋„ ๋„˜๊ฒจ์คŒ
		
		int result = orderService.registOrder(order);
		
		if(result > 0) {
			System.out.println("์ฃผ๋ฌธ ์™„๋ฃŒ ! :)");
		} else {
			System.out.println("์ฃผ๋ฌธ ์‹คํŒจ :(");
		}
	}
}

๐Ÿ‘‰ Run

โ—ผ Application Class

public class Application {

	public static void main(String[] args) {
		
		new OrderMenu().displayMenu();
	}
}

๐Ÿ‘€ DAO๋ฅผ ํ™œ์šฉํ•œ Controller < ์Œ์‹ ์ฃผ๋ฌธ ํ”„๋กœ๊ทธ๋žจ >

๐Ÿ’โ€โ™€๏ธ Controller์˜ ์—ญํ•  ๐Ÿ”ฅ์ค‘์š”๐Ÿ”ฅ

  • View์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •๋ณด๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ ๋ฐ›์Œ
  • ์ „๋‹ฌ ๋ฐ›์€ ๊ฐ’๋“ค์„ ๊ฒ€์ฆํ•˜๊ณ , ์ถ”๊ฐ€์ ์ธ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๊ฐ€๊ณต
  • Service์ชฝ์œผ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ์ธ์Šคํ„ด์Šค์— ๋‹ด๊ณ  Service์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ
  • ํ˜ธ์ถœํ•œ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ ๋ฐ›์•„ ์–ด๋–ค (Result)View๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ๊ฒƒ์ธ์ง€๋ฅผ ๊ฒฐ์ •

๐Ÿ™‹โ€ ์œ„์˜ 'DAO๋ฅผ ํ™œ์šฉํ•œ View'๊ฐ€ ์ผ๋ถ€ ๋ณ€๊ฒฝ/์ถ”๊ฐ€๋œ ๊ฒƒ ์ด๋ฏ€๋กœ ๋™์ผํ•œ ์ฝ”๋“œ๋Š” ์ƒ๋žต

๐Ÿ‘‰ Controller

โ—ผ OrderController Class

public class OrderController {
	
	private OrderService orderService = new OrderService();

	/* ๋ชจ๋“  ์นดํ…Œ๊ณ ๋ฆฌ ์กฐํšŒํ•˜๊ธฐ */
	public List<CategoryDTO> selectAllCategory() {
		
		List<CategoryDTO> categoryList = orderService.selectAllCategory();
		
		return categoryList;
	}
	
	/* ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก์—์„œ ๋ฉ”๋‰ด ๊ณ ๋ฅด๊ธฐ */
	public List<MenuDTO> selectMenuByCategory(int categoryCode) {
		return orderService.selectMenuByCategory(categoryCode);
	}

	/* ์ฃผ๋ฌธ ์ •๋ณด ๋„ฃ๊ธฐ */
	public void registOrder(Map<String, Object> requestMap) {
		
		/* 1. ๋ทฐ์—์„œ ์ „๋‹ฌ ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ ๊บผ๋‚ด์„œ ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ */
		int totalOrderPrice = (Integer)requestMap.get("totalOrderPrice");
		List<OrderMenuDTO> orderMenuList = (List<OrderMenuDTO>)requestMap.get("orderMenuList");
		
		/* 2. ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ƒ์„ฑํ•˜๊ธฐ */
		java.util.Date orderTime = new java.util.Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd");
		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
		String date = dateFormat.format(orderTime);
		String time = timeFormat.format(orderTime);
		
		/* 3. ์„œ๋น„์Šค์ชฝ์œผ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด DTO ์ธ์Šคํ„ด์Šค์— ๋‹ด๊ธฐ */
		OrderDTO order = new OrderDTO();
		order.setDate(date);
		order.setTime(time);
		order.setTotalOrderPrice(totalOrderPrice);
		order.setOrderMenuList(orderMenuList);
		
		/* 4. ์„œ๋น„์Šค(๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง)๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด ๋ฐ›์Œ */
		int result = orderService.registOrder(order);
		
		/* 5. ์„œ๋น„์Šค ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•ด ์„ฑ๊ณต ์‹คํŒจ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ๋ทฐ๋ฅผ ๊ฒฐ์ •ํ•จ */
		ResultView resultView = new ResultView();
		if(result > 0) {
			resultView.success();
		} else {
			resultView.failed();
		}
 	}
}

๐Ÿ‘‰ View

โ—ผ OrderMenu Class

public class OrderMenu {
	
	private OrderController orderController = new OrderController();

	public void displayMenu() {
		
		/* ๋ฐ˜๋ณต
		 * ------------------------
		 * 1. ์นดํ…Œ๊ณ ๋ฆฌ ์กฐํšŒ
		 * 2. ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ฉ”๋‰ด ์กฐํšŒ
		 * 3. ์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋–ค ๋ฉ”๋‰ด๋ฅผ ์ฃผ๋ฌธ ๋ฐ›์„ ๊ฒƒ์ธ์ง€ ์ž…๋ ฅ
		 * 4. ์ฃผ๋ฌธํ•  ์ˆ˜๋Ÿ‰ ์ž…๋ ฅ
		 * ------------------------
		 * 5. ์ฃผ๋ฌธ 
		 * */
		
		Scanner sc = new Scanner(System.in);
		
		List<OrderMenuDTO> orderMenuList = new ArrayList<>();
		int totalOrderPrice = 0;
		
		do {
			System.out.println("=============== ์Œ์‹ ์ฃผ๋ฌธ ํ”„๋กœ๊ทธ๋žจ ==================");
			
			List<CategoryDTO> categoryList = orderController.selectAllCategory();
			for(CategoryDTO category : categoryList) {
				System.out.println(category.getName());
			}
			
			System.out.println("=================================================");
			System.out.print("์ฃผ๋ฌธํ•˜์‹ค ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š” : ");
			String inputCategory = sc.nextLine();
			
			int categoryCode = 0;
			for(CategoryDTO category : categoryList) {
				if(category.getName().equals(inputCategory)) {
					categoryCode = category.getCode();
				}
			}
			
			System.out.println("================ ์ฃผ๋ฌธ ๊ฐ€๋Šฅ ๋ฉ”๋‰ด ====================");
			List<MenuDTO> menuList = orderController.selectMenuByCategory(categoryCode);
			for(MenuDTO menu : menuList) {
				System.out.println(menu);
			}
			
			System.out.print("์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š” : ");
			String inputMenu = sc.nextLine();
			
			int menuCode = 0;
			int menuPrice = 0;
			
			for(int i = 0; i < menuList.size();  i++) {
				MenuDTO menu = menuList.get(i);
				if(menu.getName().equals(inputMenu)) {
					menuCode = menu.getCode();
					menuPrice = menu.getPrice();
				}
			}
			
			System.out.print("์ฃผ๋ฌธํ•˜์‹ค ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
			int orderAmount = sc.nextInt();
			
			OrderMenuDTO orderMenu = new OrderMenuDTO();
			orderMenu.setMenuCode(menuCode);
			orderMenu.setOrderAmount(orderAmount);
			
			orderMenuList.add(orderMenu);
			totalOrderPrice += (menuPrice * orderAmount);
			
			System.out.print("๊ณ„์† ์ฃผ๋ฌธํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?(์˜ˆ/์•„๋‹ˆ์˜ค) : ");
			sc.nextLine();
			boolean isContinue = sc.nextLine().equals("์˜ˆ") ? true : false;
			
			if(!isContinue) break;
			
		} while(true);
		
		for(OrderMenuDTO orderMenu : orderMenuList) {
			System.out.println(orderMenu);
		}

		Map<String, Object> requestMap = new HashMap<>();
		requestMap.put("totalOrderPrice", totalOrderPrice);
		requestMap.put("orderMenuList", orderMenuList);
		
		orderController.registOrder(requestMap);
	}
}

โ—ผ ResultView Class

public class ResultView {
	
	public void success() {
		System.out.println("์ฃผ๋ฌธ์— ์„ฑ๊ณตํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
	}

	public void failed() {
		System.out.println("์ฃผ๋ฌธ์— ์‹คํŒจํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
	}
}
profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€