반응형

 

service 생성

 

service을 class로 만들 것이기에 serviceImpl을 만들지 않아도 됨.

이 경우 service는 mapper에 값을 전달함.

 

 

BdService.java

package com.th.hmmerorng.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.th.hmmerorng.mapper.BdMapper;
import com.th.hmmerorng.vo.BdVO;

@Service //비지닉스 로직을 보통 담음 (Front로 넘어 가는 중)
public class BdService {

	@Autowired //서비스는 맵퍼를 부름 (프레임워크에서 그렇게 설계함. 관례적으로 좋음)
	private BdMapper bdMapper;
	
	public List<BdVO> getBdList() {
		return bdMapper.getBdList();
	}
	
	public BdVO getBd(BdVO bdVO) {
		return bdMapper.getBd(bdVO);
	}
	
	public int insBd(BdVO bdVO) {
		return bdMapper.insBd(bdVO);
	}
	
	public int upBd(BdVO bdVO) {
		return bdMapper.upBd(bdVO);
	}
	
	public int delBd(BdVO bdVO) {
		return bdMapper.delBd(bdVO);
	}
}

 

 

 

controller 생성

 

 

BdController.java

package com.th.hmmerorng.controller;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.th.hmmerorng.service.BdService;
import com.th.hmmerorng.vo.BdVO;

@RestController //@Controller + @ResponseBody
@RequestMapping("/api") //접수 창구 이름을 만들어야 함(규모가 크면 계층적으로)
public class BdController {
	
	@Autowired
	private BdService bdService;
	
	@GetMapping("/bds")
	public List<BdVO> getBdList() {
		return bdService.getBdList();
	}
	
	@GetMapping("/bds/{bdNum}") //{} 안에 있는 걸 받기 위해선 @PathVariable 사용
	public BdVO getBd(@PathVariable int bdNum) { //@PathVariable의 이름이 {}안에 있는 이름과 같아야 함
		BdVO bdVO = new BdVO();
		bdVO.setBdNum(bdNum);
		
		return bdService.getBd(bdVO);
	}
	
	@PostMapping("/bds") //파일 첨부면 @RequestBody 사용 x
	public String insBd(@RequestBody BdVO bdVO) {
		int cnt = bdService.insBd(bdVO);
		
		if(cnt == 0) {
			return "NG";
		}
		return "OK";
	}
	
	@PutMapping("/bds")
	public String upBd(@RequestBody BdVO bdVO) {
		int cnt = bdService.upBd(bdVO);
		
		if(cnt == 0) {
			return "NG";
		}
		return "OK";
	}
	
	@DeleteMapping("/bds/{bdNum}")
	public String delBd(@PathVariable int bdNum) {
		BdVO bdVO = new BdVO();
		bdVO.setBdNum(bdNum);
		
		int cnt = bdService.delBd(bdVO);
		
		if(cnt == 0) {
			return "NG";
		}
		return "OK";
	}
}

 

 

 

+ 위의 파일을 만든 후 하단의 링크에서 테스트 가능

(sts4에서 서버를 실행해야 함!!)

 

chrome-extension://eipdnjedkpcnlmmdfdkgfpljanehloah/workspace

 

크롬의 확장 프로그램을 설치해야함.

 

 

테스트 화면

 

 

반응형