MVC 요청 처리 과정

 

서비스 계층 ==> controller에서 dao를 직접 받았었음 그러면 dao가 바뀌면 다 바꿔야했음 
하지만 서비스 계층을 주면 수정이 쉽고 결합이 느슨해짐 
비지니스 로직은 주로 서비스 계층에서 한다.

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:import url="/top"/>
<c:import url="/carousel"/>
<c:import url="/prodPspec">
	<c:param name="pspec" value="HIT"/>
</c:import>
<c:import url="/prodPspec">
	<c:param name="pspec" value="NEW"/>
</c:import>
<c:import url="/foot"/>

ProductController
mapping하고 view로 넘기는 역할

package com.tis.myshop;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.tis.domain.ProductVO;
import com.tis.service.ShopService;

@Controller
public class ProductController {
	
	@Autowired
	private ShopService shopSvc;
	
	@RequestMapping(value="/prodPspec",method=RequestMethod.GET)
	public String productByPspec(Model model,@RequestParam(defaultValue="") String pspec) {
		
		//String pspec=req.getParameter("pspec")을 대신해주는 것이다.
		List<ProductVO> plist = shopSvc.selectByPspec(pspec);
		
		model.addAttribute("hitList",plist);
		
		return "shop/mallHit";
		// /WEB-INF/views/shop/mallHit.jsp
	}
}

 

ShopService

package com.tis.service;

import java.util.List;
import java.util.Map;

import com.tis.domain.CartVO;
import com.tis.domain.ProductVO;

public interface ShopService {
	
	/** 상품관련 메소드================*/
	
	/*Pspec 별로 상품 정보 가져오기*/
	public List<ProductVO> selectByPspec(String pspec);
	
	/*카테고리별 상품정보 가져오기*/
	public List<ProductVO> selectByCategory(String code);
	
	/**상품번호로 특정 상품 정보 가져오기*/
	public ProductVO selectByPnum(Integer pnum);
	
	/**장바구니 관련 메소드===============*/
	int addCart(CartVO cartVo);//장바구니 추가하기
	int updateCartQty(CartVO cartVo);//장바구니 추가 관련=>기존에 담긴 상품이면 수량만 수정하기
	int editCart(CartVO cartVo);// 장바구니 수정하기
	List<CartVO> selectCartView(int midx);//특정 회원의 장바구니 목록보기
	
	int delCart(int cartNum);
	int delCartAll(CartVO cartVo);
	int delCartOrder(Map<String,Integer>map);
	
	int getCartCountByIdx(CartVO cartVo);
	
	CartVO getCartTotal(int midx_fk);//특정 회원의 장바구니 총액,총포인트 구하기

	public void delCartByOrder(int midx_fk, int pnum);
	
	
	/**상품평-리뷰 관련 메소드=================*/
	/*int insertReview(PReviewVO rvo);
	List<PReviewVO> getReviewList(int pnum_fk, int start, int end);
	int getReviewCount(int pnum_fk);
	int deleteReview(int ridx);
	PReviewVO getReview(int ridx);
	int updateReview(PReviewVO rvo);*/

}

ShopServiceImpl

package com.tis.service;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.tis.domain.CartVO;
import com.tis.domain.ProductVO;
import com.tis.mapper.ProductMapper;

@Service
public class ShopServiceImpl implements ShopService {

	@Inject //pom.xml에 등록되어 있어야한다.
	private ProductMapper productMapper;
	
	@Override
	public List<ProductVO> selectByPspec(String pspec) {
		// TODO Auto-generated method stub
		return productMapper.selectByPspec(pspec);
	}

	@Override
	public List<ProductVO> selectByCategory(String code) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ProductVO selectByPnum(Integer pnum) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int addCart(CartVO cartVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateCartQty(CartVO cartVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int editCart(CartVO cartVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<CartVO> selectCartView(int midx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int delCart(int cartNum) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delCartAll(CartVO cartVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delCartOrder(Map<String, Integer> map) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCartCountByIdx(CartVO cartVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public CartVO getCartTotal(int midx_fk) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delCartByOrder(int midx_fk, int pnum) {
		// TODO Auto-generated method stub

	}

}

ProductMapper.java
Spring version up 되면서 interface 구현하는 객체 없이 사용가능

package com.tis.mapper;

import java.util.List;

import com.tis.domain.ProductVO;

public interface ProductMapper {
	
	//pspec별로 상품정보 가져오기
	public List<ProductVO> selectByPspec(String pspec);
	
}

ProductMapper.xml
interface에 method이름과 같아야한다.

<?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.tis.mapper.ProductMapper">
	<!-- pspec별로 상품정보 가져오기 -->
	<select id="selectByPspec" parameterType="string" resultType="Product">
		select <include refid="product_column"/> from product where pspec=#{value} order by pnum desc
	</select>
	<!-- 상위 카테고리 목록 가져오기 -->
	<select id="getUpCategoryList" resultType="Category">
		select * from upCategory order by upCg_code asc
	</select>
	<select id="getDownCategoryList" resultType="Category" parameterType="string">
		select * from downCategory where upCg_code=#{value}
	</select>
	<insert id="productInsert" parameterType="Product">
		insert into product values(product_seq.nextval,#{upCg_code},
		#{downCg_code},#{pname},#{pimage1:VARCHAR},#{pimage2:VARCHAR},#{pimage3:VARCHAR},
		#{price},#{saleprice:NUMERIC},#{pqty:NUMERIC},#{point:NUMERIC},#{pspec:VARCHAR},
		#{pcontents:VARCHAR},#{pcompany:VARCHAR},sysdate)
	</insert>
	<select id="getProductList" resultType="Product">
		select up.upCg_code, up.upCg_name, d.downCg_code, d.downCg_name, p.*
		from upCategory up right outer join product p
		on up.upCg_code = p.upCg_code
		left outer join downCategory d
		on p.downCg_code = d.downCg_code order by 1 asc, 3 asc
	</select>
	<select id="getProductCount" resultType="_int">
		select count(pnum) from product
	</select>
	<sql id="product_column">
		pnum, upCg_code,downCg_code, pname,
		nvl(pimage1, 'noimage.JPG') pimage1,
		nvl(pimage1, 'noimage.JPG') pimage2,
		nvl(pimage1, 'noimage.JPG') pimage3,
		price, saleprice,pqty,point,pspec,
		pcontents,pcompany,pindate
	</sql>
	<select id="productInfo" resultType="Product" parameterType="_int">
		select 
		<include refid="product_column"/>
		from product where pnum=#{value}
	</select>
	<delete id="productDelete" parameterType="string">
		delete from product where pnum=#{pnum} 
	</delete>
</mapper>

context-datasource
mybatis-spring:scan을 설정해야 한다.

<!-- [4] mybatis-spring:scan 설정-->
<mybatis-spring:scan base-package="com.tis.mapper"/>
<!-- mybatis-spring:scan에 패키지를 등록하면 해당 패키지에 있는 인터페이스를 구현한 구현객체(Proxy객체)
를 스프링이 자동으로  만들어준다. 단 이때 XXXMapper.xml파일에 기술된 namespace는 
해당 인터페이스의 패키지명.인터페이스명으로 기술해야하며, 각 엘리먼트의 id는 인터페이스의 메소드명과 일치해야 한다. -->

 

+ Recent posts