Skip to content

Commit

Permalink
[Week9][Chap4] Feat : @query, 값, DTO 조회하기 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
songhee1 committed Nov 20, 2023
1 parent c72a592 commit 8d3852b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 최송희/data-jpa/src/main/java/study/datajpa/dto/MemberDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package study.datajpa.dto;

import lombok.Data;

@Data
public class MemberDto {
private Long id;
private String username;
private String teamName;

public MemberDto(Long id, String username, String teamName) {
this.id = id;
this.username = username;
this.teamName = teamName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import study.datajpa.dto.MemberDto;
import study.datajpa.entity.Member;

import java.util.List;
Expand All @@ -13,4 +14,10 @@ public interface MemberRepository extends JpaRepository<Member, Long> {

@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();

@Query("select new study.datajpa.dto.MemberDto(m.id, m.username, t.name) from Member m join m.team t")
List<MemberDto> findMemberDto();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import study.datajpa.dto.MemberDto;
import study.datajpa.entity.Member;
import study.datajpa.entity.Team;

import java.util.List;
import java.util.Optional;
Expand All @@ -19,6 +21,7 @@
@Rollback(false)
class MemberRepositoryTest {
@Autowired MemberRepository memberRepository;
@Autowired TeamRepository teamRepository;

@Test
public void testMember(){
Expand Down Expand Up @@ -90,4 +93,35 @@ public void testQuery(){
assertThat(result.get(0)).isEqualTo(m1);

}

@Test
public void findUsernameList(){
Member m1 = new Member("AAA", 10);
Member m2 = new Member("BBB", 20);

memberRepository.save(m1);
memberRepository.save(m2);

List<String> usernameList = memberRepository.findUsernameList();
for (String s : usernameList) {
System.out.println("s = "+s);
}

}

@Test
public void findMemberDto(){
Team team = new Team("teamA");
teamRepository.save(team);

Member m1 = new Member("AAA", 10);
memberRepository.save(m1);
m1.setTeam(team);

List<MemberDto> memberDto = memberRepository.findMemberDto();
for (MemberDto s : memberDto) {
System.out.println("dto = "+s);
}

}
}

0 comments on commit 8d3852b

Please sign in to comment.