Copy @ Repository
public class MemberJpaRepository {
private final EntityManager em;
public MemberJpaRepository ( EntityManager em) {
this . em = em;
}
public List < Member > findAll () {
return em . createQuery ( "select m from Member m" , Member . class )
. getResultList ();
}
public List < Member > findByUsername ( String username) {
return em . createQuery ( "select m from Member m where m.username = :username" , Member . class )
. setParameter ( "username" , username)
. getResultList ();
}
}
Copy @ Repository
public class MemberJpaRepository {
private final EntityManager em;
private final JPAQueryFactory queryFactory;
public MemberJpaRepository ( EntityManager em) {
this . em = em;
this . queryFactory = new JPAQueryFactory(em) ;
}
public List < Member > findAll () {
return queryFactory . selectFrom (member) . fetch ();
}
public List < Member > findByUsername ( String username) {
return queryFactory
. selectFrom (member)
. where ( member . username . eq (username))
. fetch ();
}
}
Copy @ Configuration
public class QueryDSLConfig {
@ Bean
JPAQueryFactory jpaQueryFactory ( EntityManager em) {
return new JPAQueryFactory(em) ;
}
}
Copy @ Data
public class MemberSearchParams {
//회원명, 팀명, 나이(ageGoe, ageLoe)
private String username;
private String teamName;
private Integer ageGoe;
private Integer ageLoe;
}
Copy public List< MemberTeamDto > searchByBuilder( MemberSearchParams params) {
BooleanBuilder builder = new BooleanBuilder() ;
if ( StringUtils . hasText ( params . getUsername ())) {
builder . and ( member . username . eq ( params . getUsername ()));
}
if ( StringUtils . hasText ( params . getTeamName ())) {
builder . and ( team . name . eq ( params . getTeamName ()));
}
if ( condition . getAgeGoe () != null ) {
builder . and ( member . age . goe ( params . getAgeGoe ()));
}
if ( condition . getAgeLoe () != null ) {
builder . and ( member . age . loe ( params . getAgeLoe ()));
}
return queryFactory
. select ( new QMemberTeamDto(
member . id ,
member . username ,
member . age ,
team . id ,
team . name ) )
. from (member)
. leftJoin ( member . team , team)
. where (builder) // 빌더를 통해 조건절 설정
. fetch ();
}
Copy 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);
}