创建名称为'flightController'的bean时出错:通过字段'flightrepos'表示的不满意的依赖关系;

时间:2018-09-18 07:15:06

标签: java spring spring-mvc spring-boot spring-data-jpa

我在第一个代码中创建了2个单独的代码,我的代码运行良好,而在其他代码中我遇到上述问题。我将分享这两个代码,请plz帮我解决这个问题,因为我是今年春季的新手,并提供了一些教程。< / p>

代码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";
}}

用户bean类

@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 '!

2 个答案:

答案 0 :(得分:2)

您的@Param方法的findFlights()中有一个错字。

更改

@Param("departurecity")String from

收件人

@Param("departureCity")String from

答案 1 :(得分:1)

...在带注释的查询中找不到参数'Optional [departurecity]'...

  • f.departureCity,而不是f.departurecity
  • @Param("departureCity"),而不是@Param("departurecity")