1. 程式人生 > >使用springboot 時contraller中findById 方法報錯問題

使用springboot 時contraller中findById 方法報錯問題

在書寫springboot的contraller時 出現了 findByIdfindOne 報錯出紅。

經過查詢一番資料後,發現spring boot 2版本中所用的查詢是   findById();  並且在使用的時候會加上一個get()方法

程式碼如下:

 @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/fone/{id}")
    public User selectOne(@PathVariable("id") Integer id){
        User user = new User();

        //userRepository.findOne(id);
        //userRepository.findOne(user);
        user = userRepository.findById(id).get();
        System.out.println(user);
        return user;
    }

這樣查詢資料便會成功。