@Test
public void dynamicQueryWhereParam() {
String usernameParam = null;
Integer ageParam = 10;
List<Member> result = queryFactory
.selectFrom(member)
// .where(usernameEq(usernameParam), ageEq(ageParam))
.where(allEq(usernameParam, ageParam))
.fetch();
assertThat(result.size()).isEqualTo(1);
}
private BooleanExpression usernameEq(String usernameParam) {
if (usernameParam == null) {
return Expressions.TRUE;
}
return member.username.eq(usernameParam);
}
private BooleanExpression ageEq(Integer ageParam) {
if (ageParam == null) {
return Expressions.TRUE;
}
return member.age.eq(ageParam);
}
private BooleanExpression allEq(String usernameParam, Integer ageParam) {
return usernameEq(usernameParam).and(ageEq(ageParam));
}select m.* from member m where true and m.age = 10;위와 같이 Expressions.TRUE를 반환하게 만들면 쿼리에 true가 포함돼서 실무에서 사용하기에는 좀 그럴까요?