수업(국비지원)/Spring
[Spring] (MVC 2) 상품 목록 작성(list, Item,ItemDao, ItemController, ShopService)
byeolsub
2023. 4. 20. 23:49
📌 list.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- /springmvc1/src/main/webapp/WEB-INF/view/item/list.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상품 목록</title>
</head>
<body>
<a href="create">상품 등록</a>
<a href="../cart/cartView" style="float:right">장바구니</a>
<table>
<tr><th width="80">상품ID</th>
<th width="320">상품명</th><th width="100">가격</th>
<th width="80">수정</th><th width="80">삭제</th></tr>
<c:forEach items="${itemList}" var="item">
<tr><td align="center">${item.id}</td>
<td align="left">
<a href="detail?id=${item.id}">${item.name}</a></td>
<td align="right">${item.price}</td>
<td align="center"><a href="updateForm?id=${item.id}">수정</a></td>
<td align="center"><a href="deleteForm?id=${item.id}">삭제</a></td></tr>
</c:forEach>
</table>
</body>
</html>
📌 Item.java 생성
package logic;
import org.springframework.web.multipart.MultipartFile;
public class Item {
private int id;
private String name;
private int price;
private String description;
private String pictureUrl;
private MultipartFile picture;
//setter.getter, toString
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPictureUrl() {
return pictureUrl;
}
public void setPictureUrl(String pictureUrl) {
this.pictureUrl = pictureUrl;
}
public MultipartFile getPicture() {
return picture;
}
public void setPicture(MultipartFile picture) {
this.picture = picture;
}
@Override
public String toString() {
return "item [id=" + id + ", name=" + name + ", price=" + price + ", description=" + description
+ ", pictureUrl=" + pictureUrl + ", picture=" + picture + "]";
}
}
📌 ItemDao.java 생성
@Component : 해당 클래스를 객체화
- model 기능 : db와 연결된 클래스
package dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import logic.Item;
/*
* @Component : 해당 클래스를 객체화
* - model 기능 : db와 연결된 클래스
*/
@Repository //@Component(객체를 만들어주는) + model 기능
public class ItemDao {
private NamedParameterJdbcTemplate template;
private Map<String,Object> param = new HashMap<>();
private RowMapper<Item> mapper = new BeanPropertyRowMapper<>(Item.class);
@Autowired // 현재 컨테이너(객체)중 DataSource 객체를 주입.
//DataSource : 데이터 베이스에 연결되어져 있는 객체.(...DriverManagerDataSource 객체 주입)
public void setDataSource(DataSource dataSource) {
template = new NamedParameterJdbcTemplate(dataSource);
}
public List<Item> list() {
//mapper : db의 컬럼명과 mapper에 지정된 Item 클래스의 프로퍼티를 비교하여 Item 클래스에 객체로 저장
return template.query("select * from item order by id", param, mapper);
}
}
📌 ItemController.java 생성
@Controller : 객체화 대상이 되는 클래스
- Controller 기능 : url의 요청시 호출되는 클래스
package controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import logic.Item;
import logic.ShopService;
/*
* @Controller : 객체화 대상이 되는 클래스
* - Controller 기능 : url의 요청시 호출되는 클래스
*/
@Controller //@Component(객체를 만들어주는) + Controller 기능
@RequestMapping("item") // <http://localhost:8088/springmvc1/item> 호출이 들어오면 실행
public class ItemController {
@Autowired //ShopService 객체를 주입.
private ShopService service;
//http://localhost:8088/springmvc1/item/list 요청시 호출되는 메서드
@RequestMapping("list")
public ModelAndView list() {
//1. ModelAndView : 데이터값 + 뷰 정보를 저장하고 있는 객체
ModelAndView mav = new ModelAndView();
//itemList : db의 item 테이블의 모든 데이터를 Item 객체들로 저장하고 있는 객체
List<Item> itemList = service.itemList();
mav.addObject("itemList",itemList); //데이터 저장.
//뷰의 이름은 기본적으로 요청 url의 정보로 설정된다 : "item/list" 설정
return mav;
}
📌 ShopService.java 생성
@Component : 해당 클래스를 객체화
- Service 기능 : Controller 와 Model사이의 중간 역할의 클래스
package logic;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.ItemDao;
/*
* @Component : 해당 클래스를 객체화
* - Service 기능 : Controller 와 Model사이의 중간 역할의 클래스
*/
@Service //@Component + Service 기능
public class ShopService {
@Autowired //현재 내 컨테이너(객체)들 중에 itemDao 객체를 주입.
private ItemDao itemDao;
public List<Item> itemList() {
return itemDao.list();
}
}