Я создал 2 отдельных кода в первом коде, мой код выполняет штрафы, в то время как в другом коде я сталкиваюсь с вышеупомянутой проблемой. Я поделюсь обоими кодами, пожалуйста, помогите мне, поскольку я новичок в этой весне и следую некоторому руководству.
КОД 1
Контроллер
@Controller
public class UserController {
@Autowired
private UserRepository userRepos;
@RequestMapping("/showReg")
public String showRegistrationPage() {
return "registerUser";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestParam("email")String email, @RequestParam("password")String password, ModelMap modelMap) {
User user = userRepos.findByEmail(email);
if(user.getPassword().equals(password)) {
return "findflights";
}
else {
modelMap.addAttribute("msg", "Invalid try again");
}
return "login";
}}
Класс бина пользователя
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
private String email;
private String password;
//getters and setters
}}
Класс репозитория
public interface UserRepository extends JpaRepository<User, Long> {
@Query(value = "select * from user u where u.email = :email", nativeQuery = true)
public User findByEmail(@Param("email") String email);
}
КОД 2
Контроллер
@Controller
public class FlightController {
@Autowired
private FlightRepository flightrepos;
@RequestMapping("/findFlights")
public String findFlights(@RequestParam("from")String from, @RequestParam("to")String to,@RequestParam("departureDate") @DateTimeFormat(pattern ="MM-dd-yyyy")Date departureDate, ModelMap modelMap ) {
List<Flight> flights = flightrepos.findFlights(from, to, departureDate);
modelMap.addAttribute("flights", flights);
return "displayFlights";
}}
Класс полетных бобов
@Entity
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String flightNumber;
private String operatingAirlines;
private String departureCity;
private String arrivalCity;
private Date dateOfDeparture;
private Timestamp estimatedDepartureTime;
//getters and setters
}}
Репозиторий
public interface FlightRepository extends JpaRepository<Flight, Long> {
@Query( value ="select * from Flight f where f.departurecity =:departureCity and f.arrivalCity =:arrivalCity and f.dateOfDeparture =:dateOfDeparture ", nativeQuery = true)
List<Flight> findFlights(@Param("departurecity")String from, @Param("arrivalCity")String to, @Param("dateOfDeparture")Date departureDate);
}
Я все проверил, но теперь я получаю ошибку ниже
or creating bean with name 'flightController': Unsatisfied dependency expressed through field 'flightrepos'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flightRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List com.project.test.repos.FlightRepository.findFlights(java.lang.String,java.lang.String,java.util.Date) but parameter 'Optional[departurecity]' not found in annotated query 'select * from Flight f where f.departurecity =:departureCity and f.arrivalCity =:arrivalCity and f.dateOfDeparture =:dateOfDeparture '!