๐โโ๏ธ CRUD๋,
๋๋ถ๋ถ์ ์ปดํจํฐ ์ํํธ์จ์ด๊ฐ ๊ฐ์ง๋ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๊ธฐ๋ฅ์ธ Create(์์ฑ), Read(์ฝ๊ธฐ), Update(๊ฐฑ์ ), Delete(์ญ์ )๋ฅผ ๋ฌถ์ด์ ์ผ์ปซ๋ ๋ง
ojdbc8.jar
/ mybatis-3.5.11.jar
ํ์ผ ์ฝ์
ojdbc8.jar
/ mybatis-3.5.11.jar
ํ์ผ ์ถ๊ฐ๐ Ref. mybatis.jar ํ์ผ ๋ค์ด๋ก๋
mybatis-3.5.11.jar
๐โ ๋์ด๋ ํด๋์ค ๋ฐ ํ์ผ ์์๊ฐ ๊ณง ์์ฑ ์์์ด๋ฏ๋ก, ํ๋ฆ ์ ํ์ ํ๊ธฐ ๐ฅ์ค์๐ฅ
(์์ธํ ์์ฑ ์์๋ [ ] ์์ ์๋ ์ค๋ช ์ ์ฐธ๊ณ ํ ๊ฒ)
Mybatis
์์ '์์ํ๊ธฐ' > 'XML์์ SqlSessionFactory ๋น๋ํ๊ธฐ' ํญ ์๋์ ํ
ํ๋ฆฟ์ ๋ณต์ฌํ์ฌ ๊ฐ ์ฑ์๋ฃ๊ธฐ
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"/> <!-- JDBC์ MANAGED ์ ํ ๊ฐ๋ฅ
JDBC : ์๋ ์ปค๋ฐ, MANEGED : ์๋ ์ปค๋ฐ -->
<dataSource type="POOLED"> <!-- POOLED์ UNPOOLED ์ ํ ๊ฐ๋ฅ
POOLED : ์ปค๋ฅ์
ํ ์ฌ์ฉ, UNPOOLED : ์ปค๋ฅ์
ํ ๋ฏธ์ฌ์ฉ -->
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="C##GREEDY"/>
<property name="password" value="GREEDY"/>
</dataSource>
</environment>
</environments>
<mappers> <!-- ๋งคํ์ ์ํด mapper ๋ฑ๋ก -->
<mapper resource="com/greedy/section01/xmlconfig/menu-mapper.xml"/>
</mappers>
</configuration>
public class Template {
>>> 'SqlSessionFactory'๋ ์ ํ๋ฆฌ์ผ์ด์
์ ์คํํ๋ ๋์ ์กด์ฌํด์ผํจ
>>> ์ ํ๋ฆฌ์ผ์ด์
์ด ์คํ ๋๋ ๋์ ์ฌ๋ฌ ์ฐจ๋ก SqlSessionFactory๋ฅผ ๋ค์ ๋น๋ํ์ง ์๋ ๊ฒ์ด ๊ฐ์ฅ ์ข์ ํํ
>>> ์ ํ๋ฆฌ์ผ์ด์
์ค์ฝํ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ '์ฑ๊ธํค ํจํด'์ ์ด์ฉํ๋ ๊ฒ
// '์ฑ๊ธํคํจํด'์ ์ํด ํ๋(๊ณต๊ฐ) ์์ฑ
private static SqlSessionFactory sqlSessionFactory;
public static SqlSession getSqlSession() {
if(sqlSessionFactory == null) { // ๋จ ํ๋๋ง ์์ฑ๋ ์ ์๋๋ก ๋ณด์ฅํ๊ธฐ ์ํด
// try-catch๋ฌธ์ ์ด ์กฐ๊ฑด์ if๋ฌธ์ผ๋ก ๊ฐ์ธ๊ธฐ
// ํ๋ฒ ์์ฑ๋ ์ดํ๋ก๋ null์ด ์๋๋ฏ๋ก ๋ค์ build๋ฅผ ์ํ X
>>> '์ฑ๊ธํคํจํด'
try {
String resource = "com/greedy/section01/xmlconfig/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
>>> 'SqlSessionFacoryBuilder๋ ์์ฑ ํ ์ ์งํ ํ์ X
>>> ๋ฐ๋ผ์ '๋ฉ์๋ ์ค์ฝํ'๋ก ์์ฑ
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
>>> 'SqlSession'์ ์์ฒญ ์๋ง๋ค ์์ฑํด์ผํจ
>>> SqlSession์ ์ค๋ ๋์ ์์ ํ์ง ์๊ธฐ ๋๋ฌธ์ ๊ณต์ ๋์ง ์์์ผํจ
>>> ์์ฒญ ์ ์์ฑํ๊ณ ์์ฒญ์ด ์๋ฃ๋๋ฉด closeํ๋ HTTP์์ฒญ๊ณผ ์ ์ฌํ ์ค์ฝํ์ ๋๋ ๊ฒ์ด ๊ฐ์ฅ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ
>>> (๋ฐ๋ผ์ Service์์ getSqlSession()๋ฉ์๋๋ก SqlSession๋ฅผ ํธ์ถํ๊ณ ์ฌ์ฉํ ๋ค, ๊ทธ ์ค์ฝํ์์ ๋ฐ๋ฉ)
SqlSession sqlSession = sqlSessionFactory.openSession(false);
// sqlSession ์ฌ์ฉํ ํ ๋ฐ๋ฉ
return sqlSession;
}
}
๐ Ref.
* singleton(์ฑ๊ธํค)
: ์ค๋ก์ง ํ๋์ ๊ฐ์ฒด๋ง ๋ง๋ค์ด์ง๋ ๊ฒ์ ๋ณด์ฅ
private int menuCode;
private String menuName;
private int menuPrice;
private int categoryCode;
private String orderableStatus;
/* ๊ธฐ๋ณธ ์์ฑ์, ๋ชจ๋ ๋งค๊ฐ๋ณ์๊ฐ ์กด์ฌํ๋ ์์ฑ์ */
...
/* getter & setter */
...
/* toString */
...
Scanner sc = new Scanner(System.in);
MenuController menucontroller = new MenuController();
do {
System.out.println("===== ๋ฉ๋ด ๊ด๋ฆฌ =====");
System.out.println("1. ๋ฉ๋ด ์ ์ฒด ์กฐํ");
System.out.println("2. ๋ฉ๋ด ์ฝ๋๋ก ๋ฉ๋ด ์กฐํ");
System.out.println("3. ์ ๊ท ๋ฉ๋ด ์ถ๊ฐ");
System.out.println("4. ๋ฉ๋ด ์์ ");
System.out.println("5. ๋ฉ๋ด ์ญ์ ");
System.out.print("๋ฉ๋ด ๊ด๋ฆฌ ๋ฒํธ๋ฅผ ์
๋ ฅ : ");
int no = sc.nextInt();
switch(no) {
case 1: menucontroller.selectAllMenu(); break; // [MenuController ํด๋์ค ์์ฑ ๋ฐ selectAllMenu()๋ฉ์๋ ์์ฑ]
case 2: menucontroller.selectMenuByCode(inputMenuCode()); break;
// ์๋์ inputMenuCode() ๋ฉ์๋์ ๋ฐํ๊ฐ์ด selectMenuByCode()์ ์ธ์๋ก ๋์ด๊ฐ
case 3: menucontroller.registMenu(inputMenu()); break;
case 4: menucontroller.updateMenu(inputModifyMenu()); break;
case 5: menucontroller.deleteMenu(inputMenuCode()); break;
default : System.out.println("์๋ชป ๋ ๋ฉ๋ด๋ฅผ ์ ํํ์
จ์ต๋๋ค.");
}
} while(true);
}
private static Map<String, String> inputMenuCode(){
Scanner sc = new Scanner(System.in);
System.out.print("๋ฉ๋ด ์ฝ๋ ์
๋ ฅ : ");
String code = sc.nextLine();
Map<String, String> parameter = new HashMap<>();
parameter.put("code", code);
return parameter;
}
private static Map<String, String> inputMenu() {
Scanner sc = new Scanner(System.in);
System.out.print("๋ฉ๋ด ์ด๋ฆ : ");
String name = sc.nextLine();
System.out.print("๋ฉ๋ด ๊ฐ๊ฒฉ : ");
String price = sc.nextLine();
System.out.print("์นดํ
๊ณ ๋ฆฌ ์ฝ๋ : ");
String CategoryCode = sc.nextLine();
Map<String, String> parameter = new HashMap<>();
parameter.put("name", name);
parameter.put("price", price);
parameter.put("categoryCode", CategoryCode);
return parameter;
}
private static Map<String, String> inputModifyMenu() {
Scanner sc = new Scanner(System.in);
System.out.print("์์ ํ ๋ฉ๋ด์ ๋ฉ๋ด ์ฝ๋ : ");
String code = sc.nextLine();
System.out.print("์์ ํ ๋ฉ๋ด ์ด๋ฆ : ");
String name = sc.nextLine();
System.out.print("์์ ํ ๋ฉ๋ด ๊ฐ๊ฒฉ : ");
String price = sc.nextLine();
System.out.print("์์ ํ ์นดํ
๊ณ ๋ฆฌ ์ฝ๋ : ");
String CategoryCode = sc.nextLine();
Map<String, String> parameter = new HashMap<>();
parameter.put("code", code);
parameter.put("name", name);
parameter.put("price", price);
parameter.put("categoryCode", CategoryCode);
return parameter;
}
๐โ Controller์ ์ญํ
- View์์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ ๋ณด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ ๋ฐ์ผ๋ฉด ์ ๋ฌ ๋ฐ์ ๊ฐ๋ค์ ๊ฒ์ฆ / ์ถ๊ฐ์ ์ธ ์ ๋ณด๊ฐ ํ์ํ ๊ฒฝ์ฐ ๊ฐ๊ณต
- Service๋ก ์ ๋ฌํ๊ธฐ ์ํ ์ธ์คํด์ค์ ๋ด๊ณ ๋ฉ์๋ ํธ์ถ
- ํธ์ถํ ์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ ๋ฐ์ ์ด๋ ํ View๋ฅผ ๋ค์ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๊ฒ์ธ์ง๋ฅผ ๊ฒฐ์
public class MenuController {
/* final๋ก ์ ์ธ ํ ์์ฑ์์์ ์ด๊ธฐํํ ๊ฒ */
private final MenuService menuService; // [MenuService ํ๋ ์ ์ธ ํ ํด๋์ค ๋ง๋ค๊ธฐ]
private final PrintResult printResult; // [PrintResult ํ๋ ์ ์ธ ํ ํด๋์ค ๋ง๋ค๊ธฐ]
/* ์์ฑ๋ ํ๋ ์ด๊ธฐํ */
public MenuController() {
menuService = new MenuService();
printResult = new PrintResult();
}
public void selectAllMenu() {
List<MenuDTO> menuList = menuService.selectAllMenu();
// [menuService ํด๋์ค ์์ฑํ์ฌ selectAllMenu()๋ฉ์๋ ์์ฑ]
/* ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค Result View */
if(menuList != null) {
printResult.printMenuList(menuList);
} else {
printResult.printErrorMessage("selectList");
}
}
public void selectMenuByCode(Map<String, String> parameter) {
/* ์ ๋ฌ๋๋ parameter๋ String์ผ๋ก ๋ฐ์์ค๊ธฐ ๋๋ฌธ์ int๋ก parsing */
int code = Integer.parseInt(parameter.get("code"));
MenuDTO menu = menuService.selectMenuByCode(code);
// [menuService์ selectMenuByCode() ๋ฉ์๋ ๋ง๋ค๋ฉด์ ์ธ์์ ๋ฌ]
if(menu != null) {
printResult.printMenu(menu);
} else {
printResult.printErrorMessage("selectOne");
}
}
public void registMenu(Map<String, String> parameter) {
/* ์ ๋ฌ๋๋ parameter๋ String์ผ๋ก ๋ฐ์์ค๊ธฐ ๋๋ฌธ์ int๋ก parsing */
/* ๊ฐ์ด ์ฌ๋ฌ ๊ฐ์ด๋ฏ๋ก MenuDTO๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด ํธ๋ฆฌ */
MenuDTO menu = new MenuDTO();
menu.setMenuName(parameter.get("name"));
menu.setMenuPrice(Integer.parseInt(parameter.get("price")));
menu.setCategoryCode(Integer.parseInt(parameter.get("categoryCode")));
if(menuService.registMenu(menu)) { // [menuService์ registMenu()๋ฉ์๋ ์์ฑํ๋ฉด์ ์ธ์์ ๋ฌ]
printResult.printSuccessMessage("insert"); // [printResult์ printSuccessMessage() ๋ฉ์๋ ์์ฑ]
} else {
printResult.printErrorMessage("insert");
}
}
public void updateMenu(Map<String, String> parameter) {
/* ์ ๋ฌ๋๋ parameter๋ String์ผ๋ก ๋ฐ์์ค๊ธฐ ๋๋ฌธ์ int๋ก parsing */
/* ๊ฐ์ด ์ฌ๋ฌ ๊ฐ์ด๋ฏ๋ก MenuDTO๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด ํธ๋ฆฌ */
MenuDTO menu = new MenuDTO();
menu.setMenuCode(Integer.parseInt(parameter.get("code")));
menu.setMenuName(parameter.get("name"));
menu.setMenuPrice(Integer.parseInt(parameter.get("price")));
menu.setCategoryCode(Integer.parseInt(parameter.get("categoryCode")));
if(menuService.modifyMenu(menu)) {
printResult.printSuccessMessage("update");
} else {
printResult.printErrorMessage("update");
}
}
public void deleteMenu(Map<String, String> parameter) {
/* ์ ๋ฌ๋๋ parameter๋ String์ผ๋ก ๋ฐ์์ค๊ธฐ ๋๋ฌธ์ int๋ก parsing */
int code = Integer.parseInt(parameter.get("code"));
/* DELETE๋ DTO๋ฅผ ์ฌ์ฉํ ํ์ X */
if(menuService.deleteMenu(code)) {
printResult.printSuccessMessage("delete");
} else {
printResult.printErrorMessage("delete");
}
}
}
๐โ Service์ ์ญํ
- SqlSession(Connection) ์์ฑ
- DAO์ ๋ฉ์๋ ํธ์ถ
: ๋ ผ๋ฆฌ์ ์ธ ๊ธฐ๋ฅ ๋จ์๋ก ๋ฌถ์ธ ๋ฉ์๋๋ค์ ๋ชจ๋ ํธ์ถํ๋ฏ๋ก ์ฌ๋ฌ๋ฒ ํธ์ถ ํ ์๋ ์์- ํธ๋์ญ์ ์ ์ด (commit, rollback)
- SqlSession(Connection) ๋ฐ๋ฉ
public class MenuService {
private final MenuDAO menuDAO; // [MenuDAO ํด๋์ค ์์ฑ]
public MenuService() {
menuDAO = new MenuDAO();
}
public List<MenuDTO> selectAllMenu() {
/* SqlSession ํธ์ถ (getConnection()๊ณผ ๊ฐ์) */
SqlSession sqls = getSqlSession();
List<MenuDTO> menuList = menuDAO.selectAllMenu(sqls);
// [menuDAO์ selectAllMenu()๋ฉ์๋ ์์ฑ ๋ฐ ์ธ์ ์ ๋ฌ]
>>> Service๋ ํญ์ DAO์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉฐ SqlSession(Connection) ์ ๋ฌ
/* SELECT์ด๋ฏ๋ก ํธ๋์ญ์
(commit, rollback) ํ์ X */
/* ๋ฐ๋ฉ */
sqls.close();
/* ๋ฐํ(์ ๋ฌ) */
return menuList;
}
public MenuDTO selectMenuByCode(int code) {
SqlSession sqls = getSqlSession();
MenuDTO menu = menuDAO.selectMenuByCode(sqls, code);
sqls.close();
return menu;
}
public boolean registMenu(MenuDTO menu) { >>> ๋ฐํ๊ฐ์ด true or false์ด๋ฏ๋ก booleanํ์
SqlSession sqls = getSqlSession();
/* INSERT ์ด๋ฏ๋ก int๋ก ๊ฒฐ๊ณผ๊ฐ ๋ฐํ */
int result = menuDAO.insertMenu(sqls, menu);
// [menuDAO์ insertMenu()๋ฉ์๋ ์์ฑ ๋ฐ ์ธ์๋ค ์ ๋ฌ]
/* ํธ๋์ญ์
๊ด๋ฆฌ */
if(result > 0) {
sqls.commit();
} else {
sqls.rollback();
}
>>> ๋ฐ๋ก ํ
ํ๋ฆฟ์ commit, rollback์ฝ๋๋ฅผ ์์ฑํ์ง ์์๋ Mybatis์ ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ !
sqls.close();
return result > 0 ? true : false;
}
public boolean modifyMenu(MenuDTO menu) {
SqlSession sqls = getSqlSession();
/* UPDATE ์ด๋ฏ๋ก int๋ก ๊ฒฐ๊ณผ๊ฐ ๋ฐํ */
int result = menuDAO.updateMenu(sqls, menu);
/* ํธ๋์ญ์
๊ด๋ฆฌ */
if(result > 0) {
sqls.commit();
} else {
sqls.rollback();
}
sqls.close();
return result > 0 ? true : false;
}
public boolean deleteMenu(int code) {
SqlSession sqls = getSqlSession();
/* DELETE ์ด๋ฏ๋ก int๋ก ๊ฒฐ๊ณผ๊ฐ ๋ฐํ */
int result = menuDAO.deleteMenu(sqls, code);
/* ํธ๋์ญ์
๊ด๋ฆฌ */
if(result > 0) {
sqls.commit();
} else {
sqls.rollback();
}
sqls.close();
return result > 0 ? true : false;
}
}
๐โ DAO์ ์ญํ
- ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด ๊ทธ์ ์์ํ๋ Mybatis์ ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ mapper.xmlํ์ผ ํธ์ถํ ๋ค, ๊ทธ๊ฒ์ ๋ฐํ
Mybatis์ ์์ฒด ๋ฉ์๋๋ก ์ธํด DAO์์ ๋ฉ์๋๋ฅผ ๋นํจ์จ์ ์ผ๋ก ์ฌ๋ฌ ๋ฒ ์ ์ ์ผ์ด ํ์ ํ ์ค์ด๋ค์์ (ํ๋ ์์ํฌ ์ฌ์ฉ์ ์ฅ์ )
public class MenuDAO {
public List<MenuDTO> selectAllMenu(SqlSession sqls) {
/* return sqls.selectList("namespace.id"); */ >>> ์ ๊ทผ ๋ฐฉ์
return sqls.selectList("MenuMapper.selectAllMenu");
// ์กฐํํ๊ณ ์ถ์ ๊ฒ์ด MenuDTO์ ๋ฆฌ์คํธ์ด๋ฏ๋ก selectList()๋ฉ์๋ ํ์ฉ
// ๊ดํธ ์์๋ ์ด๋ค ์ฟผ๋ฆฌ๋ฌธ์ ์ ๋ฌํ ๊ฒ์ธ์ง ์์ฑ ("namespace.id") *menu-mapper.xml ์ฐธ๊ณ
}
public MenuDTO selectMenuByCode(SqlSession sqls, int code) {
return sqls.selectOne("MenuMapper.selectMenuByCode", code);
// "MenuMapper.selectMenuByCode" : ์กฐํํ๊ณ ์ํ๋ ์ฟผ๋ฆฌ๋ฌธ
// code : ์ํ ์, ์ฌ์ฉ๋ ๋งํ ์ถ๊ฐ์ ์ธ ๊ฐ
}
public int insertMenu(SqlSession sqls, MenuDTO menu) {
return sqls.insert("MenuMapper.insertMenu", menu);
}
public int updateMenu(SqlSession sqls, MenuDTO menu) {
return sqls.update("MenuMapper.updateMenu", menu);
}
public int deleteMenu(SqlSession sqls, int code) {
return sqls.delete("MenuMapper.deleteMenu", code);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="MenuMapper">
<!-- ์กฐํํ ์ปฌ๋ผ๊ณผ DTO๋ฅผ ๋งคํ(์ฐ๊ฒฐ)์ํค๊ธฐ ์ํ ์ค์ ์ผ๋ก resultMap์ ์ ์ธ -->
<resultMap type="com.greedy.section01.xmlconfig.MenuDTO" id="menuResultMap">
<id property="menuCode" column="MENU_CODE"/> <!-- property="DTO์ ํ๋ ๋ช
์นญ" -->
<result property="menuName" column="MENU_NAME"/> <!-- result์๋ id์ ์
๋ ฅํ์ง์์ ๋๋จธ์ง ๋ชจ๋ ์
๋ ฅ -->
<result property="menuPrice" column="MENU_PRICE"/>
<result property="categoryCode" column="CATEGORY_CODE"/>
<result property="orderableStatus" column="ORDERABLE_STATUS"/>
</resultMap>
<select id="selectAllMenu" resultMap="menuResultMap"> <!-- ๊ฒฐ๊ณผ๊ฐ์ "menuResultMap"๋ฅผ ์ด์ฉํด์ ๋งคํ์ํฌ ๊ฒ-->
SELECT
MENU_CODE
, MENU_NAME
, MENU_PRICE
, CATEGORY_CODE
, ORDERABLE_STATUS
FROM TBL_MENU
WHERE ORDERABLE_STATUS = 'Y'
ORDER BY MENU_CODE
</select>
<select id="selectMenuByCode" parameterType= "_int" resultMap="menuResultMap">
<!-- ์์ ๋ง์ฐฌ๊ฐ์ง๋ก DTO์์ ์ฐธ์กฐํด์ค๊ธฐ ๋๋ฌธ์ resultMap์ผ๋ก ๋์ผํ๊ฒ ์์ฑ -->
<!-- parameterType= "_int" : ๋ง์ด๋ฐํฐ์ค์์ ์ ๊ณตํ๋ ๋ณ์นญ์ผ๋ก ํ๊ธฐ. int๋ '_int' (๋ช
์ํ์ง ์์๋ ๋์์ ๋ฌธ์ X) -->
<!-- ํ๋ผ๋ฏธํฐ๊ฐ ํ ๊ฐ์ธ ๊ฒฝ์ฐ ๋ฐ์ธ๋ฉ ๋๋ ์ด๋ฆ(ex:code)์ ๋ฌด์์ ์์ฑํด๋ ์๊ด X. parameterType ๋ํ ์ง์ ํ์ง์์๋ ๋จ.
resultMap : ์์์ ์ ์ํ resultMap์ ๋ฆฌํด ํ์
์ผ๋ก ์ด์ฉํ๋ ๊ฒฝ์ฐ
resultType : ์ด๋ฏธ ์ ์๋์ด์๋ ํ์
์ ๋ฆฌํด ํ์
์ผ๋ก ์ด์ฉํ๋ ๊ฒฝ์ฐ
-->
SELECT
MENU_CODE
, MENU_NAME
, MENU_PRICE
, CATEGORY_CODE
, ORDERABLE_STATUS
FROM TBL_MENU
WHERE ORDERABLE_STATUS = 'Y'
AND MENU_CODE = #{ code } <!-- code ๋์ ๋ค๋ฅธ ๋ฌธ์ ๊ฐ๋ฅ -->
<!-- ?(์์นํ๋)๊ฐ ์๋ #{ code }์ฒ๋ผ ์ด๋ค ๊ฐ์ ๋ฃ์์ง ํ์คํ ๋ช
์ํด์ผํจ
(์์นํ๋์ ๊ฐ์ ๋ฃ๋ ์ฝ๋๋ฅผ MyBatis๊ฐ ๋์ ์คํํ๋ฏ๋ก ๋ฐ๋ก ๊ฐ์ ๋ฃ์ ๊ธฐํ X) -->
</select>
<!-- insert, update, delete์ ๊ฒฝ์ฐ resultType ์์ฑ X.
๊ธฐ๋ณธ _int๋ก ์ํ ๊ฒฐ๊ณผ ํ์ ๊ฐฏ์๋ฅผ ๋ฆฌํด. parameterType์ ์๋ต ๊ฐ๋ฅ
ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌ ๋ DTO์ ํ๋๋ช
์ผ๋ก ๊ฐ์ ๋ฐ์ธ๋ฉ ํด์ค์ผํ๋ค. getter๋ฅผ ์ด์ฉํ๋ฉฐ gertter๊ฐ ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฐ์-->
<insert id="insertMenu" parameterType="com.greedy.section01.xmlconfig.MenuDTO">
<!-- parameterType์ MenuDTO์ ํด๋์ค๋ฅผ ๊ธฐ์
(๋ช
์ ํ์X)-->
INSERT
INTO TBL_MENU
(
MENU_CODE
, MENU_NAME
, MENU_PRICE
, CATEGORY_CODE
, ORDERABLE_STATUS
)
VALUES
(
SEQ_MENU_CODE.NEXTVAL
<!-- Mybatis์์ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ด๋ฏ๋ก, ํ๋๋ช
๊ณผ ์ ํํ ์ผ์นํ๊ฒ ์์ฑ ๋ฐ DTO์ getter ๋ฉ์๋ ์กด์ฌํด์ผํจ -->
, #{ menuName }
, #{ menuPrice }
, #{ categoryCode }
, 'Y' <!-- ์ด๋ฏธ 'Y'๋ก fix๋ ๊ฐ์ -->
)
</insert>
<update id="updateMenu" parameterType="com.greedy.section01.xmlconfig.MenuDTO">
UPDATE
TBL_MENU
SET MENU_NAME = #{ menuName }
, MENU_PRICE = #{ menuPrice }
, CATEGORY_CODE = #{ categoryCode }
WHERE MENU_CODE = #{ menuCode }
</update>
<delete id="deleteMenu" parameterType="_int">
DELETE
FROM TBL_MENU
WHERE MENU_CODE = #{ menuCode }
<!-- ๊ฐ์ด ํ๋์ด๋ฏ๋ก, ์ฌ๊ธฐ์๋ ์๋ฌด ๋จ์ด๊ฐ ๋ค์ด๊ฐ๋ ์๊ด X -->
</delete>
</mapper>
public class PrintResult {
/* ์ ๋ฌ ๋ ๋ฆฌ์คํธ ์ถ๋ ฅ์ฉ์ผ๋ก ์ฌ์ฉํ๋ ๋ฉ์๋ */
public void printMenuList(List<MenuDTO> menuList) {
for(MenuDTO menu : menuList) {
System.out.println(menu);
}
}
/* ์ ๋ฌ ๋ ๋ฌธ์์ด ์ถ๋ ฅ์ฉ์ผ๋ก ์ฌ์ฉํ๋ ๋ฉ์๋ */
public void printMenu(MenuDTO menu) {
System.out.println(menu);
}
/* ์๋ฌ ๋ฉ์ธ์ง ํธ์ถ์ฉ์ผ๋ก ์ฌ์ฉํ๋ ๋ฉ์๋ */
public void printErrorMessage(String errorCode) { >>> ๋งค๊ฐ๋ณ์๋ก errorCode ์ฌ ์์
String errorMessage = "";
switch(errorCode) {
case "selectList" : errorMessage = "๋ฉ๋ด ๋ชฉ๋ก ์กฐํ์ ์คํจํ์์ต๋๋ค."; break;
case "selectOne" : errorMessage = "๋ฉ๋ด ์กฐํ์ ์คํจํ์์ต๋๋ค."; break;
case "insert" : errorMessage = "๋ฉ๋ด ๋ฑ๋ก์ ์คํจํ์์ต๋๋ค."; break;
case "update" : errorMessage = "๋ฉ๋ด ์์ ์ ์คํจํ์์ต๋๋ค."; break;
case "delete" : errorMessage = "๋ฉ๋ด ์ญ์ ์ ์คํจํ์์ต๋๋ค."; break;
}
System.out.println(errorMessage);
}
/* ์ฑ๊ณต ๋ฉ์ธ์ง ํธ์ถ์ฉ์ผ๋ก ์ฌ์ฉํ๋ ๋ฉ์๋ */
public void printSuccessMessage(String successCode) {
String successMessage = "";
switch(successCode) {
case "insert" : successMessage = "๋ฉ๋ด ๋ฑ๋ก์ ์ฑ๊ณตํ์์ต๋๋ค."; break;
case "update" : successMessage = "๋ฉ๋ด ์์ ์ ์ฑ๊ณตํ์์ต๋๋ค."; break;
case "delete" : successMessage = "๋ฉ๋ด ์ญ์ ์ ์ฑ๊ณตํ์์ต๋๋ค."; break;
}
System.out.println(successMessage);
}
}
๐โโ๏ธ JAVAconfig ๋ฐฉ์์ XMLconfig ๋ฐฉ์๊ณผ ๊ธฐ๋ฅ์ ์ผ๋ก๋ ์์ ํ ๋์ผํ๋, ์ฝ๋ ์์ฑ ๋ถ๋ถ์์ ์ฝ๊ฐ์ ์ฐจ์ด ์กด์ฌ
- MenuDTO, Application, MenuController, PrintResult ํด๋์ค๋ XMLconfig ๋ฐฉ์๊ณผ ์๋ฒฝํ ๋์ผ
- MenuService์์ MenuDAO๊ฐ ์๋ MenuMapper ์ธํฐํ์ด์ค๋ฅผ ์ง์ ํธ์ถํ์ฌ MenuDAO ํด๋์ค ์กด์ฌ X
- ๋ง์ฐฌ๊ฐ์ง๋ก menu-mapper.xmlํ์ผ์ด ์๋ MenuMapper ์ธํฐํ์ด์ค๋ฅผ ํ์ฉํ๋ฏ๋ก mybatis-config.xmlํ์ผ ์กด์ฌ X
public class Template {
/* ์ฐ๊ฒฐํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๋ณด๋ฅผ ํ๋๋ก ์์ฑ */
private static String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static String URL = "jdbc:oracle:thin:@localhost:1521:xe";
private static String USER = "C##GREEDY";
private static String PASSWORD = "GREEDY";
private static SqlSessionFactory sqlSessionFactory;
public static SqlSession getSqlSession() {
if(sqlSessionFactory == null) {
/* ํ๊ฒฝ ๊ฐ์ฒด ์์ฑ */
Environment environment = new Environment("dev"
, new JdbcTransactionFactory()
, new PooledDataSource(DRIVER, URL, USER, PASSWORD)
);
Configuration configuration = new Configuration(environment);
configuration.addMapper(MenuMapper.class);
// [ MenuMapper ์ธํฐํ์ด์ค ์์ฑ ]
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
// sqlSession ์ฌ์ฉํ ํ ๋ฐ๋ฉ (.xmlconfig ๋ณด๋ค ๊ฐ๋จํ๊ฒ ์์ฑ ๊ฐ๋ฅ)
return sqlSessionFactory.openSession(false);
}
}
* XMLconfig ๋ฐฉ์์ MenuDTO์ ๋์ผ *
* XMLconfig ๋ฐฉ์์ Application์ ๋์ผ *
* XMLconfig ๋ฐฉ์์ MenuController์ ๋์ผ *
public class MenuService {
/* MenuController๋ก ๋ถํฐ ์ธ์๋ฅผ ์ ๋ฌ๋ฐ์ผ๋ฉฐ ๋ฉ์๋๋ค์ ์์ฑ */
/* ์์ฑํด๋ MenuMapper ์ธํฐํ์ด์ค๋ฅผ ํ๋์ ๋จผ์ ์ ์ธ */
private MenuMapper menuMapper;
public List<MenuDTO> selectAllMenu() {
/* SqlSession ํธ์ถ */
SqlSession sqls = getSqlSession();
/* MenuMapper์ ๋ฉ์๋ ํธ์ถ ๋ฐ ๊ฐ ๋ฐํ๋ฐ๊ธฐ */
menuMapper = sqls.getMapper(MenuMapper.class);
List<MenuDTO> menuList = menuMapper.selectAllMenu();
// [ menuMapper ์ธํฐํ์ด์ค์ selectAllMenu() ์์ฑ ]
sqls.close();
return menuList;
}
public MenuDTO selectMenuByCode(int code) {
/* SqlSession ๋ฐ menuMapper ํธ์ถ */
SqlSession sqls = getSqlSession();
menuMapper = sqls.getMapper(MenuMapper.class);
MenuDTO menu = menuMapper.selectMenuByCode(code);
// [ MenuMapper ์ธํฐํ์ด์ค์ selectMenuByCode() ์์ฑ ]
sqls.close();
return menu;
}
public boolean registMenu(MenuDTO menu) {
/* SqlSession ๋ฐ menuMapper ํธ์ถ */
SqlSession sqls = getSqlSession();
menuMapper = sqls.getMapper(MenuMapper.class);
/* INSERT์ด๋ฏ๋ก int๋ก ๋ฐํ๋ฐ๊ธฐ */
int result = menuMapper.insertMenu(menu);
if(result > 0) {
sqls.commit();
} else {
sqls.rollback();
}
sqls.close();
return result > 0 ? true : false;
}
public boolean modifyMenu(MenuDTO menu) {
/* SqlSession ๋ฐ menuMapper ํธ์ถ */
SqlSession sqls = getSqlSession();
menuMapper = sqls.getMapper(MenuMapper.class);
/* UPDATE์ด๋ฏ๋ก int๋ก ๋ฐํ๋ฐ๊ธฐ */
int result = menuMapper.modifyMenu(menu);
if(result > 0) {
sqls.commit();
} else {
sqls.rollback();
}
sqls.close();
return result > 0 ? true : false;
}
public boolean deleteMenu(int code) {
/* SqlSession ๋ฐ menuMapper ํธ์ถ */
SqlSession sqls = getSqlSession();
menuMapper = sqls.getMapper(MenuMapper.class);
/* DELETE์ด๋ฏ๋ก int๋ก ๋ฐํ๋ฐ๊ธฐ */
int result = menuMapper.deleteMenu(code);
if(result > 0) {
sqls.commit();
} else {
sqls.rollback();
}
sqls.close();
return result > 0 ? true : false;
}
}
๐โ MenuMapper์ ์ญํ
menu-mapper.xmlํ์ผ์ ์ ๋ ฅํ๋ resultMap ๋ฐ ์ฟผ๋ฆฌ๋ฌธ์ javaํ์์ผ๋ก ์์ฑ
public interface MenuMapper {
/* @Results : ์ด๊ณณ์ ์ ์๋ ๋๋ก ๋งคํ (์ด๋ค ์ปฌ๋ผ์ ์ด๋ค ๊ฐ(ํ๋๋ช
)์ ๋ฃ์์ง ์์ฑ) */
@Results(id = "menuResultMap", value= {
@Result(id = true, property = "menuCode", column = "MENU_CODE"),
@Result(property = "menuName", column = "MENU_NAME"),
@Result(property = "menuPrice", column = "MENU_PRICE"),
@Result(property = "categoryCode", column = "CATEGORY_CODE"),
@Result(property = "orderableStatus", column = "ORDERABLE_STATUS")
})
/* 1. ๋ฉ๋ด ์ ์ฒด ์กฐํ */
@Select("SELECT \n" +
" MENU_CODE\n" +
" , MENU_NAME\n" +
" , MENU_PRICE\n" +
" , CATEGORY_CODE\n" +
" , ORDERABLE_STATUS\n" +
" FROM TBL_MENU\n" +
" WHERE ORDERABLE_STATUS = 'Y'\n" +
" ORDER BY MENU_CODE") // SELECT ๊ตฌ๋ฌธ์ ์์ฑ
List<MenuDTO> selectAllMenu();
/* 2. ๋ฉ๋ด ์ฝ๋๋ก ๋ฉ๋ด ์กฐํ */
@ResultMap("menuResultMap") // ์์์ ์์ฑํ resultMap์ ๋ค์ ์ฌ์ฉ ๊ฐ๋ฅ // @ResultMap(id)
@Select("SELECT \n" +
" MENU_CODE\n" +
" , MENU_NAME\n" +
" , MENU_PRICE\n" +
" , CATEGORY_CODE\n" +
" , ORDERABLE_STATUS\n" +
" FROM TBL_MENU\n" +
" WHERE ORDERABLE_STATUS = 'Y'\n" +
" AND MENU_CODE = #{ code }")
MenuDTO selectMenuByCode(int code);
/* 3. ์ ๊ท ๋ฉ๋ด ์ถ๊ฐ */
@Insert("INSERT\n" +
" INTO TBL_MENU\n" +
"(\n" +
" MENU_CODE\n" +
", MENU_NAME\n" +
", MENU_PRICE\n" +
", CATEGORY_CODE\n" +
", ORDERABLE_STATUS\n" +
")\n" +
"VALUES\n" +
"(\n" +
" SEQ_MENU_CODE.NEXTVAL\n" +
", #{ menuName }\n" +
", #{ menuPrice }\n" +
", #{ categoryCode }\n" +
", 'Y'\n" +
")")
int insertMenu(MenuDTO menu);
/* 4. ๋ฉ๋ด ์์ */
@Update("UPDATE\n" +
" TBL_MENU\n" +
" SET MENU_NAME = #{ menuName }\n" + // #{ } ๊ฐ์ ํ๋๋ช
๊ณผ ์ ํํ ์ผ์นํ๋๋ก ์์ฑ
" , MENU_PRICE = #{ menuPrice }\n" +
" , CATEGORY_CODE = #{ categoryCode }\n" +
" WHERE MENU_CODE = #{ menuCode }")
int modifyMenu(MenuDTO menu);
/* 5. ๋ฉ๋ด ์ญ์ */
@Delete("DELETE\n" +
" FROM TBL_MENU\n" +
" WHERE MENU_CODE = #{ code }")
int deleteMenu(int code);
}
* XMLconfig ๋ฐฉ์์ PrintResult์ ๋์ผ *
๐โโ๏ธ XMLconfig + JAVAconfig ๋ฐฉ์์ด๋,
JAVAconfig๋ XMLconfig๋ณด๋ค ์ ์ ํด๋์ค๋ก ๋ ํจ์จ์ ์ธ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐ. ํ์ง๋ง MenuMapper ์ธํฐํ์ด์ค์ ์์ฑํ ๊ฒ๊ณผ ๊ฐ์ด ๊ต์ฅํ ๋ฒ๊ฑฐ๋กญ๊ฒ ์ฟผ๋ฆฌ๋ฌธ์ ๊ฐํํ๋ฉฐ ์์ฑํด์ผํ๋ค๋ ๋จ์ ์กด์ฌ.
๋ฐ๋ผ์, AVAconfig ๋ฐฉ์์ XMLconfig์ menu-mapper.xmlํ์ผ์ ์ฐ๋์์ผ ๋ ๋ฐฉ์์ ์ฅ์ ๋ง์ ์ฌ์ฉํ ๋ฐฉ์์ ์ผ์ปซ์
๐โ ์ ๊น ! ์ด๋ป๊ฒ ์ฐ๋์ ์ํค๋์ ? ๐ฅ์ค์๐ฅ
[1] ํด๋น ํ๋ก์ ํธ์ mappers ๋ผ๋ ์์คํด๋๋ฅผ ์์ฑํ๊ณ ํ์์ com.greedy.section03.remix ํจํค์ง ์์ฑ
[2] .xmlconfig์ menu-mapper.xml๋ฅผ ํจํค์ง ์์ ๋ณต์ฌ
[3] MenuMapper ์ธํฐํ์ด์ค์ ์ฐ๋์ํค๊ธฐ ์ํด menu-mapper.xmlํ์ผ์ ์ด๋ฆ์ MenuMapper.xml๋ก ๋ณ๊ฒฝ
[4] MenuMapper.xml์ namespace๋ฅผ "com.greedy.section03.remix.MenuMapper"๋ก ์ค์ (๊ฐ์ ๊ฒฝ๋ก)
[5] .xmlconfig์์ ๋ณต์ฌํด์จ ํ์ผ์ด๋ฏ๋ก ๋ชจ๋ ๋ถ๋ถ์ section03.remix๋ก ์์ (ex : "com.greedy.section03.remix.MenuDTO")
๐ ์ ๊ณผ์ ์ ๋ชจ๋ ์คํํด์ฃผ๋ฉด MenuMapper์ ํ๋ค๊ฒ ์ฟผ๋ฆฌ๋ฌธ์ ๊ฐํํด๊ฐ๋ฉฐ ์์ฑํ ํ์์์ด .xml ํ์ผ์ ์ฐธ์กฐํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅ
* JAVAconfig ๋ฐฉ์์ Template์ ๋์ผ *
* XMLconfig ๋ฐฉ์ ๋ฐ JAVAconfig ๋ฐฉ์์ MenuDTO์ ๋์ผ *
* XMLconfig ๋ฐฉ์ ๋ฐ JAVAconfig ๋ฐฉ์์ Application์ ๋์ผ *
* XMLconfig ๋ฐฉ์ ๋ฐ JAVAconfig ๋ฐฉ์์ MenuController์ ๋์ผ *
* JAVAconfig ๋ฐฉ์์ MenuService์ ๋์ผ *
public interface MenuMapper {
/* ์๋ ๊ตฌ๊ตฌ์ ์ ์ฟผ๋ฆฌ๋ฌธ์ ์ด๋ ต๊ฒ ์์ฑํด์ผํ์ผ๋, ๋ฉ์๋๋ง ์์ฑ */
/* 1. ๋ฉ๋ด ์ ์ฒด ์กฐํ */
List<MenuDTO> selectAllMenu();
/* 2. ๋ฉ๋ด ์ฝ๋๋ก ๋ฉ๋ด ์กฐํ */
MenuDTO selectMenuByCode(int code);
/* 3. ์ ๊ท ๋ฉ๋ด ์ถ๊ฐ */
int insertMenu(MenuDTO menu);
/* 4. ๋ฉ๋ด ์์ */
int updateMenu(MenuDTO menu);
/* 5. ๋ฉ๋ด ์ญ์ */
int deleteMenu(int code);
}
๐โโ๏ธ ๋ฐ๋์ ๊ฒฝ๋ก๊ฐ MenuMapper ์ธํฐํ์ด์ค์ ์ผ์นํ๋๋ก ๋ณ๊ฒฝ
(com.greedy.section03.remix.@@@)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
mapper๋ฅผ DAO ์ธํฐํ์ด์ค์ ๋์ผํ ํจํค์ง์ ๋๊ณ DAO ์ธํฐํ์ด์ค์ ํ๋ค์์ namespace๋ก ์ง์ ํ๋ค.
๋ํ ๋งคํผ ์ธํฐํ์ด์ค์ XML์ ์ด๋ฆ์ด ๋์ผํด์ผํจ
์์ฑํ ๋ฉ์๋์ ์ด๋ฆ๊ณผ id๊ฐ ์ผ์นํ๋ฉฐ ๋ฆฌํด ํ์
์ด ์ผ์นํ๋ ์ฟผ๋ฆฌ๋ฌธ์ ๋ชจ๋ ์์ฑํด์ผํจ
-->
<mapper namespace="com.greedy.section03.remix.MenuMapper">
<!-- ์กฐํํ ์ปฌ๋ผ๊ณผ DTO๋ฅผ ๋งคํ(์ฐ๊ฒฐ)์ํค๊ธฐ ์ํ ์ค์ ์ผ๋ก resultMap์ ์ ์ธ -->
<resultMap type="com.greedy.section03.remix.MenuDTO" id="menuResultMap">
<id property="menuCode" column="MENU_CODE"/>
<result property="menuName" column="MENU_NAME"/>
<result property="menuPrice" column="MENU_PRICE"/>
<result property="categoryCode" column="CATEGORY_CODE"/>
<result property="orderableStatus" column="ORDERABLE_STATUS"/>
</resultMap>
<select id="selectAllMenu" resultMap="menuResultMap">
SELECT
MENU_CODE
, MENU_NAME
, MENU_PRICE
, CATEGORY_CODE
, ORDERABLE_STATUS
FROM TBL_MENU
WHERE ORDERABLE_STATUS = 'Y'
ORDER BY MENU_CODE
</select>
<select id="selectMenuByCode" parameterType= "_int" resultMap="menuResultMap">
SELECT
MENU_CODE
, MENU_NAME
, MENU_PRICE
, CATEGORY_CODE
, ORDERABLE_STATUS
FROM TBL_MENU
WHERE ORDERABLE_STATUS = 'Y'
AND MENU_CODE = #{code}
</select>
<insert id="insertMenu" parameterType="com.greedy.section03.remix.MenuDTO">
INSERT
INTO TBL_MENU
(
MENU_CODE
, MENU_NAME
, MENU_PRICE
, CATEGORY_CODE
, ORDERABLE_STATUS
)
VALUES
(
SEQ_MENU_CODE.NEXTVAL
, #{ menuName }
, #{ menuPrice }
, #{ categoryCode }
, 'Y'
)
</insert>
<update id="updateMenu" parameterType="com.greedy.section03.remix.MenuDTO">
UPDATE
TBL_MENU
SET MENU_NAME = #{ menuName }
, MENU_PRICE = #{ menuPrice }
, CATEGORY_CODE = #{ categoryCode }
WHERE MENU_CODE = #{ menuCode }
</update>
<delete id="deleteMenu" parameterType="_int">
DELETE
FROM TBL_MENU
WHERE MENU_CODE = #{ menuCode }
</delete>
</mapper>
* XMLconfig ๋ฐฉ์ ๋ฐ JAVAconfig ๋ฐฉ์์ PrintResult์ ๋์ผ *