1. 상품 리스트 보기
Codes
StoreController
@GetMapping("/store/list")
public String list(HttpServletRequest request, HttpSession session) {
// TODO : 유저 정보 확인하기 (세션 : 부가 로직)
User validatedUser = (User) session.getAttribute("sessionUser");
if (validatedUser == null) throw new RuntimeException("로그인 세션이 만료되었습니다. 다시 로그인해 주세요.");
// TODO : 전체 상품 리스트 받아와서 model(request)에 담고 보내기
List<Store> storeList = storeService.전체상품보기();
request.setAttribute("models", storeList);
return "store/list";
}
StoreService
public List<Store> 전체상품보기() {
return storeRepository.findAll();
}
StoreRepository
public List<Store> findAll() {
Query q = em.createNativeQuery("select * from store_tb order by id desc", Store.class);
List<Store> storeList = q.getResultList();
return storeList;
}
Form
{{> layout/header}}
<div class="container mt-2">
<table class="table table-hover">
<thead>
<tr>
<th>번호</th>
<th>상품명</th>
<th>상세보기</th>
</tr>
</thead>
<tbody>
{{#models}}
<tr>
<td>{{id}}</td>
<td>{{name}}</td>
<td><a href="/store/{id}/detail">상세보기</a></td>
</tr>
{{/models}}
</tbody>
</table>
</div>
</body>
</html>

2. 상품 등록하기
Codes
StoreController
@GetMapping("/store/save-form")
public String saveForm(HttpSession session) {
// TODO : 유저 정보 확인하기 (세션 : 부가 로직)
User validatedUser = (User) session.getAttribute("sessionUser");
if (validatedUser == null) throw new RuntimeException("로그인 세션이 만료되었습니다. 다시 로그인해 주세요.");
return "store/save-form";
}
// TODO : save
@PostMapping("/store/save")
public String save(StoreRequest.SaveDTO saveDTO, HttpSession session) {
// TODO : 유저 정보 확인하기 (세션 : 부가 로직)
User validatedUser = (User) session.getAttribute("sessionUser");
if (validatedUser == null) throw new RuntimeException("로그인 세션이 만료되었습니다. 다시 로그인해 주세요.");
storeService.상품추가(saveDTO.getName(), saveDTO.getStock(), saveDTO.getPrice());
return "redirect:/store/list";
}
StoreService
@Transactional
public void 상품추가(String name, int stock, int price) {
storeRepository.save(name, stock, price);
}
StoreRepository
public void save(String name, int stock, int price) {
Query q = em.createNativeQuery("insert into store_tb(name, stock, price) values(?, ?, ?)");
q.setParameter(1, name);
q.setParameter(2, stock);
q.setParameter(3, price);
q.executeUpdate();
}
Form
{{> layout/header}}
<div class="container mt-2">
<div class="mt-4 p-5 bg-light text-dark rounded-4">
<h1>상품등록 페이지</h1>
<form action="/store/save" method="post">
<div class="mb-3 mt-3">
<input type="text" class="form-control" placeholder="상품명을 입력하세요" name="name">
</div>
<div class="mb-3">
<input type="text" class="form-control" placeholder="재고를 입력하세요" name="stock">
</div>
<div class="mb-3 mt-3">
<input type="text" class="form-control" placeholder="가격을 입력하세요" name="price">
</div>
<button type="submit" class="btn btn-primary">상품등록</button>
</form>
</div>
</div>
</body>
</html>

3. 상품 상세보기
Codes
StoreController
@GetMapping("/store/{id}/detail")
public String detail(@PathVariable int id, HttpServletRequest request, HttpSession session) {
// TODO : 유저 정보 확인하기 (세션 : 부가 로직)
User validatedUser = (User) session.getAttribute("sessionUser");
if (validatedUser == null) throw new RuntimeException("로그인 세션이 만료되었습니다. 다시 로그인해 주세요.");
// TODO : 상품 id 받아와서 상세 정보 받아와서 model(request)에 담고 보내기
Store detail = storeService.상세보기(id);
request.setAttribute("model", detail);
return "store/detail";
}
StoreService
public Store 상세보기(int id) {
return storeRepository.findDetailByID(id);
}
StoreRepository
public Store findDetailByID(int id) {
Query q = em.createNativeQuery("select * from store_tb where id = ?", Store.class);
q.setParameter(1, id);
return (Store) q.getSingleResult();
}
Form
{{> layout/header}}
<div class="container mt-2">
<table class="table table-hover">
<thead>
<tr>
<th>번호</th>
<th>상품명</th>
<th>상세보기</th>
</tr>
</thead>
<tbody>
{{#models}}
<tr>
<td>{{id}}</td>
<td>{{name}}</td>
<td><a href="/store/{id}/detail">상세보기</a></td>
</tr>
{{/models}}
</tbody>
</table>
</div>
</body>
</html>

4. 상품 수정하기
Codes
StoreController
@GetMapping("/store/{id}/update-form")
public String updateForm(@PathVariable int id, HttpServletRequest request, HttpSession session) {
// TODO : 유저 정보 확인하기 (세션 : 부가 로직)
User validatedUser = (User) session.getAttribute("sessionUser");
if (validatedUser == null) throw new RuntimeException("로그인 세션이 만료되었습니다. 다시 로그인해 주세요.");
// TODO : 상품 id 받아와서 상세 정보 받아와서 model(request)에 담고 보내기
Store updateDTO = storeService.상품수정화면(id);
request.setAttribute("model", updateDTO);
return "store/update-form";
}
// TODO : update
@PostMapping("/store/{id}/update")
public String update(StoreRequest.SaveDTO saveDTO, @PathVariable int id, HttpSession session) {
// TODO : 유저 정보 확인하기 (세션 : 부가 로직)
User validatedUser = (User) session.getAttribute("sessionUser");
if (validatedUser == null) throw new RuntimeException("로그인 세션이 만료되었습니다. 다시 로그인해 주세요.");
storeService.상품수정(id, saveDTO.getName(), saveDTO.getStock(), saveDTO.getPrice());
return "redirect:/store/"+id+"/detail";
}
StoreService
public Store 상품수정화면(int id) {
return storeRepository.findDetailByID(id);
}
@Transactional
public void 상품수정(int id, String name, Integer stock, Integer price) {
storeRepository.UpdateByID(id, name, stock, price);
}
StoreRepository
public void UpdateByID(int id, String name, Integer stock, Integer price) {
Query q = em.createNativeQuery("update store_tb set name = ?, stock = ?, price = ? where id = ?");
q.setParameter(1, name);
q.setParameter(2, stock);
q.setParameter(3, price);
q.setParameter(4, id);
q.executeUpdate();
}
Form
{{> layout/header}}
<div class="container mt-2">
<div class="mt-4 p-5 bg-light text-dark rounded-4">
<h1>상품수정 페이지</h1>
<form action="/store/{{model.id}}/update" method="post">
<div class="mb-3 mt-3">
<input type="text" class="form-control" name="name" value={{model.name}}>
</div>
<div class="mb-3">
<input type="text" class="form-control" name="stock" value={{model.stock}}>
</div>
<div class="mb-3 mt-3">
<input type="text" class="form-control" name="price" value={{model.price}}>
</div>
<button type="submit" class="btn btn-primary">상품수정</button>
</form>
</div>
</div>
</body>
</html>


5. 상품 삭제하기
Codes
StoreController
// TODO : delete
@PostMapping("/store/{id}/delete")
public String delete(@PathVariable int id, HttpSession session) {
// TODO : 유저 정보 확인하기 (세션 : 부가 로직)
User validatedUser = (User) session.getAttribute("sessionUser");
if (validatedUser == null) throw new RuntimeException("로그인 세션이 만료되었습니다. 다시 로그인해 주세요.");
storeService.상품삭제(id);
return "redirect:/store/list";
}
StoreService
@Transactional
public void 상품삭제(int id) {
storeRepository.deleteById(id);
}
StoreRepository
public void deleteById(int id) {
Query q = em.createNativeQuery("delete from store_tb where id = ?");
q.setParameter(1, id);
q.executeUpdate();
}
Share article