控制器
@RestController
@RequestMapping("/api")
public class RouteController {
@Autowired
RouteServiceImpl routeService;
@GetMapping("/getAllRoutes")
public List<Route> getAllRoutes(){
return routeService.getAll();
}
}
服务
@Service
public class RouteServiceImpl implements RouteService{
@Autowired
RouteRepository routeRepository;
@Override
public Route insert(Route route) {
return routeRepository.save(route);
}
@Override
public Route update(Route route) {
return routeRepository.save(route);
}
@Override
public void delete(int id) {
routeRepository.deleteById(id);
}
@Override
public Route getRouteById(int id) {
return routeRepository.findById(id).get();
}
@Override
public Route getRouteByName(String name) {
return null;
}
@Override
public List<Route> getAll() {
return routeRepository.findAll();
}
}
存储库
@Repository
public interface RouteRepository extends JpaRepository<Route,Integer> {
}
答案 0 :(得分:0)
没有与您的问题相关联,但您应始终@Autowire接口而不是在您的服务中实现,也不需要在扩展JpaRepository的repo上使用@Repository。