article thumbnail image
Published 2022. 7. 26. 21:27

사용자 정의 리포지토리 사용법

1. 사용자 정의 인터페이스 작성
2. 사용자 정의 인터페이스 구현
3. 스프링 데이터 리포지토리에 사용자 정의 인터페이스 상속

 

 

사용자 정의 리포지토리 구성

 

 

1. 사용자 정의 인터페이스 작성

public interface MemberRepositoryCustom {
 List<MemberTeamDto> search(MemberSearchCondition condition);
}

 

 

2. 사용자 정의 인터페이스 구현

 

public class MemberRepositoryImpl implements MemberRepositoryCustom {
 private final JPAQueryFactory queryFactory;
 
 public MemberRepositoryImpl(EntityManager em) {
 this.queryFactory = new JPAQueryFactory(em);
 }
 
 @Override
 //회원명, 팀명, 나이(ageGoe, ageLoe)
 public List<MemberTeamDto> search(MemberSearchCondition condition) {
 return queryFactory
 .select(new QMemberTeamDto(
 member.id,
 member.username, member.age,
 team.id,
 team.name))
 .from(member)
 .leftJoin(member.team, team)
 .where(usernameEq(condition.getUsername()),
 teamNameEq(condition.getTeamName()),
 ageGoe(condition.getAgeGoe()),
 ageLoe(condition.getAgeLoe()))
 .fetch();
 }
 
 private BooleanExpression usernameEq(String username) {
 return isEmpty(username) ? null : member.username.eq(username);
 }
 
 private BooleanExpression teamNameEq(String teamName) {
 return isEmpty(teamName) ? null : team.name.eq(teamName);
 }
 
 private BooleanExpression ageGoe(Integer ageGoe) {
 return ageGoe == null ? null : member.age.goe(ageGoe);
 }
 
 private BooleanExpression ageLoe(Integer ageLoe) {
 return ageLoe == null ? null : member.age.loe(ageLoe);
 }
 
}

 

3. 스프링 데이터 리포지토리에 사용자 정의 인터페이스 상속

public interface MemberRepository extends JpaRepository<Member, Long>,
MemberRepositoryCustom {
 List<Member> findByUsername(String username);
}

 

 

 

커스텀 리포지토리 동작 테스트 추가

@Test
public void searchTest() {
 Team teamA = new Team("teamA");
 Team teamB = new Team("teamB");
 em.persist(teamA);
 em.persist(teamB);
 
 Member member1 = new Member("member1", 10, teamA);
 Member member2 = new Member("member2", 20, teamA);
 Member member3 = new Member("member3", 30, teamB);
 Member member4 = new Member("member4", 40, teamB);
 em.persist(member1);
 em.persist(member2);
 em.persist(member3);
 em.persist(member4);
 
 MemberSearchCondition condition = new MemberSearchCondition();
 condition.setAgeGoe(35);
 condition.setAgeLoe(40);
 condition.setTeamName("teamB");
 

 List<MemberTeamDto> result = memberRepository.search(condition);
 assertThat(result).extracting("username").containsExactly("member4");
}

 

 

 

 

 

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

복사했습니다!