【发布时间】: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