๐โโ๏ธ DAO(Data Access Object)๋,
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๊ทผ์ฉ ๊ฐ์ฒด. CRUD ์ฐ์ฐ์ ๋ด๋นํ๋ ๋ฉ์๋๋ค์ ์งํฉ์ผ๋ก ์ด๋ฃจ์ด์ง ํด๋์ค
- ์ด๋ ํ ๊ธฐ๋ฅ์ด ํ์ํ ๋๋ง๋ค ๊ฐ๋จํ ํธ์ถ๋งํ์ฌ ์ฌ์ฉํ ์ ์๊ฒ๋ ๋ฉ์๋๋ฅผ ๋ถ๋ฆฌํ์ฌ DAO์ ๋ด์ ์ฌ์ฉ
<?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>
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();
}
}
}
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 + "]";
}
}
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 + "]";
}
}
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();
}
}
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;
}
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;
}
}
>>> ์ ๊ท ๋ฉ๋ด ๋ฑ๋ก ์ ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก์ ์กฐํํด์ ๋ณด์ฌ์ฃผ๊ณ ์ ๊ท ๋ฉ๋ด ๋ฑ๋ก์ ์งํํ๋ ํ๋ฆ
MenuDAO menuDAO = new MenuDAO();
>>> ๋ฉ์๋๊ฐ ์๋ Application์์ Connection ๊ฐ์ฒด ์์ฑ
Connection conn = getConnection();
>>> ์ด ์ํ์ด ๋๋๊ณ ๋์ ๋ฐ์์ผํ๋ ๊ฐ์ด List์ด๊ธฐ ๋๋ฌธ์ List ์ฌ์ฉ
List<CategoryDTO> categoryList = menuDAO.selectAllcategory(conn);
// ๋ฐํ ํ์
// ์ธ์
for(CategoryDTO category : categoryList) {
System.out.println(category);
}
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์ ์ฌ๊ธฐ์ ๋ซ์์ฃผ๊ธฐ
<?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>
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();
}
}
}
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 + "]";
}
}
* 'DAO์ ์ฌ์ฉ'์ MenuDTO 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();
}
}
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;
}
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; >>> ๊ทธ๋ฆฌ๊ณ ๋ฆฌํด
}
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;
}
}
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);
>>> ํธ๋์ญ์
(๋
ผ๋ฆฌ์ ์ธ ๊ธฐ๋ฅ ์ํ ๋จ์) ๊ด๋ฆฌ๋ฅผ ์ํด 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);
}
๐ Ref. Service์ ์ญํ
1. Connection ์์ฑ
2. DAO์ ๋ฉ์๋ ํธ์ถ (๋
ผ๋ฆฌ์ ์ธ ๊ธฐ๋ฅ ๋จ์๋ก ๋ฌถ์ธ ๋ฉ์๋๋ค์ ๋ชจ๋ ํธ์ถํ๋ฏ๋ก ์ฌ๋ฌ ๋ฒ ํธ์ถ ํ ์๋ ์์)
3. ํธ๋์ญ์
์ ์ด(commit, rollback)
4. Connection ์ข
๋ฃ
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);
}
}
public class Application {
public static void main(String[] args) {
new MenuService().registNewMenu();
}
}
<?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>
* DAO๋ฅผ ํ์ฉํ Service์ JDBCTemplate๊ณผ ๋์ผ *
* 'DAO๋ฅผ ํ์ฉํ Service'์ CategoryDTO Class์ ๋์ผ *
* 'DAO์ ์ฌ์ฉ'์ MenuDTO 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 + "]";
}
}
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 + "]";
}
}
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();
}
}
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;
>>> ๋ฐํ๊ฐ ๋๋ฝํ์ง์๊ฒ ์กฐ์ฌ !
}
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;
>>> ๋ฐํ๊ฐ ๋๋ฝํ์ง์๊ฒ ์กฐ์ฌ !
}
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;
}
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;
}
public class OrderService {
// OrderDAO ๊ฐ์ฒด ์์ฑ
private OrderDAO orderDAO = new OrderDAO();
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; // ์ต์ข
๋ฐํ
}
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;
}
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 ๋ฐํ
}
}
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("์ฃผ๋ฌธ ์คํจ :(");
}
}
}
public class Application {
public static void main(String[] args) {
new OrderMenu().displayMenu();
}
}
๐โโ๏ธ Controller์ ์ญํ ๐ฅ์ค์๐ฅ
- View์์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ ๋ณด๋ฅผ ํ๋ผ๋ฏธํฐ ํํ๋ก ์ ๋ฌ ๋ฐ์
- ์ ๋ฌ ๋ฐ์ ๊ฐ๋ค์ ๊ฒ์ฆํ๊ณ , ์ถ๊ฐ์ ์ธ ์ ๋ณด๊ฐ ํ์ํ ๊ฒฝ์ฐ ๊ฐ๊ณต
- Service์ชฝ์ผ๋ก ์ ๋ฌํ๊ธฐ ์ํ ์ธ์คํด์ค์ ๋ด๊ณ Service์์ ๋น์ฆ๋์ค ๋ก์ง์ ๋ด๋นํ๋ ๋ฉ์๋๋ฅผ ํธ์ถ
- ํธ์ถํ ์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ ๋ฐ์ ์ด๋ค (Result)View๋ฅผ ๋ค์ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๊ฒ์ธ์ง๋ฅผ ๊ฒฐ์
๐โ ์์ 'DAO๋ฅผ ํ์ฉํ View'๊ฐ ์ผ๋ถ ๋ณ๊ฒฝ/์ถ๊ฐ๋ ๊ฒ ์ด๋ฏ๋ก ๋์ผํ ์ฝ๋๋ ์๋ต
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();
}
}
}
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);
}
}
public class ResultView {
public void success() {
System.out.println("์ฃผ๋ฌธ์ ์ฑ๊ณตํ์
จ์ต๋๋ค.");
}
public void failed() {
System.out.println("์ฃผ๋ฌธ์ ์คํจํ์
จ์ต๋๋ค.");
}
}