springboot jpa 리스트 동적 조회
                                            
 2051 단어  jpa
                    
*************************
예:name,age에 따라student,name 또는age가null일 수 있습니다
******************
다오층
StudentRepository
public interface StudentRepository extends JpaRepository,
        JpaSpecificationExecutor {
}
    CustomStudentRepository
@Repository
public class StudentPlusRepository {
    @Autowired
    private StudentRepository studentRepository;
    public List find(String name,Integer age){
        Specification specification= (Specification) (root, criteriaQuery, criteriaBuilder) -> {
            List predicates=new ArrayList<>();
            if(name!=null){
                predicates.add(criteriaBuilder.like(root.get("name"),name));
            }
            if(age!=null){
                predicates.add(criteriaBuilder.equal(root.get("age"),age));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return studentRepository.findAll(specification);
    }
}
      ******************
controller층
StudentController
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private CustomStudentRepository customStudentRepository;
    @RequestMapping("/save")
    public String save(){
        for(int i=0;i<100;i++){
            Student student=new Student();
            student.setName("    "+i);
            student.setAge(i%3);
            studentRepository.save(student);
        }
        return "success";
    }
    @RequestMapping("/get")
    public List get(){
        return customStudentRepository.find("    "+"%",null);
    }
}
   이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
eclipse로 테이블에서 JPA 엔티티 만들기eclipse에서 테이블에서 JPA 엔티티를 만드는 방법을 참고하십시오. 1. 엔티티를 만들려는 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 새로 만들기 > 테이블에서 JPA 엔티티를 선택합니다. 2. "연결 추가"를...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.