
컨트롤러에서 리포지토리 코드 부분을 삭제 해준다. 대신 서비스를 같은 방식으로 적어준다
@RestController
public class SoccerTeamApiController {
//삭제
//@Autowired
//private SoccerteamRepository soccerteamRepository;
//추가
@Autowired
private SoccorteamService soccorteamService;
@GetMapping("/api/soccer-team")
public List<Team> teamList (){
return soccerteamRepository.findAll();
}
}
그 다음 팀 리스트를 불러오는 teamList 안의 내용도 바꿔준다.
@RestController
public class SoccerTeamApiController {
@Autowired
private SoccorteamService soccorteamService;
@GetMapping("/api/soccer-team")
public List<Team> teamList (){
return soccorteamService.teamList(); // 이렇게
}
아마 teamList() 가 오류가 뜰 것이다.

Create method ‘teamList’ in SoccorteamService를 클릭해서 메서드 생성을 한다.
그러면 메서드가 생성이 될 것이고 컨트롤러에 했던 것과 같이 입력해준다.
@Service
public class SoccorteamService {
@Autowired
private SoccorteamRepository soccorteamRepository;
public List<Team> teamList() {
return soccorteamRepository.findAll(); // 입력
}
}
단일 조회 기능도 동일하게 해준다.
컨트롤러
@RestController
public class SoccerTeamApiController {
@Autowired
private SoccorteamService soccorteamService;
@GetMapping("/api/soccer-team")
public List<Team> teamList (){
return soccorteamService.teamList();
}
@GetMapping("/api/soccer-team/{id}")
public Team soccerTeam(@PathVariable Long id){
return soccorteamService.soccerTeam(id);
// return soccorteamRepository.findById(id).orElse(null);
}
}
서비스
@Service
public class SoccorteamService {
@Autowired
private SoccorteamRepository soccorteamRepository;
public List<Team> teamList() {
return soccorteamRepository.findAll();
}
public Team soccerTeam(Long id) {
return soccorteamRepository.findById(id).orElse(null);
}
}
앞으로 컨트롤러 → 서비스 → 리포지토리 → 엔티티 → 리포지토리 → 서비스 → 컨트롤러 흐름으로 구현을 해 볼 예정이다.
'스프링 > 축구 팀 CRUD' 카테고리의 다른 글
SpringBoot 축구 팀 CRUD 초초초미니 프로젝트 - 축구팀 삭제기능 구현 (0) | 2024.04.22 |
---|---|
SpringBoot 축구 팀 CRUD 초초초미니 프로젝트 - 축구팀 수정 구현 (1) | 2024.04.22 |
SpringBoot 축구 팀 CRUD 초초초미니 프로젝트 - 축구팀 생성 구현 (1) | 2024.04.22 |
SpringBoot 축구 팀 CRUD 초초초미니 프로젝트 - 팀 리스트 조회, 단일 팀 조회 (4) | 2024.03.17 |