JpaSpecificationExecutor의 페이지 나누기 사용
11562 단어 【미니서비스】[웨이트 서비스 항목]【프로젝트】
JpaSpecificationExecutor의 페이지 나누기 사용
Specification 객체 작성
private Specification<Label> createSpecification(Label label){
Specification specification = new Specification<Label>(){
@Override
public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<>();
if(label.getLabelname()!=null && !"".equals(label.getLabelname())){
Predicate predicate = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
list.add(predicate);
}
if(label.getState()!=null && !"".equals(label.getState())){
Predicate predicate = cb.equal(root.get("state").as(String.class), label.getState());
list.add(predicate);
}
Predicate[] parr = new Predicate[list.size()];
//
parr = list.toArray(parr);
return cb.and(parr);
}
};
return specification;
}
페이지별 조회
public Page<Label> pageQuery(Label label, int page, int size) {
// , springdatajpa ,
Pageable pageable = PageRequest.of(page-1, size);
Specification specification = this.createSpecification(label);
return labelDao.findAll(specification, pageable);
}
LabelDao
package com.tensquare.base.dao;
import com.tensquare.base.pojo.Label;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface LabelDao extends JpaRepository<Label, String>, JpaSpecificationExecutor<Label> {
}