MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค Spring

[Spring / ์‹ค์Šต1] ์ ์ˆ˜ ๋“ฑ๋ก ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ (Service, Controller)

HYEJU01 2024. 8. 25. 19:47

1) Service interface / Service impl ๋งŒ๋“ค๊ธฐ

 

๐Ÿ’€Service interface

mapper ์™€ ๋ฉ”์„œ๋“œ๋ช…์„ ํ†ต์ผ์‹œ์ผœ์ค€๋‹ค (์ผ๊ด€์„ฑ, ๊ฐ€๋…์„ฑ)

package com.simple.basic.service.score;

import java.util.ArrayList;

import com.simple.basic.command.ScoreVO;

public interface ScoreService {
	
	public void regist(ScoreVO vo); //๋“ฑ๋ก
	public ArrayList<ScoreVO> getScore(); //์กฐํšŒ
	public void delete(int sno); //์‚ญ์ œ
}

 

 

๐Ÿ’€Service implements

@Service("x")์—์„œ "x"๋Š” ์ด ์„œ๋น„์Šค ๋นˆ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

package com.simple.basic.service.score;

import java.util.ArrayList;

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

import com.simple.basic.command.ScoreVO;
import com.simple.basic.service.score.impl.ScoreDAO;

@Service("x") //๋ฐ˜๋“œ์‹œ component-scan์— ์˜ํ•˜์—ฌ ์ฝํ˜€์•ผ ํ•จ
public class ScoreServiceImpl implements ScoreService {

	@Autowired
	private ScoreMapper scoreMapper;

}

 

 

๐Ÿ’€ ๋“ฑ๋ก 

scoreMapper์˜ regist ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ

	@Override
	public void regist(ScoreVO vo) {
		System.out.println("regist๋ฉ”์„œ๋“œ ๋™์ž‘");
		scoreMapper.regist(vo);
	}

 

๐Ÿ’€ ์กฐํšŒ 

	@Override
	public ArrayList<ScoreVO> getScore() {
		System.out.println("getScore๋ฉ”์„œ๋“œ ๋™์ž‘");
		return scoreMapper.getScore();
	}

 

๐Ÿ’€ ์‚ญ์ œ

	@Override
	public void delete(int sno) {
		System.out.println("delete๋ฉ”์„œ๋“œ ๋™์ž‘");
		scoreMapper.delete(sno);
	}

 

 

 

2) Controller ๋งŒ๋“ค๊ธฐ

 

scoreList ๋ชฉ๋ก์œผ๋กœ ์ด๋™ ๋ฉ”์„œ๋“œ
scoreRegist ๋“ฑ๋ก ๋ฉ”์„œ๋“œ
scoreForm ํผ ์š”์ฒญ ๋ฉ”์„œ๋“œ
deleteScore ์‚ญ์ œ ์š”์ฒญ ๋ฉ”์„œ๋“œ

 

 

 

๐Ÿ’€controller 

@controller ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํด๋ž˜์Šค๊ฐ€ Spring MVC์˜ ์ปจํŠธ๋กค๋Ÿฌ ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ช…์‹œ

@RequestMapping("/service") ๋Š” URL ํŒจํ„ด์ด /service๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ์ด ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์ฒ˜๋ฆฌํ•จ์„ ๋œปํ•œ๋‹ค.

 

 

package com.simple.basic.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.simple.basic.command.ScoreVO;
import com.simple.basic.service.score.ScoreService;
import com.simple.basic.service.score.ScoreServiceImpl;

@Controller
@RequestMapping("/service")
public class ScoreController {

	
}

 

 

๐Ÿ’€Service ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•ด์ค€๋‹ค.

	//1st
//	ScoreService service = new ScoreServiceImpl();
	
	//impl๋ฅผ bean์œผ๋กœ ๋“ฑ๋ก
	@Autowired //Resource <--
	@Qualifier("x")
	ScoreService scoreService;

 

 

๐Ÿ’€  ๋ชฉ๋ก์œผ๋กœ ์ด๋™ ๋ฉ”์„œ๋“œ

Model ๊ฐ์ฒด๋Š” ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ๋กœ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉ

list ๊ฐ€ "list" ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ VIEW ์— ์ „๋‹ฌ๋œ๋‹ค.

	
	//๋ชฉ๋ก
	@RequestMapping("/scoreList")
	public String scoreList(Model model) {
		
		//selectํ•ด์„œ model์— ๋‹ด์•„์„œ ํ™”๋ฉด์œผ๋กœ..
		ArrayList<ScoreVO> list = scoreService.getScore();
		model.addAttribute("list", list);
		
		return "service/scoreList";
	}

 

 

 

๐Ÿ’€  ๋“ฑ๋ก ๋ฉ”์„œ๋“œ

scoreRegist.jsp ๊ฐ™์€ JSP ํŒŒ์ผ์ด ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ˜ํ™˜๋จ.

	//๋“ฑ๋ก
	@RequestMapping("/scoreRegist")
	public String scoreRegist() {
		return "service/scoreRegist";
	}

 

 

๐Ÿ’€ ํผ ์š”์ฒญ ๋ฉ”์„œ๋“œ

 

@RequestMapping(value = "/scoreForm", method = RequestMethod.POST) 

POST ๋ฐฉ์‹์œผ๋กœ /service/scoreForm URL ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

	//ํผ์š”์ฒญ
	@RequestMapping(value = "/scoreForm", method = RequestMethod.POST)
	public String scoreForm( ScoreVO vo ) { //๊ฐ’ ๋ฐ›๊ธฐ
	
		scoreService.regist(vo);
				
		return "service/scoreResult"; //๊ฒฐ๊ณผํ™”๋ฉด
	}

 

 

 

 

๐Ÿ’€ ์‚ญ์ œ ์š”์ฒญ ๋ฉ”์„œ๋“œ 

 

${vo.sno} ๊ฐ’์„ ํƒœ์›Œ ๋ณด๋‚ธ๋‹ค ! ( ์‚ญ์ œํ•  ๋ฐ์ดํ„ฐ์˜ ID๋ฅผ URL ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ)

 

@RequestParam ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ sno ๊ฐ’์„ ์ถ”์ถœ

(//@RequestParam๋ฐฉ์‹ - ๋ฐ˜๋“œ์‹œ ๊ฐ’์„ ํ™”๋ฉด์—์„œ ๋„˜๊ฒจ์•ผ ๋จ(ํ•„์ˆ˜) )

delete ์— sno ๊ฐ’ ๋„ฃ์–ด์„œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

	
	//์‚ญ์ œ์š”์ฒญ
	@RequestMapping("/deleteScore")
	public String deleteScore(@RequestParam("sno") int sno) {
		
		scoreService.delete(sno);
		
		return "redirect:/service/scoreList"; //๋‹ค์‹œ ๋ชฉ๋กํ™”๋ฉด์œผ๋กœ
	}