StoreController
package com.metacoding.storev2.store;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@RequiredArgsConstructor
@Controller
public class StoreController {
private final StoreService storeService;
@GetMapping("/store/save-form")
public String saveForm() {
return "store/save-form";
}
@PostMapping("/store/save")
public String save(StoreRequest.SaveDTO saveDTO) {
System.out.println("name: " + saveDTO.getName());
System.out.println("stock: " + saveDTO.getStock());
System.out.println("price: " + saveDTO.getPrice());
storeService.상품등록(saveDTO);
return "redirect:/";
}
@GetMapping("/store/list")
public String list(HttpServletRequest request) {
List<Store> storeList = storeService.상품목록();
request.setAttribute("models", storeList);
return "store/list";
}
@GetMapping("/store/{id}")
public String detail(@PathVariable("id") int id, HttpServletRequest request) {
Store store = storeService.상세보기(id);
request.setAttribute("model", store);
return "store/detail";
}
}
StoreService
package com.metacoding.storev2.store;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@RequiredArgsConstructor
@Service
public class StoreService {
private final StoreRepository storeRepository;
@Transactional
public void 상품등록(StoreRequest.SaveDTO saveDTO) {
storeRepository.save(saveDTO.getName(), saveDTO.getStock(), saveDTO.getPrice());
}
public List<Store> 상품목록() {
List<Store> storeList = storeRepository.findAll();
return storeList;
}
public Store 상세보기(int id) {
Store store = storeRepository.findById(id);
return store;
}
}
StoreRepository
package com.metacoding.storev2.store;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import java.util.List;
@RequiredArgsConstructor
@Repository
public class StoreRepository {
private final EntityManager em;
public void save(String name, int stock, int price) {
Query query = em.createNativeQuery("insert into store_tb (name, stock, price) values (?, ?, ?)");
query.setParameter(1, name);
query.setParameter(2, stock);
query.setParameter(3, price);
query.executeUpdate();
}
public List<Store> findAll() {
Query query = em.createNativeQuery("select * from store_tb order by id desc", Store.class);
return query.getResultList();
}
public Store findById(int id) {
Query query = em.createNativeQuery("select * from store_tb where id = ?", Store.class);
query.setParameter(1, id);
return (Store) query.getSingleResult();
}
}
detail
{{> layout/header}}
<div class="container mt-2">
<div class="mt-4 p-5 bg-light text-dark rounded-4">
{{#model}}
<p>번호 : {{id}}</p>
<p>상품명 : {{name}}</p>
<p>상품가격 : {{price}}</p>
<p>상품재고 : {{stock}}</p>
{{/model}}
</div>
<div class="mt-3 mb-3">
<a href="#" class="btn btn-outline-primary">수정</a>
<form action="#" method="POST" class="d-inline">
<button type="submit" class="btn btn-outline-primary">삭제</button>
</form>
</div>
<div class="mt-4 p-5 bg-light text-dark rounded-4">
<form action="#">
<input type="hidden" value="1">
<input type="text" placeholder="개수를 입력하세요">
<button type="submit" class="btn btn-outline-primary">구매</button>
</form>
</div>
</div>
</body>
</html>
store/list
{{> 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}}">상세보기</a></td>
</tr>
{{/models}}
</tbody>
</table>
</div>
</body>
</html>
Share article