Код: Выделить всё
com.findersgame.questtracker
|- FindersGameQuestTrackerApiApplication.java
|
|- controller
| |- QuestController.java
|
|- model
| |- Location.java
| |- Provider.java
| |- Quest.java
|
|- repository
| |- LocationRepository.java
| |- ProviderRepository.java
| |- QuestRepository.java
|
|- service
|- QuestService.java
Перейдем к основному файлу FindersGameQuestTrackerApiApplication.java:
Код: Выделить всё
package com.findersgame.questtracker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication(scanBasePackages={ "com.findersgame.questtracker.*" })
@EnableJpaRepositories("com.findersgame.questtracker.repository.*")
@ComponentScan(basePackages = { "com.findersgame.questtracker.*" })
public class FindersGameQuestTrackerApiApplication {
public static void main(String[] args) {
SpringApplication.run(FindersGameQuestTrackerApiApplication.class, args);
}
}
Код: Выделить всё
@SpringBootApplication
Далее идут контроллер, служба и репозитории, которые я создал:
Код: Выделить всё
package com.findersgame.questtracker.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.findersgame.questtracker.service.QuestService;
@RestController
public class QuestController {
private QuestService questService;
public QuestController(QuestService questService) {
super();
this.questService = questService;
}
@GetMapping("fix-quests")
public void fixQuests() {
questService.fixQuests();
}
}
Код: Выделить всё
package com.findersgame.questtracker.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.findersgame.questtracker.model.Location;
import com.findersgame.questtracker.model.Provider;
import com.findersgame.questtracker.model.Quest;
import com.findersgame.questtracker.repository.LocationRepository;
import com.findersgame.questtracker.repository.ProviderRepository;
import com.findersgame.questtracker.repository.QuestRepository;
@Service
public class QuestService {
@Autowired
private LocationRepository locationRepository;
@Autowired
private ProviderRepository providerRepository;
@Autowired
private QuestRepository questRepository;
@Autowired
public QuestServiceImpl(
FlooRepository flooRepository,
GiverRepository giverRepository,
QuestRepository questRepository) {
super();
this.flooRepository = flooRepository;
this.giverRepository = giverRepository;
this.questRepository = questRepository;
}
public void fixQuests() {
List quests = questRepository.findAll();
quests.forEach(quest -> {
String locationName = quest.getLocationName();
String providerName = quest.getProviderName();
if (locationName != null) {
Location location = locationRepository.findByName(locationName);
if (location != null) {
quest.setLocationId(location.getId());
}
}
if (providerName != null) {
Provider provider = providerRepository.findByName(providerName);
if (provider != null) {
quest.setLocationId(provider.getId());
}
}
questRepository.save(quest);
});
}
}
Код: Выделить всё
package com.findersgame.questtracker.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.findersgame.questtracker.model.Location;
@Repository
public interface LocationRepository extends JpaRepository {
@Query(value = "SELECT * FROM locations WHERE `name` = ?1", nativeQuery = true)
Location findByName(String name);
}
Код: Выделить всё
package com.findersgame.questtracker.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.findersgame.questtracker.model.Provider;
@Repository
public interface ProviderRepository extends JpaRepository
{
@Query(value = "SELECT * FROM providers WHERE `name` = ?1", nativeQuery = true)
Provider findByName(String name);
}
Код: Выделить всё
package com.findersgame.questtracker.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.findersgame.questtracker.model.Quest;
@Repository
public interface QuestRepository extends JpaRepository {
//pass
}
- имеет идентификатор, locationId, locationName, идентификатор поставщика, имя поставщика и имя.
Код: Выделить всё
Quest
< li>И Location, и Provider пока имеют только идентификатор и имя. - Все вышеперечисленные методы имеют геттеры и сеттеры.
Код: Выделить всё
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.findersgame.questtracker.service.QuestService required a bean of type 'com.findersgame.questtracker.repository.LocationRepository' that could not be found.
Action:
Consider defining a bean of type 'com.findersgame.questtracker.repository.LocationRepository' in your configuration.
- Добавьте @EnableJpaRepositories в основной class, сделал это.
- Добавил @ComponentScan в основной класс, сделал это.
- Добавил @EnableJpaRepositories в основной класс сделал это.
- Добавьте @Service в службу и @Repository в репозитории, но у меня это было с самого начала.
Также пробовал без @Repository, так как он не нужен (уже часть JpaRepository).
[*]Я также пробовал удалить LocationRepository и ProviderRepository, чтобы убедиться, что написанные мною запросы не повлияли на него или что наличие нескольких сервисов не повлияло на него.
- единственная разница заключалась в том, что вместо этого он не мог найти QuestRepository.
Ничто из того, что я пробовал до сих пор, не помогло. Я уверен, что мне не хватает чего-то простого, но за все мои поиски я так и не смог этого найти. Пожалуйста, помогите!
Подробнее здесь: https://stackoverflow.com/questions/790 ... ing-a-bean