Spring Data JPA - 반환 타입
목차 Spring Data JPA - 벌크성 수정 쿼리 Spring Data JPA - Convertor Spring Data JPA - Auditing Spring Data JPA - Paging Request Paramater Spring Data JPA - 페이징과 정렬 Post not found: spring/spring-data-jpa/07-spring-data-jpa Spring Data JPA - 반환 타입 Spring Data JPA - Query 파라미터 바인딩 Spring Data JPA - Query 를 이용한 조회 결과를 특정 값으로 반환하기 Spring Data JPA - JPQL (Java Persistence Query Lange) 사용하기 Spring Data JPA - 메소드 이름으로 쿼리 생성하기 Spring Data JPA - 시작하기 반환 타입List<Member> findListByUsername(String username); // 반환 타입 : 컬랙션Member findMemberByUsername(String username); // 반환 타입 : 단건Optional<Member> findOptionalByUsername(String useranme); // 반환 티입 : 단건 Optional public interface MemberRepository extends JpaRepository<Member, Long> { List<Member> findByUsernameAndAgeGreaterThan(String username, int age); @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); // 값으로 조회하기 @Query("select m.username from Member m") List<String> findUsernameList(); // DTO로 조회하기 @Query("select new com.example.datajpa.dto.MemberDto( m.id, m.username, t.name)from Member m join m.team t") List<MemberDto> findMemberByDto(); @Query("select m from Member m where m.username in :names") List<Member> findByNames(@Param("names") Collection<String> names); List<Member> findListByUsername(String username); // 반환 타입 : 컬랙션 Member findMemberByUsername(String username); // 반환 타입 : 단건 Optional<Member> findOptionalByUsername(String useranme); // 반환 티입 : 단건 Optional} @Testpublic void returnType(){ Member member1 = new Member("AAA", 10); Member member2 = new Member("BBB", 20); memberRepository.save(member1); memberRepository.save(member2); List<Member> members = memberRepository.findListByUsername("AAA"); Member member = memberRepository.findMemberByUsername("AAA"); Optional<Member> optional = memberRepository.findOptionalByUsername("AAA"); assertThat(members).isInstanceOf(List.class); assertThat(member).isInstanceOf(Member.class); assertThat(optional).isInstanceOf(Optional.class);}