1. 단순 값 조회
public interface MemberRepository extends JpaRepository<Member, Long> {
// @Query, 값, DTO 조회하기
@Query("select m.username from Member m")
List<String> findUsernameList();
}
단순 값 조회 테스트
@Autowired MemberRepository memberRepository;
// 단순 값 조회
@Test
public void findUsernameList(){
Member m1 = new Member("memberA", 10);
Member m2 = new Member("member2", 20);
memberRepository.save(m1);
memberRepository.save(m2);
List<String> usernameList = memberRepository.findUsernameList();
for (String s : usernameList) {
System.out.println("s = " + s);
}
}
2. DTO 조회
public interface MemberRepository extends JpaRepository<Member, Long> {
// DTO 조회
@Query("select new study.datajpa.dto.MemberDto(m.id, m.username, t.name) from Member m join m.team t")
List<MemberDto> findMemberDto();
}
dto 조회 테스트
@Autowired MemberRepository memberRepository;
@Autowired TeamRepository teamRepository;
// DTO 조회
@Test
public void findMemberDto(){
Team team = new Team("teamA");
teamRepository.save(team);
Member m1 = new Member("memberA", 10);
m1.setTeam(team);
memberRepository.save(m1);
List<MemberDto> memberDto = memberRepository.findMemberDto();
for (MemberDto dto : memberDto) {
System.out.println("dto = " + dto);
}
}
'프로그래밍 > SpringDataJPA' 카테고리의 다른 글
Logging 설정 (0) | 2022.06.18 |
---|---|
파라미터 바인딩 (0) | 2022.06.18 |
주요 메서드, 쿼리 메소드 기능 (0) | 2022.06.18 |
예제 도메인 모델 (1) | 2022.06.18 |
스프링 부트 라이브러리 살펴보기 (0) | 2022.06.18 |