๐โ Spring์์ JPA๋ฅผ ํ์ฉํด๋ณด์ !
# server port config
server:
port: 8001
# DB config
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:xe
username: C##GREEDY
password: GREEDY
# JPA config
jpa:
generate-ddl: false # create table์ ํ ๋๋ง true(๊ธฐ๋ณธ๊ฐ)
show-sql: true # JPA๊ฐ ์ํํ๋ SQL๊ตฌ๋ฌธ์ ์ฝ์์์ ํ์ธ ๊ฐ๋ฅ
database: oracle
properties:
hibernate:
'[format_sql]': true # SQL์ ๊ฐํํ์ฌ ๋ณด๊ธฐ์ข๊ฒ ์ถ๋ ฅ
<!-- Menu(์์์ฑ ๊ฐ์ฒด) -> MenuDTO(๋น์์์ฑ ๊ฐ์ฒด)๋ก ๋ณํํ ์ ์๋ ModelMapper ๋ผ์ด๋ธ๋ฌ๋ฆฌ -->
<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>
@Configuration
public class BeanConfiguration {
/* ModelMapper๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ ํ, ModelMapper๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด Bean ๋ฑ๋ก */
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
<h1 align="center">๐๐ฉ GREEDY RESTAURANT์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋น! ๐ฉ๐พ</h1>
<div align="center">
<button onclick="location.href='/menu/81'">81๋ฒ ๋ฉ๋ด ๋ณด๊ธฐ</button>
<button onclick="location.href='/menu/list'">๋ฉ๋ดํ ๋ณด๊ธฐ</button>
<button onclick="location.href='/menu/regist'">๋ฉ๋ด ๋ฑ๋กํ๊ธฐ</button>
<button onclick="location.href='/menu/modify'">๋ฉ๋ด ์์ ํ๊ธฐ</button>
<button onclick="location.href='/menu/remove'">๋ฉ๋ด ์ญ์ ํ๊ธฐ</button>
<button onclick="searchMenu()">๋ฉ๋ด ๊ฒ์ํ๊ธฐ</button>
<button onclick="location.href='/menu/recommend'">์ค๋์ ๋ฉ๋ด ์ถ์ฒ๋ฐ๊ธฐ</button>
</div>
<script>
/* ์
๋ ฅ๋ ํค์๋๋ก ๊ฒ์ํ ๊ฒฐ๊ณผ๋ฅผ ํ๋ฉด์ ์กฐํ์ํค๊ธฐ */
function searchMenu() {
const keyword = prompt('ํค์๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ ๐ฅณ')
if(keyword !== null && keyword.trim().length !== 0) {
console.log(keyword);
window.location.href = "/menu/search?keyword=" + keyword;
} else {
alert('ํค์๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ ๐คฅ')
}
}
</script>
<h1 align="center">โจ ๋ฉ๋ด โจ</h1>
<table align="center" border="1">
<tr>
<th>๋ฉ๋ด๋ฒํธ</th>
<th>๋ฉ๋ด๋ช
</th>
<th>๋ฉ๋ด๊ฐ๊ฒฉ</th>
<th>์นดํ
๊ณ ๋ฆฌ์ฝ๋</th>
<th>ํ๋งค์ํ</th>
</tr>
<tr>
<td th:text="${ menu.menuCode }"></td>
<td th:text="${ menu.menuName }"></td>
<td th:text="${ menu.menuPrice }"></td>
<td th:text="${ menu.categoryCode }"></td>
<td th:text="${ menu.orderableStatus }"></td>
</tr>
</table>
<script>
/* ๋ฉ๋ด ์์ ํ, ํด๋น ํ์ด์ง๋ก redirect ๋๊ธฐ ์ alert ๋์ฐ๊ธฐ */
if(window.location.hash === '#menu-modify'){
alert('[[${message}]]'); // message ๋ณ์๋ ์ปจํธ๋กค๋ฌ์์ ์ ๋ฌํ Flash Attribute
}
</script>
<h1 align="center">โจ ๋ฉ๋ดํ โจ</h1>
<table align="center" border="1">
<tr>
<th>๋ฉ๋ด๋ฒํธ</th>
<th>๋ฉ๋ด๋ช
</th>
<th>๋ฉ๋ด๊ฐ๊ฒฉ</th>
<th>์นดํ
๊ณ ๋ฆฌ์ฝ๋</th>
<th>ํ๋งค์ํ</th>
</tr>
<tr th:each="menu : ${ menuList }">
<td th:text="${ menu.menuCode }"></td>
<td th:text="${ menu.menuName }"></td>
<td th:text="${ menu.menuPrice }"></td>
<td th:text="${ menu.categoryCode }"></td>
<td th:text="${ menu.orderableStatus }"></td>
</tr>
</table>
<script>
/* ์ ๊ท ๋ฉ๋ด ๋ฑ๋ก or ๋ฉ๋ด ์ญ์ ํ, ํด๋น ํ์ด์ง๋ก redirect ๋๊ธฐ ์ alert ๋์ฐ๊ธฐ */
if(window.location.hash === '#menu-regist' || window.location.hash === '#menu-remove'){
alert('[[${message}]]'); // message ๋ณ์๋ ์ปจํธ๋กค๋ฌ์์ ์ ๋ฌํ Flash Attribute
}
</script>
<h3 align="center">์ ๊ท ๋ฉ๋ด ๋ฑ๋ก</h3>
<form align="center" action="/menu/regist" method="post">
<label>๋ฉ๋ด ์ด๋ฆ : </label><input type="text" name="menuName"><br>
<label>๋ฉ๋ด ๊ฐ๊ฒฉ : </label><input type="number" name="menuPrice"><br>
<label>์นดํ
๊ณ ๋ฆฌ : </label>
<select name="categoryCode" id="categoryCode">
<!--
์นดํ
๊ณ ๋ฆฌ ๋ด์ฉ์ด ๋ณํํ ์ ์๋ค๋ฉด ์กฐํ ํด์ ์ฒ๋ฆฌํด์ผ ํ๋ฉฐ
์ฌ๊ธฐ์์๋ ajax๋ก ์ฒ๋ฆฌ
-->
</select><br>
<label>ํ๋งค ์ํ : </label>
<select name="orderableStatus">
<option value="Y">ํ๋งค๊ฐ๋ฅ</option>
<option value="N">ํ๋งค๋ถ๊ฐ</option>
</select><br>
<input type="submit" value="์ ์ก">
</form>
<script>
/* ๋น๋๊ธฐ ํต์ (ajax)์ ํตํด ๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ ๋ฆฌ์คํธ ์กฐํ */
$(function(){
$.ajax({
url : '/menu/category',
success : function(data) {
console.log(data);
let html = '';
/* data๋ผ๋ ์กฐํ๋ ๋ฐฐ์ด์ ๋ฐ๋ณตํ์ฌ ํฉ์ฐ์ํค๊ธฐ */
for(let index in data) {
html += `<option value='${data[index].categoryCode}'>${data[index].categoryName}</option>`;
// ์กฐํ๋ ์นดํ
๊ณ ๋ฆฌ์ ์ด๋ฆ์ ํ๋ฉด์ ๋
ธ์ถ์ํค๊ณ ๊ทธ ์นดํ
๊ณ ๋ฆฌ๊ฐ ์ ํ(<option>)๋๋ฉด value๋ ํด๋น ์นดํ
๊ณ ๋ฆฌ์ ์ฝ๋
}
/* ๊ทธ ๊ฐ์ <select>์ insertํ์ฌ ํ๋ฉด์ ๋
ธ์ถ๋๋๋ก ํจ */
document.querySelector("#categoryCode").insertAdjacentHTML('beforeend', html);
},
error : function(xhr) {
console.log(xhr);
}
});
})
</script>
<h3 align="center">๋ฉ๋ด ์์ </h3>
<form align="center" action="/menu/modify" method="post">
<label>์์ ํ ๋ฉ๋ด : </label>
<select name="menuCode" id="menuCode"></select><br>
<label>์๋ก์ด ๋ฉ๋ด ์ด๋ฆ : </label>
<input type="text" name="menuName"><br>
<label>์๋ก์ด ๋ฉ๋ด ๊ฐ๊ฒฉ : </label>
<input type="number" name="menuPrice"><br>
<label>์๋ก์ด ๋ฉ๋ด์ ์นดํ
๊ณ ๋ฆฌ : </label>
<select name="categoryCode" id="categoryCode"></select><br>
<label>ํ๋งค ์ํ : </label>
<select name="orderableStatus">
<option value="Y">ํ๋งค๊ฐ๋ฅ</option>
<option value="N">ํ๋งค๋ถ๊ฐ</option>
</select><br>
<input type="submit" value="์ ์ก">
</form>
<script>
/* ๋น๋๊ธฐ ํต์ (ajax)์ ํตํด ๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ ๋ฆฌ์คํธ ์กฐํ */
$(function(){
$.ajax({
url : '/menu/category',
success : function(data) {
console.log(data);
let html = '';
/* data๋ผ๋ ์กฐํ๋ ๋ฐฐ์ด์ ๋ฐ๋ณตํ์ฌ ํฉ์ฐ์ํค๊ธฐ */
for(let index in data) {
html += `<option value='${data[index].categoryCode}'>${data[index].categoryName}</option>`;
// ์กฐํ๋ ์นดํ
๊ณ ๋ฆฌ์ ์ด๋ฆ์ ํ๋ฉด์ ๋
ธ์ถ์ํค๊ณ ๊ทธ ์นดํ
๊ณ ๋ฆฌ๊ฐ ์ ํ(<option>)๋๋ฉด value๋ ํด๋น ์นดํ
๊ณ ๋ฆฌ์ ์ฝ๋
}
/* ๊ทธ ๊ฐ์ <select>์ insertํ์ฌ ํ๋ฉด์ ๋
ธ์ถ๋๋๋ก ํจ */
document.querySelector("#categoryCode").insertAdjacentHTML('beforeend', html);
},
error : function(xhr) {
console.log(xhr);
}
});
})
/* ๋น๋๊ธฐ ํต์ (ajax)์ ํตํด ๋ชจ๋ ๋ฉ๋ด ๋ฆฌ์คํธ ์กฐํ */
$(function(){
$.ajax({
url : '/menu/menu',
success : function(data) {
console.log(data);
let html = '';
/* data๋ผ๋ ์กฐํ๋ ๋ฐฐ์ด์ ๋ฐ๋ณตํ์ฌ ํฉ์ฐ์ํค๊ธฐ */
for(let index in data) {
html += `<option value='${data[index].menuCode}'>[${data[index].menuCode}] ${data[index].menuName}</option>`;
// ์กฐํ๋ ๋ฉ๋ด์ฝ๋์ ์ด๋ฆ์ ํ๋ฉด์ ๋
ธ์ถ์ํค๊ณ ๊ทธ ๋ฉ๋ด๊ฐ ์ ํ(<option>)๋๋ฉด value๋ ํด๋น ๋ฉ๋ด์ ์ฝ๋
}
/* ๊ทธ ๊ฐ์ <select>์ insertํ์ฌ ํ๋ฉด์ ๋
ธ์ถ๋๋๋ก ํจ */
document.querySelector("#menuCode").insertAdjacentHTML('beforeend', html);
},
error : function(xhr) {
console.log(xhr);
}
});
})
</script>
<h3 align="center">๋ฉ๋ด ์ญ์ </h3>
<form align="center" action="/menu/remove" method="post">
<label>์ญ์ ํ ๋ฉ๋ด : </label>
<select name="menuCode" id="menuCode"></select><br>
<input type="submit" value="์ ์ก">
</form>
<script>
/* ๋น๋๊ธฐ ํต์ (ajax)์ ํตํด ๋ชจ๋ ๋ฉ๋ด ๋ฆฌ์คํธ ์กฐํ */
$(function(){
$.ajax({
url : '/menu/menu',
success : function(data) {
console.log(data);
let html = '';
/* data๋ผ๋ ์กฐํ๋ ๋ฐฐ์ด์ ๋ฐ๋ณตํ์ฌ ํฉ์ฐ์ํค๊ธฐ */
for(let index in data) {
html += `<option value='${data[index].menuCode}'>${data[index].menuName}</option>`;
// ์กฐํ๋ ๋ฉ๋ด์ ์ด๋ฆ์ ํ๋ฉด์ ๋
ธ์ถ์ํค๊ณ ๊ทธ ๋ฉ๋ด๊ฐ ์ ํ(<option>)๋๋ฉด value๋ ํด๋น ๋ฉ๋ด์ ์ฝ๋
}
/* ๊ทธ ๊ฐ์ <select>์ insertํ์ฌ ํ๋ฉด์ ๋
ธ์ถ๋๋๋ก ํจ */
document.querySelector("#menuCode").insertAdjacentHTML('beforeend', html);
},
error : function(xhr) {
console.log(xhr);
}
});
})
</script>
@Entity
@Table(name="TBL_CATEGORY")
public class Category {
@Id
@Column(name="CATEGORY_CODE")
private int categoryCode;
@Column(name="CATEGORY_NAME")
private String categoryName;
@Column(name="REF_CATEGORY_CODE")
private Integer refCategoryCode;
/* ๊ธฐ๋ณธ ์
ํ
*/
}
@Entity
@Table(name="TBL_MENU")
@SequenceGenerator(
name="SEQ_MENU_CODE_GENERATOR",
sequenceName="SEQ_MENU_CODE",
initialValue=100,
allocationSize=1
)
public class Menu {
@Id
@Column(name="MENU_CODE")
@GeneratedValue(
strategy=GenerationType.SEQUENCE,
generator="SEQ_MENU_CODE_GENERATOR"
)
private int menuCode;
@Column(name="MENU_NAME")
private String menuName;
@Column(name="MENU_PRICE")
private int menuPrice;
@Column(name="CATEGORY_CODE")
private int categoryCode;
@Column(name="ORDERABLE_STATUS")
private String orderableStatus;
/* ๊ธฐ๋ณธ ์
ํ
*/
}
public class CategoryDTO {
private int categoryCode;
private String categoryName;
private Integer refCategoryCode;
/* ๊ธฐ๋ณธ ์
ํ
*/
}
public class MenuDTO {
private int menuCode;
private String menuName;
private int menuPrice;
private int categoryCode;
private String orderableStatus;
/* ๊ธฐ๋ณธ ์
ํ
*/
}
@Controller
public class MainController {
@GetMapping(value = {"/", "/main"})
public String main() {
return "main/main";
}
}
@PathVariable
@ResponseBody
RedirectAttributes
addFlashAttribute()
shuffle()
subList()
@Controller
@RequestMapping("/menu")
public class MenuController {
private MenuService menuService;
public MenuController(MenuService menuService) {
this.menuService = menuService;
}
/* ๋ฉ๋ด ์ฝ๋๋ก ํด๋น ๋ฉ๋ด ์กฐํํ๊ธฐ */
@GetMapping("/{menuCode}")
public String findMenuByCode(@PathVariable int menuCode, Model model) {
MenuDTO menu = menuService.findMenuByCode(menuCode);
// System.out.println("menu : " + menu);
model.addAttribute("menu", menu);
return "menu/one";
}
/* ๋ชจ๋ ๋ฉ๋ด ๋ฆฌ์คํธ ์กฐํํ๊ธฐ */
@GetMapping("/list")
public String findAllMenu(Model model) {
List<MenuDTO> menuList = menuService.findAllMenu();
model.addAttribute("menuList", menuList);
return "menu/list";
}
/* '๋ฉ๋ด ๋ฑ๋กํ๊ธฐ'๋ฅผ ์ํ ํ๋ฉด ์ ํ */
@GetMapping("/regist")
public void registPage() {}
/* ์นดํ
๊ณ ๋ฆฌ ๋ฆฌ์คํธ๋ฅผ ์กฐํํ๋ ๋น๋๊ธฐ ํต์ (ajax) */
@GetMapping(value="category", produces="application/json; charset=UTF-8")
@ResponseBody /* ๋ฐํํ๋ ๊ฐ์ด ๊ณง ์๋ต๋๋ ๊ฐ์ ๋ฐ๋ */
public List<CategoryDTO> findCategoryList() {
return menuService.findAllCategory();
}
/* ๋ฉ๋ด ๋ฑ๋กํ๊ธฐ */
@PostMapping("/regist")
public String registNewMenu(@ModelAttribute MenuDTO newMenu, RedirectAttributes rttr) { /* @ModelAttribute๋ ์๋ต๋์ด๋ ์ ์ ๋์ํ์ง๋ง ๋ช
์ */
menuService.registNewMenu(newMenu);
rttr.addFlashAttribute("message", "๋ฉ๋ด ๋ฑ๋ก ์ฑ๊ณต! ๋ฑ๋ก๋ ๋ฉ๋ด๋ฅผ ํ์ธํ์ธ์ ๐ฅณ");
return "redirect:/menu/list#menu-regist";
}
/* ๋ฉ๋ด ์์ ํ๊ธฐ */
@GetMapping("/modify")
public void modifyPage() {}
@PostMapping("/modify")
public String menuModify(@ModelAttribute MenuDTO menu, RedirectAttributes rttr) {
menuService.modifyMenu(menu);
rttr.addFlashAttribute("message", "๋ฉ๋ด ์์ ์ฑ๊ณต! ์์ ๋ ๋ฉ๋ด๋ฅผ ํ์ธํ์ธ์ ๐");
/* ์์ ๋ ๋ฉ๋ด์ ์์ธํ์ด์ง๋ก ์ด๋ */
return "redirect:/menu/" + menu.getMenuCode() + "#menu-modify";
}
/* ๋ฉ๋ด ์ญ์ ํ๊ธฐ */
@GetMapping("/remove")
public void removePage() {}
/* ๋ฉ๋ด ๋ฆฌ์คํธ๋ฅผ ์กฐํํ๋ ๋น๋๊ธฐ ํต์ (ajax) */
@GetMapping(value="menu", produces="application/json; charset=UTF-8")
@ResponseBody /* ๋ฐํํ๋ ๊ฐ์ด ๊ณง ์๋ต๋๋ ๊ฐ์ ๋ฐ๋ */
public List<MenuDTO> findMenuList() {
return menuService.findAllMenu();
}
@PostMapping("/remove")
public String menuRemove(@ModelAttribute MenuDTO menu, RedirectAttributes rttr) {
menuService.removeMenu(menu);
rttr.addFlashAttribute("message", "๋ฉ๋ด ์ญ์ ์ฑ๊ณต! ๐");
return "redirect:/menu/list#menu-remove";
}
/* ๋ฉ๋ด ๊ฒ์ํ๊ธฐ */
@GetMapping("/search")
public String searchedPage(@RequestParam(value="keyword", required=false) String keyword, Model model) {
List<MenuDTO> menuList = menuService.searchMenu(keyword);
model.addAttribute("menuList", menuList);
return "/menu/list";
}
/* ์ค๋์ ๋ฉ๋ด ์ถ์ฒ๋ฐ๊ธฐ */
@GetMapping("/recommend")
public String recommendPage(Model model) {
List<MenuDTO> menuList = menuService.findAllMenu();
Collections.shuffle(menuList); // shuffle() ๋ฉ์๋๋ก menuList ์์ ์๋ ๊ฐ ๋๋ค์ผ๋ก ์ฌ๋ฐฐ์น
List<MenuDTO> recommendedMenulist = menuList.subList(0, 5); // subList() ๋ฉ์๋๋ก 0๋ถํฐ 4๋ฒ ์ธ๋ฑ์ค๊น์ง๋ง ์ถ์ถ
model.addAttribute("menuList", recommendedMenulist);
return "/menu/list";
}
}
@PersistenceContext
: ์คํ๋ง ๋ถํธ๊ฐ ์์์ฑ ์ปจํ
์คํธ๋ฅผ ๊ด๋ฆฌํ๋ ์ํฐํฐ ๋งค๋์ ๋ฅผ ์ฃผ์
ํ๋ ์ด๋
ธํ
์ด์
modelMapper.map()
stream().map()
collect(Collectors.toList())
@Service
public class MenuService {
private MenuRepository menuRepository; // MenuRepository๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํด ํ๋ ์ ์ธ ๋ฐ ์์ฑ์๋ฅผ ํตํ ์์กด์ฑ ์ฃผ์
private ModelMapper modelMapper; // ModelMapper๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํด ํ๋ ์ ์ธ ๋ฐ ์์ฑ์๋ฅผ ํตํ ์์กด์ฑ ์ฃผ์
@PersistenceContext // ์คํ๋ง ๋ถํธ๊ฐ ์์์ฑ ์ปจํ
์คํธ๋ฅผ ๊ด๋ฆฌํ๋ ์ํฐํฐ ๋งค๋์ ๋ฅผ ์ฃผ์
ํ๋ ์ด๋
ธํ
์ด์
private EntityManager entityManager;
public MenuService(MenuRepository menuRepository, ModelMapper modelMapper) {
this.menuRepository = menuRepository;
this.modelMapper = modelMapper;
}
/* ์์์ฑ ๊ฐ์ฒด(์ํฐํฐ)๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ๊ฐ ํผ์ ๋ ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ฏ๋ก ๋น์์ ๊ฐ์ฒด๋ก ๋ณ๊ฒฝํ์ฌ ๋ฐํ */
/* MenuDTO : ์ํฐํฐ๊ฐ ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ฌถ์ด์ ๋ณด๋ผ ์ ์๋ ๊ฐ์ฒด */
/* ๋ฉ๋ด ์ฝ๋๋ก ํด๋น ๋ฉ๋ด ์กฐํํ๊ธฐ */
public MenuDTO findMenuByCode(int menuCode) {
/* Menu -> MenuDTO(๋น์์ ๊ฐ์ฒด)๋ก ๋ณํํ ์ ์๋ ModelMapper ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์กด์ฑ ์ถ๊ฐ ํ ์ฌ์ฉ */
return modelMapper.map(menuRepository.findMenuByCode(entityManager, menuCode), MenuDTO.class);
// modelMapper.map(๋ณํํ๊ณ ์ํ๋ ๋์์ ์ํฐํฐ ๊ฐ์ฒด, ๋ณํํ๊ณ ์ํ๋ ํ์
);
}
/* ๋ชจ๋ ๋ฉ๋ด ๋ฆฌ์คํธ ์กฐํํ๊ธฐ */
public List<MenuDTO> findAllMenu() {
/* List<Menu> Menu ๊ฐ์ฒด๊ฐ ์ฌ๋ฌ ๊ฐ ์์ผ๋ฏ๋ก Streamํ ์์ผ MenuDTO */
List<Menu> menuList = menuRepository.findAllMenu(entityManager);
return menuList.stream().map(menu -> modelMapper.map(menu, MenuDTO.class)).collect(Collectors.toList());
/* menu : Menu ์ํฐํฐ ๊ฐ์ฒด๋ค
* stream์ map()์ ๊ดํธ ์์ ๋์์ ํ๋ํ๋ ์ํํ๊ณ ๋์ ๊ฐ๊ณต๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
* collect(Collectors.toList() : ๋ค์ stream์ List ํํ๋ก ๋ณํ */
/*
* [์ฝ๋ ํด์]
* 1. menuList๋ผ๋ List<Menu> ํ์
์ streamํ ํ์ฌ map() ์ฌ์ฉ
* 2. menu๋ผ๋ Menu ์ํฐํฐ ๊ฐ์ฒด๋ค์ modelMapper์ map()๋ฉ์๋๋ฅผ ํตํด MenuDTO ํ์
์ผ๋ก ๋ณํ
* 3. ๋ค์ stream์ Listํํ๋ก ๋ณํ
*/
}
/* ๋ชจ๋ ์นดํ
๊ณ ๋ฆฌ ๋ฆฌ์คํธ ์กฐํํ๊ธฐ */
public List<CategoryDTO> findAllCategory() {
List<Category> categoryList = menuRepository.findAllCategory(entityManager);
return categoryList.stream().map(category -> modelMapper.map(category, CategoryDTO.class)).collect(Collectors.toList());
}
/* ์คํ๋ง์์๋ ํธ๋์ญ์
์ฒ๋ฆฌ๋ฅผ ์ง์
* ์ด๋
ธํ
์ด์
์ผ๋ก @Transactional์ ์ ์ธํ๋ ์ ์ธ์ ํธ๋์ญ์
์ด ๋ณดํธ์ ์ธ ๋ฐฉ์
* ํด๋์ค ๋ ๋ฒจ๊ณผ ๋ฉ์๋ ๋ ๋ฒจ์ ์์ฑ๋ ์ ์๊ณ ํด๋์ค ๋ ๋ฒจ์ ์์ฑ ์ ํ์ ๋ชจ๋ ๋ฉ์๋์ ์ ์ฉ
* ์ด๋
ธํ
์ด์
์ด ์ ์ธ๋๋ฉด ๋ฉ์๋ ํธ์ถ ์ ์๋์ผ๋ก ํ๋ก์ ๊ฐ์ฒด(๊ฐ๋ก์ฑ๋ ๊ฐ์ฒด)๊ฐ ์์ฑ๋๋ฉฐ ํด๋น ํ๋ก์ ๊ฐ์ฒด๋ ์ ์ ์ํ ์ฌ๋ถ์ ๋ฐ๋ผ
* commit, rollback ์ฒ๋ฆฌ๋ฅผ ํจ */
/* ๋ฉ๋ด ๋ฑ๋กํ๊ธฐ */
@Transactional
public void registNewMenu(MenuDTO newMenu) {
/* ์ด์ ๋ฐฉ์๊ณผ๋ ๋ฐ๋๋ก MenuDTO๋ฅผ Menuํํ๋ก ๋ณ๊ฒฝํ์ฌ Menuํ์
์ผ๋ก ์ ๋ฌ */
menuRepository.registNewMenu(entityManager, modelMapper.map(newMenu, Menu.class));
}
/* ๋ฉ๋ด ์์ ํ๊ธฐ */
@Transactional
public void modifyMenu(MenuDTO menu) {
menuRepository.modifyMenu(entityManager, modelMapper.map(menu, Menu.class));
}
/* ๋ฉ๋ด ์ญ์ ํ๊ธฐ */
@Transactional
public void removeMenu(MenuDTO menu) {
menuRepository.removeMenu(entityManager, modelMapper.map(menu, Menu.class));
}
/* ๋ฉ๋ด ๊ฒ์ํ๊ธฐ */
public List<MenuDTO> searchMenu(String keyword) {
List<Menu> menuList = menuRepository.searchMenu(entityManager, keyword);
return menuList.stream().map(menu -> modelMapper.map(menu, MenuDTO.class)).collect(Collectors.toList());
}
}
find()
persist()
remove()
createQuery()
getResultList()
setParameter()
@Repository
public class MenuRepository {
/* ๋ฉ๋ด ์ฝ๋๋ก ํด๋น ๋ฉ๋ด ์กฐํํ๊ธฐ */
public Menu findMenuByCode(EntityManager entityManager, int menuCode) {
/* find()๋ ํ
์ด๋ธ์ PK๋ฅผ ํตํด ํด๋น ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋์ด๋ฏ๋ก ์ฌ๊ธฐ์๋ EntityManager๋ฅผ ํ์ฉํ์ฌ ๊ฐ์ ธ์ด */
return entityManager.find(Menu.class, menuCode);
}
/* ๋ชจ๋ ๋ฉ๋ด ๋ฆฌ์คํธ ์กฐํํ๊ธฐ */
public List<Menu> findAllMenu(EntityManager entityManager) {
String jpql = "SELECT m FROM Menu AS m ORDER BY m.menuCode ASC";
return entityManager.createQuery(jpql, Menu.class).getResultList();
}
/* '๋ฉ๋ด ๋ฑ๋กํ๊ธฐ'์์ ํ์ํ ์นดํ
๊ณ ๋ฆฌ ๋ฆฌ์คํธ ์กฐํํ๊ธฐ(ajax) */
public List<Category> findAllCategory(EntityManager entityManager) {
String jpql = "SELECT c FROM Category AS c ORDER BY c.categoryCode ASC";
return entityManager.createQuery(jpql, Category.class).getResultList();
}
/* ๋ฉ๋ด ๋ฑ๋กํ๊ธฐ */
public void registNewMenu(EntityManager entityManager, Menu menu) {
/* entityManager์ผ, ์์์ฑ ์ปจํ
์คํธ์ menu๋ฅผ ์ ์ฅํ๊ณ ๊ด๋ฆฌํด์ฃผ๋ ด! */
entityManager.persist(menu);
}
/* ๋ฉ๋ด ์์ ํ๊ธฐ */
public void modifyMenu(EntityManager entityManager, Menu menu) {
/* menu : ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ menuCode, menuName, menuPrice, categoryCode, orderableStatus๊ฐ ๋์ด์ด */
System.out.println(menu);
/* ์ ๋ฌ ๋ฐ์ ๋ฉ๋ด ์ ๋ณด๋ฅผ ํตํด ํด๋น ์ํฐํฐ๋ฅผ ๋จผ์ ์กฐํ */
Menu selectedMenu = entityManager.find(Menu.class, menu.getMenuCode());
/* ์กฐํ๋ ๋ฉ๋ด ๊ฐ์ฒด๋ฅผ ์์ */
selectedMenu.setMenuName(menu.getMenuName());
selectedMenu.setMenuPrice(menu.getMenuPrice());
selectedMenu.setCategoryCode(menu.getCategoryCode());
selectedMenu.setOrderableStatus(menu.getOrderableStatus());
}
/* ๋ฉ๋ด ์ญ์ ํ๊ธฐ */
public void removeMenu(EntityManager entityManager, Menu menu) {
/* ์ ๋ฌ ๋ฐ์ ๋ฉ๋ด ์ ๋ณด๋ฅผ ํตํด ํด๋น ์ํฐํฐ๋ฅผ ๋จผ์ ์กฐํ */
Menu selectedMenu = entityManager.find(Menu.class, menu.getMenuCode());
entityManager.remove(selectedMenu);
}
/* ๋ฉ๋ด ๊ฒ์ํ๊ธฐ */
public List<Menu> searchMenu(EntityManager entityManager, String keyword) {
String jpql = "SELECT m FROM Menu AS m WHERE m.menuName LIKE :keyword ORDER BY m.menuCode ASC";
return entityManager.createQuery(jpql, Menu.class).setParameter("keyword", "%"+keyword+"%").getResultList();
}
}