이번에 실습해볼 주제는 메뉴를 조회하고 신규 메뉴를 추가하는 간단하게 진행할예정입니다.
스프링 부트에서 jsp를 사용해야할려면 설정을 또해줘야해서
더 깔끔하고 나름 편리한 ? 타임리프를 써보겠습니다.
타임리프는 기본 html에 타임리프 선언만 추가후 사용하면된다.
사용하기전에 스프링부트만들때 타임리프 주입을 해줘야 사용이 가능하다.
실습에 사용된 테이블은 다음과 같다.
TBL_MENU
TBL_CATEGORY
1. 메뉴 목록 조회하기
순서는 mapper > service > controller > list.html 순으로 작성된다.
Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.memory.project.menu.model.dao.MenuMapper">
<resultMap id="menuResultMap" type="com.memory.project.menu.model.vo.Menu">
<id property="code" column="MENU_CODE"/>
<result property="name" column="MENU_NAME"/>
<result property="price" column="MENU_PRICE"/>
<result property="categoryCode" column="CATEGORY_CODE"/>
<result property="orderableStatus" column="ORDERABLE_STATUS"/>
</resultMap>
<select id="findAllList" 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>
</mapper>
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<Menu> 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<Menu> 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<Menu> menuList = menuService.findAllList();
mv.addObject("menuList", menuList);
mv.setViewName("menu/list");
return mv;
}
}
list.html
기존 jsp와 사용법은 비슷하지만 .
html태그에 타임리프 선언하고 태그에 th: ~~~으로작성해야한다.
<!DOCTYPE html>
<html xmlns:th="http://tymelefe.org">
<head>
<meta charset="UTF-8">
<title>list</title>
</head>
<body>
<div th:replace="common/header.html"></div>
<table border="1" align="center">
<tr>
<th>메뉴 번호</th>
<th>메뉴 이름</th>
<th>메뉴 가격</th>
<th>카테고리 번호</th>
<th>판매 상태</th>
</tr>
<tr th:each="menu : ${menuList}">
<td th:text="${menu.code}"></td>
<td th:text="${menu.name}"></td>
<td th:text="${menu.price}"></td>
<td th:text="${menu.categoryCode}"></td>
<td th:text="${menu.orderableStatus}"></td>
</tr>
</table>
</body>
</html>
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
<!DOCTYPE html>
<html xmlns="http://www.tymelefe.org">
<head>
<meta charset="UTF-8">
<title>regist</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<div th:include="common/header.html"></div>
<h2 align="center">신규 메뉴 등록</h2>
<form action="/menu/regist" method="post" align="center">
메뉴 이름 : <input type="text" name="name"><br>
메뉴 가격 : <input type="number" name="price"><br>
카테고리 : <select name="categoryCode" id="categoryCode"></select> <br>
판매 상태 : <select name="orderableStatus">
<option value="Y">Y</option>
<option value="N">N</option>
</select><br>
<button type="submit">전송</button>
</form>
</body>
</html>
원래 view페이지만 작업하면 카테고리가 안보여야 정상입니다.
현재 저는 에이젝스 이용해 화면에 뿌려서 정상적으로 보이게되는것입니다.
3. 신규 메뉴 페이지 카테고리 ajax로 추가하기
MenuMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.memory.project.menu.model.dao.MenuMapper">
<resultMap id="categoryResultMap" type="com.memory.project.menu.model.vo.Category">
<id property="code" column="CATEGORY_CODE"/>
<result property="name" column="CATEGORY_NAME" />
<result property="refCategoryCode" column="REF_CATEGORY_CODE"/>
</resultMap>
<select id="findAllCategory" resultMap="categoryResultMap">
SELECT
CATEGORY_CODE
, CATEGORY_NAME
, REF_CATEGORY_CODE
FROM TBL_CATEGORY
</select>
</mapper>
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<Category> 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<Category> categoryList(){
List<Category> categoryList = menuService.findAllCategory();
return categoryList;
}
}
regist.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>regist</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<div th:include="common/header.html"></div>
<h3 align="center">신규 메뉴 등록</h3>
<form action="/menu/regist" method="post" align="center">
<label>메뉴 이름 : </label>
<input type="text" name="name"><br>
<label>메뉴 가격 : </label>
<input type="number" name="price"><br>
<label>카테고리 : </label>
<select name="categoryCode" id="categoryCode">
<!-- ajax 조회 -->
</select><br>
<label>판매 상태 : </label>
<select name="orderableStatus">
<option value="Y">Y</option>
<option value="N">N</option>
</select><br>
<input type="submit" value="전송">
</form>
<script>
$(function(){
$.ajax({
url : "/menu/category",
type : "get",
success : function(data){
const categoryCode = $("#categoryCode");
for(let index in data){
categoryCode.append($("<option>").val(data[index].code).text(data[index].name));
}
},
error : function(e){
console.log(e);
}
})
})
</script>
</body>
</html>
4. 신규 메뉴 추가후 리스트 페이지로 이동하기
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.memory.project.menu.model.dao.MenuMapper">
<insert id="insertMenu" parameterType="com.memory.project.menu.model.vo.Menu">
INSERT
INTO TBL_MENU
(
MENU_CODE
, MENU_NAME
, MENU_PRICE
, CATEGORY_CODE
, ORDERABLE_STATUS
)
VALUES
(
SEQ_MENU_CODE.NEXTVAL
, #{name}
, #{price}
, #{categoryCode}
, #{orderableStatus}
)
</insert>
</mapper>
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";
}
}
'프로그래밍 > SpringBoot' 카테고리의 다른 글
SpringBoot - 6. 스프링 시큐리티 - 로그인 예제 (0) | 2021.11.09 |
---|---|
SpringBoot - 5. 스프링 시큐리티 (2) | 2021.11.08 |
SpringBoot - 3. SpringBoot 프로젝트 생성 (0) | 2021.11.01 |
SpringBoot - 2. STS4 설치하기 (0) | 2021.11.01 |
SpringBoot - 1. SpringBoot 란? (0) | 2021.11.01 |