JPA多条件查询

Posted michaelcnblogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA多条件查询相关的知识,希望对你有一定的参考价值。

Page<AoaQuestionHeader> findAll(Specification<AoaQuestionHeader> spec, Pageable pageable);
List<AoaQuestionHeader> headerPage = questionHeaderDao.findAll(new Specification<AoaQuestionHeader>(){
    @Override
public Predicate toPredicate(Root<AoaQuestionHeader> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>();
if (!typeId.equals(0L)){
list.add(cb.equal(root.get("typeId").as(Long.class),typeId)) ;
}
if (!groupId.equals(0L)){
list.add(cb.equal(root.get("groupId").as(Long.class), groupId)) ;
}
if (!leaderId.equals(0L)){
list.add(cb.equal(root.get("leaderId").as(Long.class), leaderId) );
}
Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
}
}, new PageRequest(page,size , sort));




List<AoaQuestionHeader> findAll(Specification<AoaQuestionHeader> spec);

List<AoaQuestionHeader> hlist = questionHeaderDao.findAll(new Specification<AoaQuestionHeader>(){
    @Override
public Predicate toPredicate(Root<AoaQuestionHeader> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>();
if (!typeId.equals(0L)){
list.add(cb.equal(root.get("typeId").as(Long.class),typeId)) ;
}
if (!groupId.equals(0L)){
list.add(cb.equal(root.get("groupId").as(Long.class), groupId)) ;
}
if (!leaderId.equals(0L)){
list.add(cb.equal(root.get("leaderId").as(Long.class), leaderId) );
}
Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
}
});





这么个意思,自己的笔记看不懂勿用

以上是关于JPA多条件查询的主要内容,如果未能解决你的问题,请参考以下文章

多对多关系中的 JPA 条件查询

jpa多条件查询重写Specification的toPredicate方法(转)

Spring Data JPA动态查询(多条件and)

springboot jpa mongodb 多条件分页查询

Android用query怎么进行多条件查询?

python mongoDB 模糊查询(多条件)