Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- jpa
- JavaScript
- spring
- 코드 테스트
- MySQL
- 프로그래머스
- BACK-END
- 자바스크립트
- 쿼리
- 디자인 패턴
- 알고리즘
- 백엔드
- 자바
- 스프링부트
- 오라클
- oracle
- jsp
- node.js
- web
- java
- 데이터베이스
- 미니정리
- 정리
- Next.js
- 서버
- 스프링
- 프런트엔드
- SQL
- 프론트엔드
- 코드테스트
Archives
- Today
- Total
참치코더의 꿈 메모장
Spring / Querydsl 초기 설정(setting)법 본문
728x90

- 쿼리dsl은 jpql로 작성되던 복잡하고 지저분한 쿼리를 자바코드 방식으로 손쉽게 작성할 수 있도록 도와주는 라이브러리다.
- 아래는 해당 Querydsl을 초기 설정하기 위한 방법이다.
1. querydsl 라이브러리를 build.gradle에 삽입한다.
// QueryDSL
implementation("com.querydsl:querydsl-jpa:5.1.0:jakarta")
annotationProcessor("com.querydsl:querydsl-apt:5.1.0:jakarta")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")
annotationProcessor("jakarta.persistence:jakarta.persistence-api")
2. QueryDSL Q 클래스 생성 위치를 지정한다.
// QueryDSL Q클래스 생성 위치
val generated = "src/main/generated"
sourceSets{
main {
java {
srcDirs("src/main/java", generated)
}
}
}
tasks.withType<JavaCompile> {
options.generatedSourceOutputDirectory = file(generated)
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
3. src 파일 내부에 저장했다면 .gitignore 파일 설정에 해당 폴더 경로를 입력해 git에 커밋되지 않도록 한다.
- 어짜피 빌드하면 계속 갱신되기 때문에, 쓸데없는 가비지 코드가 되버린다....
# QueryDSL
src/main/generated
4. Test 파일에서 해당 querydsl 파일이 잘 동작하는지 확인한다.
package querydsl;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import querydsl.entity.Hello;
import querydsl.entity.QHello;
import static org.assertj.core.api.Assertions.*;
@SpringBootTest
@Transactional
class QuerydslApplicationTests {
@Autowired
EntityManager em;
@Test
void contextLoads() {
Hello hello = new Hello();
em.persist(hello);
JPAQueryFactory query = new JPAQueryFactory(em);
QHello qHello = QHello.hello;
Hello result = query
.selectFrom(qHello)
.fetchOne();
assertThat(result).isEqualTo(hello);
assertThat(result.getId()).isEqualTo(hello.getId());
}
}
728x90
'Spring' 카테고리의 다른 글
| Spring / Spring Boot 메시지(MessageSource)와 국제화 정리 (0) | 2026.01.26 |
|---|---|
| Spring / Thymeleaf 라디오 버튼 & 셀렉트 박스 정리 (0) | 2026.01.23 |
| Thymeleaf / 단일 체크 박스, 다중 체크 박스 정리 (0) | 2026.01.19 |
| Spring / Thymeleaf 폼(Form) 정리 - Spring MVC 연동 (0) | 2026.01.16 |
| Spring / Thypeleaf 기본정리 (블록, 자바스크립트 인라인, 템플릿 조각) (0) | 2026.01.13 |
Comments