์ฌ์ฉ์ ์ ์ ์ธํฐํ์ด์ค ์์ฑ
public interface MemberRepositoryCustom {
List<MemberTeamDto> search(MemberSearchCondition condition);
}
- SpringDataJPA๊ฐ ์ ๊ณตํ๋ ๋ฉ์๋ ์ธ์ ํ์ํ ๋ฉ์๋๊ฐ ์์ ๋
์ฌ์ฉ์ ์ ์ ์ธํฐํ์ด์ค ๊ตฌํ
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);
}
}
์คํ๋ง ๋ฐ์ดํฐ ๋ฆฌํฌ์งํ ๋ฆฌ์ ์ฌ์ฉ์ ์ ์ ์ธํฐํ์ด์ค ์์
public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {
List<Member> findByUsername(String username);
}