JPA
[Querydsl] ์ ๋ ฌ, ํ์ด์ง, ์ง๊ณ ํจ์, ๊ทธ๋ฃน ํจ์
mangdo
2021. 8. 4. 21:49
๐ฑ ์ ๋ ฌ
: orderBy์ด์ฉ
@Test
public void sort() {
em.persist(new Member(null, 100));
em.persist(new Member("member5", 100));
em.persist(new Member("member6", 100));
List<Member> result = queryFactory
.selectFrom(member)
.where(member.age.eq(100))
.orderBy(member.age.desc(), member.username.asc().nullsLast())
.fetch();
Member member5 = result.get(0);
Member member6 = result.get(1);
Member memberNull = result.get(2);
}
๐ฑ ํ์ด์ง
: offset() -> ํ์ด์ง์ ์์์ ์ง์
: limit() -> ํ์ด์ง ํฌ๊ธฐ
@Test
public void paging1() {
em.persist(new Member(null, 100));
em.persist(new Member("member5", 100));
em.persist(new Member("member6", 100));
List<Member> result = queryFactory
.selectFrom(member)
.orderBy(member.username.desc())
.offset(1) //0๋ถํฐ ์์(zero index)
.limit(5) //์ต๋ 5๊ฑด ์กฐํ
.fetch();
assertThat(result.size()).isEqualTo(5);
}
@Test
public void paging2() {
em.persist(new Member(null, 100));
em.persist(new Member("member2", 100));
em.persist(new Member("member3", 100));
em.persist(new Member("member4", 100));
QueryResults<Member> queryResults = queryFactory
.selectFrom(member)
.orderBy(member.username.desc())
.offset(1)
.limit(2)
.fetchResults();
assertThat(queryResults.getTotal()).isEqualTo(4);
assertThat(queryResults.getLimit()).isEqualTo(2);
assertThat(queryResults.getOffset()).isEqualTo(1);
assertThat(queryResults.getResults().size()).isEqualTo(2);
}
๐ฑ ์ง๊ณํจ์
: ๊ฒฐ๊ณผ ๋ ์ฝ๋๋ค์ ๋ํ ์ฐ์ ์ ์ธ ์ฐ์ฐ์ ํ ๊ฒฐ๊ณผ(์ค์นผ๋ผ)๊ฐ์ ์ถ๋ ฅํฉ๋๋ค.
์ฌ๋ฌ๊ฐ์ง๊ฐ ์์ง๋ง ๊ทธ ์ค count, sum, max, min, avg๊ฐ ๋ง์ด ์ฌ์ฉ๋ฉ๋๋ค.
์ฃผ์ ํ ์ ์ ์ง๊ณํฉ์๋ ์งํฉ ์์ NULL ๊ฐ์ด ์์ ๊ฒฝ์ฐ ์ด๋ฅผ ์ ์ธํ๊ณ ์ฒ๋ฆฌํ๋ค๋ ๊ฒ ์ ๋๋ค.
@Test
public void aggregation() throws Exception {
em.persist(new Member(null, 100));
em.persist(new Member("member2", 100));
em.persist(new Member("member3", 110));
em.persist(new Member("member4", 90));
// Querydsl ์ด ์ ๊ณตํ๋ Tuple
// member ๋จ์ผ ํ์
์ ์กฐํํ๋ ๊ฒ์ด ์๋ ๋ฐ์ดํฐ ํ์
์ด ์ฌ๋ฌ๊ฐ์ผ๋๋ ํํ์ ์ฌ์ฉํ๋ค
// ์ค๋ฌด์์๋ ํํ์ ์ฌ์ฉํ์ง์๊ณ DTO๋ฅผ ์ฃผ๋ก ์ด๋ค.
List<Tuple> result = queryFactory
.select(member.count(),
member.age.sum(),
member.age.avg(),
member.age.max(),
member.age.min())
.from(member)
.fetch();
Tuple tuple = result.get(0);
assertThat(tuple.get(member.count())).isEqualTo(4);
assertThat(tuple.get(member.age.sum())).isEqualTo(400);
assertThat(tuple.get(member.age.avg())).isEqualTo(100);
assertThat(tuple.get(member.age.max())).isEqualTo(110);
assertThat(tuple.get(member.age.min())).isEqualTo(90);
}
๐ฑ ๊ทธ๋ฃนํ
: GROUP BY ๋ช ๋ น์ด๋ฅผ ํตํด ํน์ ์ปฌ๋ผ์ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํ ํ ์ ์์ต๋๋ค.
: GROUP BY ์ ์์ ์กฐ๊ฑด์ ์ฃผ๋ ค๋ฉด WHERE๊ฐ ์๋, HAVING ์ ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
@Test
public void group() throws Exception {
List<Tuple> result = queryFactory
.select(team.name, member.age.avg())
.from(member)
.join(member.team, team)
.groupBy(team.name)
.having() // groupBy์ ์กฐ๊ฑด
.fetch();
Tuple teamA = result.get(0);
Tuple teamB = result.get(1);
assertThat(teamA.get(team.name)).isEqualTo("teamA");
assertThat(teamA.get(member.age.avg())).isEqualTo(15);
assertThat(teamB.get(team.name)).isEqualTo("teamB");
assertThat(teamB.get(member.age.avg())).isEqualTo(35);
}