Published 2022. 7. 20. 13:15
select m from Member m where m.username = ?0 //위치 기반
select m from Member m where m.username = :name //이름 기반
import org.springframework.data.repository.query.Param
public interface MemberRepository extends JpaRepository<Member, Long> {
 @Query("select m from Member m where m.username = :name")
 Member findMembers(@Param("name") String username);
}

 

컬렉션 파라미터 바인딩
Collection 타입으로 in절 지원

@Query("select m from Member m where m.username in :names")
List<Member> findByNames(@Param("names") List<String> names);
 @Test
    public void findByNames(){

        Member member1 = new Member("AAA", 10);
        Member member2 = new Member("AAA", 20);

        memberRepository.save(member1);
        memberRepository.save(member2);
        


        List<Member> result = memberRepository.findByNames(Arrays.asList("AAA", "BBB"));
        for (Member member : result) {
            System.out.println("member = " + member);
        }

    }

 

 

 

출처 : 김영한 JPA 스프링 데이터 강의

'JPA > JPA-Spring Data' 카테고리의 다른 글

스프링 데이터 JPA 페이징과 정렬  (0) 2022.07.20
순수 JPA 페이징과 정렬  (0) 2022.07.20
@Query, 값, DTO 조회하기  (0) 2022.07.20
@Query, 리포지토리 메소드에 쿼리 정의하기  (0) 2022.07.20
JPA NamedQuery  (0) 2022.07.19
복사했습니다!