使用springboot写crud时,发现JpaRepository没有findOne传Integer参数的方法
springboot操作数据库时找不到findOne方法
通过查阅资料发现这是因为springboot版本的原因
在springboot1.x的时候是有findOne(Integer id)的,但是到2.x后就没有了
这里有两种解决方案:

  1. 使用findById(Integer id).get(),但这里需要注意的是,如果存在会直接返回需要查找的信息,如果不存在就会报异常,所以在使用的时候需要做一下判断:findById(id).isPresent()

  2. 使用findById(Integer id).orElse(null),这里表示,如果id存在则返回需要查找的信息,如果不存在,这里设置为返回null(推荐)

  3. 也可以更换springboot的版本,使用1.x的版本就不存在这个问题了

参考:
https://blog.csdn.net/u012211603/article/details/79828277

相关文章:

  • 2021-04-14
  • 2021-11-12
  • 2023-03-24
  • 2021-07-25
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-01
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2022-01-10
相关资源
相似解决方案