【问题标题】:Error creating bean with name 'flightController': Unsatisfied dependency expressed through field 'flightrepos';创建名称为“flightController”的 bean 时出错:通过字段“flightrepos”表示的依赖关系不满足;
【发布时间】:2019-02-22 03:44:14
【问题描述】:

我在第一个代码中创建了 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";
}}

用户 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 '!

【问题讨论】:

  • 这里的驼峰拼写错误:将离开城市更改为离开城市

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


【解决方案1】:

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

改变,

@Param("departurecity")String from

到,

@Param("departureCity")String from

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我猜同样的tuto也有同样的问题:p juste change @Param("departurecity") to @Param("departureCity")

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 2019-10-11
        • 2021-07-23
        • 2020-11-25
        • 2017-05-15
        • 2018-03-19
        • 2021-06-23
        • 2019-08-08
        • 2020-12-12
        相关资源
        最近更新 更多