728x90
반응형
2022.06.24 - [Spring/Querydsl] - Query DSL 시작하기
조건에 따른 값을 지정해주는 문법
DB
- Team Table
- Member Table
Simple case
@Repository
public class MemberRepositorySupport extends QuerydslRepositorySupport {
private final JPAQueryFactory queryFactory;
public MemberRepositorySupport(JPAQueryFactory queryFactory) {
super(Member.class);
this.queryFactory = queryFactory;
}
public List<String> findMemberCase(){
return queryFactory
.select(member.age
.when(10).then("10살")
.when(20).then("20살")
.otherwise("0")
)
.from(member)
.fetch();
}
}
/* ================= */
@SpringBootTest
class MemberRepositorySupportTest {
@Autowired
private MemberRepositorySupport memberRepositorySupport;
@Test
void simpleCase(){
List<String> memberCase = memberRepositorySupport.findMemberCase();
memberCase.forEach(System.out::println);
}
}
/* ================= */
/*
결과
10살
20살
0
*/
Complex case
@Repository
public class MemberRepositorySupport extends QuerydslRepositorySupport {
private final JPAQueryFactory queryFactory;
public MemberRepositorySupport(JPAQueryFactory queryFactory) {
super(Member.class);
this.queryFactory = queryFactory;
}
}
/* ================= */
@SpringBootTest
class MemberRepositorySupportTest {
@Autowired
private MemberRepositorySupport memberRepositorySupport;
@Test
void complexCase(){
List<Tuple> memberComplexCase = memberRepositorySupport.findMemberComplexCase();
for(Tuple tuple : memberComplexCase){
System.out.println(tuple);
}
}
}
/* ================= */
/*
결과
[test1, 10, 미성년자]
[test2, 20, 20살 성인]
[test3, 12, 미성년자]
*/
참고
https://hjhng125.github.io/querydsl/querydsl-case/
파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음
반응형
'Spring > Querydsl' 카테고리의 다른 글
Query DSL 시작하기 (0) | 2022.12.12 |
---|---|
Query DSL - Dynamic Query (0) | 2022.07.06 |
Query DSL - Paging (0) | 2022.07.06 |
Query DSL - Subquery (0) | 2022.07.05 |
Query DSL - Join (0) | 2022.07.03 |