Spring Boot QueryDSL 세팅

LeeJaeJun·2022년 8월 10일
0
post-custom-banner

환경

SpringBoot, Java11, Gradle 7.5

코드

Config 파일생성

package com.anselmo.veganus.location.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Configuration
public class LocationQuerydslConfig {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory(){
        return new JPAQueryFactory(entityManager);
    }
}

QueryDSL 연결용 CustomRepository 생성

package com.anselmo.veganus.location.repository;

import org.springframework.stereotype.Repository;

@Repository
	public interface LocationCustomRepository {
}

QueryDSL 작성용 Class 생성

package com.anselmo.veganus.location.repository;

import com.anselmo.veganus.location.dto.StoreDto;
import com.anselmo.veganus.location.repository.LocationCustomRepository;
import com.anselmo.veganus.location.repository.entity.LocationEntity;
import com.anselmo.veganus.location.repository.entity.QLocationEntity;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class LocationQueryDSL implements LocationCustomRepository {

    private final JPAQueryFactory jpaQueryFactory;

    public List<LocationEntity> findByAll(){
        QLocationEntity qLocationEntity = QLocationEntity.locationEntity;
        return jpaQueryFactory.selectFrom(qLocationEntity).having().fetch();
    }
}

기본 Repository extends 추가

package com.anselmo.veganus.location.repository;

import com.anselmo.veganus.location.repository.entity.LocationEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LocationRepository extends JpaRepository<LocationEntity,Long> ,LocationCustomRepository{

}

build.gradle

// querydsl -------------------------
buildscript {
    ext {
        queryDslVersion = "5.0.0"
    }
}

plugins {
    id 'org.springframework.boot' version '2.7.2'
    id 'io.spring.dependency-management' version '1.0.12.RELEASE'
    id 'java'

    // querydsl -------------------------
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
dependencies {
    // querydsl -------------------------
    implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    implementation "com.querydsl:querydsl-apt:${queryDslVersion}"
}
// querydsl 시작 -------------------------
def querydslDir = "$buildDir/generated/querydsl"

querydsl {
    jpa = true
    querydslSourcesDir = querydslDir
}

sourceSets {
    main.java.srcDir querydslDir
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    querydsl.extendsFrom compileClasspath
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}
// querydsl 끝 -------------------------
post-custom-banner

0개의 댓글