jpa複数条件クエリー

6679 ワード

jpaエンティティ倉庫
public interface StudentRepository extends
        JpaRepository<Student, String>,
        JpaSpecificationExecutor<Student>{
}

jpaクエリー条件パッケージ
	/**
	*      
	*/
    private Specification<student> query(String id, String name) {
        return (Specification<student>) (root, criteriaQuery, criteriaBuilder) -> {
            List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(id)) {
                predicates.add(criteriaBuilder.equal(root.get("id"), id));
            }
            if (StringUtils.isNotBlank(name)) {
                predicates.add(criteriaBuilder.like(root.get("name"), name));
            }
            return criteriaQuery.where(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()])).getRestriction();
        };
    }
	

ページングクエリの使用
 PageRequest pageable = PageRequest.of(1, 10); 
 Specification<BaseStation> specification = query(id, name);
 studentRepository.findAll(specification, pageable);