Post

QueryDSL 기본문법

검색 조건 쿼리

리스트 조회

1
2
3
4
5
6
7
8
9
10
public List<EntityClass> retrieveAllEntities() {

        JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

        List<EntityClass> result = queryFactory
            .selectFrom(entityClass)
            .fetch();

        return result;
    } 

selectFrom(QType) : 해당 엔티티에서 모든 컬럼 조회

단일 조회

1
2
3
4
5
6
7
8
9
10
11
12
    public EntityClass fetchSingleEntity(String fieldValue) {
        
        JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);


        EntityClass result = queryFactory
            .selectFrom(entityClass)
            .where(entityClass.field.eq(fieldValue))
            .fetchOne();

        return result;
    }

fetch(): 리스트를 조회, 데이터가 존재하지 않을 경우 빈 리스트를 반환

fetchOne(): 단일 조회, 결과가 없으면 Null, 결과가 단일이 아닐 경우 Exception 발생

fetchFirst(): 단일 조회, 결과가 여러 개일 경우 맨 처음 레코드 반환

컬럼 선택 조회

1
2
3
4
5
6
7
8
9
10
public List<Object[]> retrieveSpecificColumns() {
        JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

        List<Object[]> result = queryFactory
            .select(entityClass.field1, entityClass.field2)
            .from(entityClass)
            .fetch();

        return result;
    }

select(Qtype Column1 , Qtype Column2):엔티티의 해당 컬럼(들) 조회

from(Qtype) : 조회 대상 엔티티

조건

1
2
3
4
5
6
7
8
9
10
   public List<EntityClass> retrieveEntitiesWithCondition(String name) {
        JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

        List<EntityClass> result = queryFactory
            .selectFrom(entityClass)
            .where(entityClass.name.eq(name))  
            .fetch();

        return result;
    }

where(조건) : 해당 조건과 일치하는 레코드(들)을 반환

복합조건

eq(): 일치 하는지를 수행

1
BooleanExpression condition = entity.field.eq("value");

ne(): 일치하지 않는지를 수행

1
2
BooleanExpression condition = entity.field.ne("value");

like() : 부분 문자열매칭을 수행

1
2
BooleanExpression condition = entity.field.like("%value%"); // value 가 있는지
BooleanExpression condition = entity.field.like("value%"); //value로 시작

startsWith(): 첫글자 일치를 수행

1
2
BooleanExpression condition = entity.field.startsWith("prefix");

endsWith(): 끝글자 일치를 수행

1
2
BooleanExpression condition = entity.field.endsWith("suffix");

isNull(): NULL 여부를 확인

1
2
BooleanExpression condition = entity.field.isNull();

isNotNull(): NOT NULL 여부를 확인

1
2
BooleanExpression condition = entity.field.isNotNull();

and(), or(): 조건들을 결합

1
2
3
4
BooleanExpression condition = entity.field.eq("value")
    .and(entity.anotherField.startsWith("prefix"))
    .or(entity.someField.isNull());

in(): 여러 값 중 하나와 일치하는지 확인

1
2
List<String> values = Arrays.asList("value1", "value2", "value3");
BooleanExpression condition = entity.field.in(values);

notIn(): 여러 값 중 하나와 안하는지 확인

1
2
List<String> values = Arrays.asList("value1", "value2", "value3");
BooleanExpression condition = entity.field.notIn(values);

between() : 특정 필드의 값을 범위로 지정하는데 사용

1
2
3
4
5
List<EntityClass> result = queryFactory
    .selectFrom(entityClass)
    .where(entityClass.field.between(minValue, maxValue))
    .fetch();

lt() : 특정 필드가 지정된 값보다 작은지 확인

1
2
3
4
5
6
List<EntityClass> result = queryFactory
    .selectFrom(entityClass)
    .where(entityClass.field.lt(thresholdValue))
    .fetch();


lte() : 특정 필드가 지정된 값보다 작거나 같은지 확인

1
2
3
4
5
List<EntityClass> result = queryFactory
    .selectFrom(entityClass)
    .where(entityClass.field.lte(thresholdValue))
    .fetch();

gt() : 특정 필드가 지정된 값보다 큰지 확인

1
2
3
4
List<EntityClass> result = queryFactory
    .selectFrom(entityClass)
    .where(entityClass.field.gt(thresholdValue))
    .fetch();

gte() : 특정 필드가 지정된 값보다 크거나 같은지 확인

1
2
3
4
5
List<EntityClass> result = queryFactory
    .selectFrom(entityClass)
    .where(entityClass.field.gte(thresholdValue))
    .fetch();

This post is licensed under CC BY 4.0 by the author.