@Test
public void findDtoByJPQL() {
List<MemberDto> list = em.createQuery("select new study.querydsl.dto.MemberDto(m.username, m.age)" +
" from Member m", MemberDto.class).getResultList();
for (MemberDto memberDto : list) {
System.out.println("memberDto = " + memberDto);
}
@Test
public void findDtoBySetter() {
List<MemberDto> fetch = queryFactory
.select(Projections.bean(MemberDto.class,
member.username,
member.age))
.from(member)
.fetch();
for (MemberDto memberDto : fetch) {
System.out.println("memberDto = " + memberDto);
}
}
@Test
public void findDtoByField() {
List<MemberDto> fetch = queryFactory
.select(Projections.fields(MemberDto.class,
member.username,
member.age))
.from(member)
.fetch();
for (MemberDto memberDto : fetch) {
System.out.println("memberDto = " + memberDto);
}
}
@Test
public void findUserDto() {
QMember memberSub = new QMember("memberSub");
List<UserDto> fetch = queryFactory
.select(Projections.fields(UserDto.class,
member.username.as("name"),
// member.age,
ExpressionUtils.as(
JPAExpressions
.select(memberSub.age.max())
.from(memberSub), "age")
)
)
.from(member)
.fetch();
for (UserDto userDto : fetch) {
System.out.println("userDto = " + userDto);
}
}
@Test
public void findDtoByConstructor() {
List<UserDto> fetch = queryFactory
.select(Projections.constructor(UserDto.class,
member.username,
member.age))
.from(member)
.fetch();
for (UserDto userDto : fetch) {
System.out.println("memberDto = " + userDto);
}
}
package study.querydsl.dto;
import com.querydsl.core.annotations.QueryProjection;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class MemberDto {
private String username;
private int age;
@QueryProjection
public MemberDto(String username, int age) {
this.username = username;
this.age = age;```
}
}
@Test
public void findDtoByQueryProjection() {
List<MemberDto> fetch = queryFactory
.select(new QMemberDto(member.username, member.age))
.from(member)
.fetch();
for (MemberDto memberDto : fetch) {
System.out.println("memberDto = " + memberDto);
}
}
@Test
public void dynamicQuery_booleanBuilder() {
String usernameParam = "member1";
Integer ageParam = null;
List<Member> result = searchMember1(usernameParam, ageParam);
assertThat(result.size()).isEqualTo(1);
}
private List<Member> searchMember1(String usernameCond, Integer ageCond) {
BooleanBuilder builder = new BooleanBuilder();
if(usernameCond != null) {
builder.and(member.username.eq(usernameCond));
}
if(ageCond != null) {
builder.and(member.age.eq(ageCond));
}
return queryFactory
.selectFrom(member)
.where()
.fetch();
}
BooleanBuilder는 데이터베이스 쿼리를 동적으로 생성하기 위한 라이브러리이다. 일반적으로 SQL 쿼리를 작성할 때는 WHERE 조건절을 사용하여 특정 조건을 만족하는 데이터를 검색한다. 그러나 BooleanBuilder를 사용하면 여러 조건들을 조합하여 동적으로 WHERE 조건절을 생성할 수 있다.
BooleanBuilder를 사용하면 AND, OR, NOT과 같은 논리 연산자를 사용하여 여러 개의 조건을 조합할 수 있다. 또한, BooleanBuilder는 코드 가독성과 유지보수성을 높이는 데 도움이 되며, 복잡한 쿼리를 생성하기 위한 수고를 덜어준다.
BooleanBuilder는 다양한 데이터베이스와 호환되는 오픈 소스 라이브러리로 제공되며, Java와 Kotlin 등의 언어에서 사용할 수 있다.
@Test
public void dynamicQuery_WhereParam() {
String usernameParam = "member1";
Integer ageParam = null;
List<Member> result = searchMember2(usernameParam, ageParam);
assertThat(result.size()).isEqualTo(1);
}
private List<Member> searchMember2(String usernameCond, Integer ageCond) {
return queryFactory
.selectFrom(member)
// .where(usernameEq(usernameCond), ageEq(ageCond))
.where(allEq(usernameCond, ageCond))
.fetch();
}
private BooleanExpression usernameEq(String usernameCond) {
//where에 null이 들어가면 무시가 된다.
return usernameCond != null ? member.username.eq(usernameCond) : null;
}
private BooleanExpression ageEq(Integer ageCond) {
return ageCond != null ? member.age.eq(ageCond) : null;
}
private BooleanExpression allEq(String usernameCond, int ageCond) {
return usernameEq(usernameCond).and(ageEq(ageCond));
}
@Test
@Commit
public void bulkUpdate() {
long count = queryFactory
.update(member)
.set(member.username, "비회원")
.where(member.age.lt(28))
.execute();
//영속성 컨텍스트 초기화
em.flush();
em.clear();
System.out.println("count = " + count);
List<Member> fetch = queryFactory
.selectFrom(member)
.fetch();
for (Member fetch1 : fetch) {
System.out.println("fetch1 = " + fetch1);
}
}
@Test
public void bulkAdd() {
long count = queryFactory
.update(member)
// .set(member.age, member.age.add(10))
// .set(member.age, member.age.add(-10))
// .set(member.age, member.age.multiply(10))
.set(member.age, member.age.divide(10))
.execute();
System.out.println("count = " + count);
em.flush();
em.clear();
List<Member> fetch = queryFactory
.selectFrom(member)
.fetch();
for (Member fetch1 : fetch) {
System.out.println("fetch1 = " + fetch1);
}
}
@Test
public void bulkDelete() {
long count = queryFactory
.delete(member)
.where(member.age.gt(31))
.execute();
System.out.println("count = " + count);
em.flush();
em.clear();
List<Member> fetch = queryFactory
.selectFrom(member)
.fetch();
for (Member fetch1 : fetch) {
System.out.println("fetch1 = " + fetch1);
}
}
em.flush(); em.clear();
@Test
public void sqlFunction() {
List<String> result = queryFactory
.select(
Expressions.stringTemplate("function('replace', {0}, {1}, {2})",
member.username,
"member",
"M")
)
.from(member)
.fetch();
for (String s : result) {
System.out.println("s = " + s);
}
}
@Test
public void sqlFunctionToUpper() {
List<Tuple> fetch = queryFactory
.select(
// Expressions.stringTemplate("function('upper', {0})", member.username)
member.username.upper().as("name_upper"),
member.username.toUpperCase().as("name_toUpperCase")
)
.from(member)
.fetch();
for (Tuple tuple : fetch) {
System.out.println("tuple = " + tuple);
}
}
}