Python 官方文档:入门教程 => 点击学习
目录Spring Boot2.X中findOne的用法但在2.X中,findOne改为了JpaRepository.findOne()在SpringBoot1.x和2.x中的不同的用
SpringBoot在1.5.X版本中,传入id即可查询对象
xxxRepository.findOne(id);
<S extends T> Optional<S> findOne(Example<S> var1);
getOne方法继续保留了,但是如果getOne(id)查询到的即使id不存在,也会返回该对象的引用,判断null无效。
后来找到了这种写法可以实现
findOne. xxxRepository.findById(id).orElse(null)
已有开发环境如下
JpaRepository支持findOne(ID)方法
T findOne(ID id);
<S extends T> Optional<S> findOne(Example<S> example);
下面是解决办法
@Override
public AyUser selectAyUserById(Integer id) {
AyUser ayUser = new AyUser();
ayUser.setId(id);
Example<AyUser> example = Example.of(ayUser);
Optional<AyUser> optional = ayUserRepository.findOne(example);
if (optional.isPresent()){
ayUser=optional.get();
return ayUser;
}else{
return null;
}
}
记录一下,方便查询!
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Spring Boot2.X中findOne的使用详解
本文链接: https://www.lsjlt.com/news/132596.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0