숨밈
들숨에 건강을 날숨에 재력을
숨밈
전체 방문자
오늘
어제
  • 분류 전체보기 (55)
    • 💻 프로젝트 (8)
      • 🍝 홍잇 (5)
      • 🏕 캠퍼 (3)
    • 👩‍💻 개발 (30)
      • ⚙️ BACKEND (16)
      • 🖥 FRONTEND (3)
      • 📡 DEVOPS (7)
      • 💡SOFTWARE (4)
    • 📑 개발 이론 (13)
      • 🚎 JAVA (1)
      • 🌱 SPRING (12)
    • 📚 CS (2)
      • 🔎 Infra (2)
    • 📔 회고 (2)

블로그 메뉴

  • 홈
  • 태그
  • 글쓰기

인기 글

태그

  • notion
  • 스프링부트
  • 자바스크립트
  • 타임리프
  • django-rest-auth_custom
  • 프리코스
  • static final
  • springboot
  • django-auth
  • Tistory
  • jsp
  • django-rest-auth
  • Django
  • querydsl
  • 스프링

티스토리

hELLO · Designed By 정상우.
숨밈

들숨에 건강을 날숨에 재력을

[SpringBoot] Querydsl 설정
👩‍💻 개발/⚙️ BACKEND

[SpringBoot] Querydsl 설정

2022. 8. 6. 12:30

jpa에서 제공하는 @Query로는 다양한 조건으로 조회를 하는데에 한계가 있다.

그래서 이를 해결하기 위해 정적 타입을 지원하는 조회 프레임워크를 사용하는데 Querydsl이 유명한 조회 프레임워크 중 하나로 이를 적용하여 프로젝트를 진행해 보고자 한다.

 

개발환경:

  • inteliJ
  • Spring Boot 2.7.1
  • Gradle
  • Lombok

1. Gradle 설정


buildscript {
	ext {
		queryDslVersion = "5.0.0"
	}
}

plugins {
	...
	//querydsl 추가
	id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
	id 'java'
}
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
}
compileQuerydsl{
	options.annotationProcessorPath = configurations.querydsl
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
	querydsl.extendsFrom compileClasspath
}
//querydsl 추가 끝

 

 

build.gradle에 위 코드를 넣어준 후

compileJava를 눌러주면 Querydsl 관련 compile들이 생성되고 

그 후에 compileQuerydsl을 눌러주면 build/generated/querydsl 디렉터리에 

Querydsl에서 사용할 수 있는 Q 클래스들이 생성된다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. Java Config 


package Backend.HIFI.config;

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 QuerydslConfiguration {

    @PersistenceContext
    private EntityManager entityManager;

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

 

위 설정으로 해당 프로젝트는 어디에서나 JpaQueryFactory를 주입받아 Querydsl을 사용할 수 있게 된다.

 

3. 주의 사항


Qclass를 담는 generated파일은 자동 생성되는 파일이니 무조건! .gitignored에 추가해야 한다.

 

사용방법은 다음 포스팅에서 다루고자 한다.

 

끝!

저작자표시 (새창열림)

'👩‍💻 개발 > ⚙️ BACKEND' 카테고리의 다른 글

[Spring/Java] modelMapper  (4) 2022.08.11
[SpringBoot] Jpa Repository와 Querydsl 사용방법  (0) 2022.08.06
[SpringBoot] Thymleaf css, js 파일 추가 및 사용법  (0) 2022.07.27
[SpringBoot] 스프링부트에서 Thymeleaf 와 JSP 같이 쓰기  (0) 2022.07.24
[SpringBoot] 스프링부트로 JSP 사용하기  (0) 2022.07.22
    '👩‍💻 개발/⚙️ BACKEND' 카테고리의 다른 글
    • [Spring/Java] modelMapper
    • [SpringBoot] Jpa Repository와 Querydsl 사용방법
    • [SpringBoot] Thymleaf css, js 파일 추가 및 사용법
    • [SpringBoot] 스프링부트에서 Thymeleaf 와 JSP 같이 쓰기
    숨밈
    숨밈
    기술블로그

    티스토리툴바