SpringBoot - 4. 메뉴 조회, 추가 실습(타임리프 사용)

SpringBoot - 4. 메뉴 조회, 추가 실습(타임리프 사용)

이번에 실습해볼 주제는 메뉴를 조회하고 신규 메뉴를 추가하는 간단하게 진행할예정입니다.

스프링 부트에서 jsp를 사용해야할려면 설정을 또해줘야해서

더 깔끔하고 나름 편리한 ? 타임리프를 써보겠습니다.

타임리프는 기본 html에 타임리프 선언만 추가후 사용하면된다.

사용하기전에 스프링부트만들때 타임리프 주입을 해줘야 사용이 가능하다.

실습에 사용된 테이블은 다음과 같다.

TBL_MENU

TBL_CATEGORY

1. 메뉴 목록 조회하기

순서는 mapper > service > controller > list.html 순으로 작성된다.

Mapper.xml

SELECT MENU_CODE , MENU_NAME , MENU_PRICE , CATEGORY_CODE , ORDERABLE_STATUS FROM TBL_MENU WHERE ORDERABLE_STATUS = 'Y' ORDER BY MENU_CODE

MenuMapper.interface

package com.memory.project.menu.model.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Mapper public interface MenuMapper { List findAllList(); }

MenuService.interface

코드 첨부 생략

MenuServiceImpl.class

package com.memory.project.menu.model.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.memory.project.menu.model.dao.MenuMapper; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Service("menuService") public class MenuServiceImpl implements MenuService{ private MenuMapper menuMapper; @Autowired public MenuServiceImpl(MenuMapper menuMapper) { this.menuMapper = menuMapper; } @Override public List findAllList() { return menuMapper.findAllList(); } }

MenuController.java

package com.memory.project.menu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.memory.project.menu.model.service.MenuService; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Controller @RequestMapping("/menu") public class MenuController { @Autowired private MenuService menuService; @GetMapping("/list") public ModelAndView findAllList(ModelAndView mv) { List menuList = menuService.findAllList(); mv.addObject("menuList", menuList); mv.setViewName("menu/list"); return mv; } }

list.html

기존 jsp와 사용법은 비슷하지만 .

html태그에 타임리프 선언하고 태그에 th: ~~~으로작성해야한다.

list 메뉴 번호 메뉴 이름 메뉴 가격 카테고리 번호 판매 상태

2. 신규 메뉴 추가 view 페이지

우선 화면만 보여주는 컨트롤러 우선 작성하였습니다.

MenuController.java

package com.memory.project.menu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.memory.project.menu.model.service.MenuService; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Controller @RequestMapping("/menu") public class MenuController { @Autowired private MenuService menuService; @GetMapping("/regist") public void getRegist() {} }

regist.html

regist 신규 메뉴 등록 메뉴 이름 : 메뉴 가격 : 카테고리 : 판매 상태 : Y N 전송

원래 view페이지만 작업하면 카테고리가 안보여야 정상입니다.

현재 저는 에이젝스 이용해 화면에 뿌려서 정상적으로 보이게되는것입니다.

3. 신규 메뉴 페이지 카테고리 ajax로 추가하기

MenuMapper.xml

SELECT CATEGORY_CODE , CATEGORY_NAME , REF_CATEGORY_CODE FROM TBL_CATEGORY

MenuMapper.interface

코드 생략

MenuService.interface

코드 생략

MenuServiceImpl.java

package com.memory.project.menu.model.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.memory.project.menu.model.dao.MenuMapper; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Service("menuService") public class MenuServiceImpl implements MenuService{ private MenuMapper menuMapper; @Autowired public MenuServiceImpl(MenuMapper menuMapper) { this.menuMapper = menuMapper; } @Override public List findAllCategory() { return menuMapper.findAllCategory(); } }

MenuController.java

package com.memory.project.menu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.memory.project.menu.model.service.MenuService; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Controller @RequestMapping("/menu") public class MenuController { @Autowired private MenuService menuService; @GetMapping("/category") @ResponseBody public List categoryList(){ List categoryList = menuService.findAllCategory(); return categoryList; } }

regist.html

regist 신규 메뉴 등록 메뉴 이름 : 메뉴 가격 : 카테고리 : 판매 상태 : Y N $(function(){ $.ajax({ url : "/menu/category", type : "get", success : function(data){ const categoryCode = $("#categoryCode"); for(let index in data){ categoryCode.append($("

4. 신규 메뉴 추가후 리스트 페이지로 이동하기

INSERT INTO TBL_MENU ( MENU_CODE , MENU_NAME , MENU_PRICE , CATEGORY_CODE , ORDERABLE_STATUS ) VALUES ( SEQ_MENU_CODE.NEXTVAL , #{name} , #{price} , #{categoryCode} , #{orderableStatus} )

MenuMapper.interface

코드 생략

MenuService.interface

코드 생략

MenuServiceImpl.java

package com.memory.project.menu.model.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.memory.project.menu.model.dao.MenuMapper; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Service("menuService") public class MenuServiceImpl implements MenuService{ private MenuMapper menuMapper; @Autowired public MenuServiceImpl(MenuMapper menuMapper) { this.menuMapper = menuMapper; } @Override @Transactional public int insertMenu(Menu menu) { return menuMapper.insertMenu(menu); } }

MenuController.java

package com.memory.project.menu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.memory.project.menu.model.service.MenuService; import com.memory.project.menu.model.vo.Category; import com.memory.project.menu.model.vo.Menu; @Controller @RequestMapping("/menu") public class MenuController { @Autowired private MenuService menuService; @PostMapping("/regist") public String postRegist(@ModelAttribute Menu menu, RedirectAttributes rttr) { int result = menuService.insertMenu(menu); if(result > 0) { rttr.addFlashAttribute("message", "success"); }else { rttr.addFlashAttribute("message", "fail"); } return "redirect:/menu/list"; } }

728x90

from http://memory-develo.tistory.com/152 by ccl(A) rewrite - 2021-11-04 18:01:32