다. 상품관리 실습 예제
- 일반 사용자 : 상품목록 보기, 장바구니 담기, 장바구니 보기
- 관리자 : 상품등록, 상품수정/삭제
- 샘플상품 이미지 다운로드
1) model.shop.dto.ProductDTO.java
package com.example.spring02.model.shop.dto;
import org.springframework.web.multipart.MultipartFile;
public class ProductDTO {
private int product_id;
private String product_name;
private int price;
private String description ;
private String picture_url;
private MultipartFile file1;
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_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 getPicture_url() {
return picture_url;
}
public void setPicture_url(String picture_url) {
this.picture_url = picture_url;
}
public MultipartFile getFile1() {
return file1;
}
public void setFile1(MultipartFile file1) {
this.file1 = file1;
}
@Override
public String toString() {
return "ProductDTO [product_id=" + product_id + ", product_name=" + product_name + ", price=" + price
+ ", description=" + description + ", picture_url=" + picture_url + ", file1=" + file1 + "]";
}
}
2) model.shop.dao.ProductDAO.java
package com.example.spring02.model.shop.dao;
import java.util.List;
import com.example.spring02.model.shop.dto.ProductDTO;
public interface ProductDAO {
List<ProductDTO> listProduct();
ProductDTO detailProduct(int product_id);
void updateProduct(ProductDTO dto);
void deleteProduct(int product_id);
void insertProduct(ProductDTO dto);
String fileInfo(int product_id);
}
3) model.shop.dao.ProductDAOImpl.java
@Repository
public class ProductDAOImpl implements ProductDAO {
@Inject
SqlSession sqlSession;
@Override
public List<ProductDTO> listProduct() {
return sqlSession.selectList("product.list_product");
}
@Override
public ProductDTO detailProduct(int product_id) {
return sqlSession.selectOne("product.detail_product", product_id);
}
@Override
public void updateProduct(ProductDTO dto) {
}
@Override
public void deleteProduct(int product_id) {
}
@Override
public void insertProduct(ProductDTO dto) {
}
@Override
public String fileInfo(int product_id) {
return null;
}
}
4) mappers/shop/productMapper.xml
<mapper namespace="product">
<select id="list_product" resultType="productDto"> <!-- alias로 클래스 경로를 줄이자 -->
select * from product order by product_name
</select>
<insert id="insert">
insert into product
values (seq_product.nextval, #{product_name},
#{price}, #{description}, #{picture_url})
</insert>
<select id="detail_product" resultType="productDto">
select * from product
where product_id = #{product_id}
</select>
<update id="update_product">
update product
set product_name=#{product_name},
price=#{price},
description=#{description},
picture_url=#{picture_url}
where product_id=#{product_id}
</update>
<!-- 첨부파일 이름 -->
<select id="file_info" resultType="String">
select picture_url from product
where product_id=#{product_id}
</select>
<delete id="deleteProduct">
delete from product
where product_id=#{product_id}
</delete>
</mapper>
5) service.shop.ProductService
public interface ProductService {
List<ProductDTO> listProduct();
ProductDTO detailProduct(int product_id);
void updateProduct(ProductDTO dto);
void deleteProduct(int product_id);
void insertProduct(ProductDTO dto);
String fileInfo(int product_id);
}
6) service.shop.ProductServiceImpl .java
@Service
public class ProductServiceImpl implements ProductService {
@Inject
ProductDAO productDAO;
@Override
public List<ProductDTO> listProduct() {
return productDAO.listProduct();
}
@Override
public ProductDTO detailProduct(int product_id) {
return productDAO.detailProduct(product_id);
}
@Override
public void updateProduct(ProductDTO dto) {
// TODO Auto-generated method stub
}
@Override
public void deleteProduct(int product_id) {
// TODO Auto-generated method stub
}
@Override
public void insertProduct(ProductDTO dto) {
// TODO Auto-generated method stub
}
@Override
public String fileInfo(int product_id) {
// TODO Auto-generated method stub
return null;
}
}
7) controller.shop.ProductController.java
@Controller // 현재 클래스를 스프링에서 관리하는 Controller Bean으로 설정
@RequestMapping("shop/product/*") //공통적인 url mapping
public class ProductController {
private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
@Inject // 의존관계 주입(DI)
ProductService productService; // 스프링에서 만든 서비스 객체를 연결시킴
@RequestMapping("list.do") // 세부적인 url mapping
public ModelAndView list(ModelAndView mav) {
mav.setViewName("/shop/product_list"); // 이동할 페이지 이름
mav.addObject("list", productService.listProduct());//데이터저장
return mav; //페이지 이동
}
@RequestMapping("detail/{product_id}") // 세부적인 url mapping
public ModelAndView detail(
@PathVariable("product_id") int product_id, ModelAndView mav) {
logger.info("product_id ---------- {}", product_id);
mav.setViewName("/shop/product_detail"); // 이동할 페이지 이름
mav.addObject("dto", productService.detailProduct(product_id));//데이터저장
return mav; //페이지 이동
}
/*
* @RequestMapping("update/{product_id}") public String update(@Path)
*/
}
8) views/shop/product_list.jsp
상품 이름을 클릭하면 상세보기 컨트롤러를 호출하는데
호출 주소는 아래와 같다.
<a href="${path}/shop/product/detail/${row.product_id}">${row.product_name}</a>
product_id가 컨트롤러에 pathvariable로 전달된다.
@RequestMapping("detail/{product_id}") // 세부적인 url mapping
public ModelAndView detail(@PathVariable("product_id") int product_id, ModelAndView mav) {
mav.setViewName("/shop/product_detail");
mav.addObject("dto", productService.detailProduct(product_id));
}
'프로그래밍 > JAVA & SPRING' 카테고리의 다른 글
[LifeSoft] spring 10강 관리자 로그인/로그아웃 기능, 상품 등록/수정/삭제 (0) | 2020.06.07 |
---|---|
[LifeSoft] spring 9강 로그인/로그아웃 처리, 장바구니 기능 만들기 (0) | 2020.06.07 |
[LifeSoft] spring 7강 상품테이블 만들기, file upload 테스트 (0) | 2020.06.07 |
[기타] spring study 모임에서 공부한 내용들 (0) | 2020.06.07 |
[백기선] 백기선님 관련 영상, 사이트 등등 (0) | 2020.06.07 |