Spring Data JPA - Query 파라미터 바인딩

목차

JPQL에 파라미터 바인딩

Param 어노테이션을 이용해 쿼리문내 파라미터들을 바인딩 해준다.

@Query("select m from Member m where m.username = :username and m.age = :age")
List<Member> findUser(@Param("username") String username, @Param("age") int age);

컬렉션 파리미터 바인딩

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("BBB", 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);
}
}
Share