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"; //๋ค์ ๋ชฉ๋กํ๋ฉด์ผ๋ก
}