SpringBoot, Java11, Gradle 7.5
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);
}
}
package com.anselmo.veganus.location.repository;
import org.springframework.stereotype.Repository;
@Repository
public interface LocationCustomRepository {
}
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();
}
}
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{
}
// 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 끝 -------------------------