jpql vs querydsl
public void jpql() {
String username = "kim";
String query = "select m from Member m" + "where m.username = :username";
List<Member> result = em.createQuery(query, Member.class).getResultList();
}
public void querydsl() {
String username = "kim";
List<Member> result = queryFactory.select(member)
.from(member)
.where(member.username.eq(username))
.fetch();
}
// buildscript 추가
buildscript {
ext {
queryDslVersion = "5.0.0"
}
}
plugins {
id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
//querydsl 추가
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
id 'java'
}
group = 'study'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
//querydsl 추가
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}"
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
// 테스트에서 lombok 사용
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
//querydsl 추가 시작
def querydslDir = "$buildDir/generated/querydsl"
querydsl {
jpa = true
querydslSourcesDir = querydslDir
}
sourceSets {
main.java.srcDir querydslDir
}
configurations {
querydsl.extendsFrom compileClasspath
}
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
//querydsl 추가 끝
검증용 엔티티 생성
package study.querydsl.entity;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Getter @Setter
public class Hello {
@Id @GeneratedValue
private Long id;
}
추가한 후, gradle/querydsl/Tasks/other/compileQuerydsl
버튼을 클릭하여 Querydsl이 잘 실행되는지 체크합니다
성공!
def querydslDir = "$buildDir/generated/querydsl"
build/generated/querydsl
이 추가되었습니다
QHello
클래스가 생성되었습니다
./gradlew clean compileQuerydsl
실행1,2 중 하나를 실행할시 QHello 클래스가 생성됩니다
@Transactional
@SpringBootTest
class QuerydslApplicationTests {
@Autowired
EntityManager em;
@Test
void contextLoads() {
Hello hello = new Hello();
em.persist(hello);
JPAQueryFactory query = new JPAQueryFactory(em);
QHello qHello = QHello.hello; //Querydsl Q타입 동작 확인
Hello result = query
.selectFrom(qHello)
.fetchOne();
assertThat(result).isEqualTo(hello);
//lombok 동작 확인 (hello.getId()
assertThat(result.getId()).isEqualTo(hello.getId());
}
}
참고
/gradlew dependencies --configuration compileClasspath
querydslSourcesDir = querydslDir
, main.java.srcDir querydslDir
: id들이 자동으로 자기 소스폴더에 import 됩니다application.yml
spring:
datasource:
url: jdbc:h2:tcp://localhost//Users/leekyoungchang/Desktop/Study/Computer/Spring/JPA(pdf, ppt)/Querydsl/db/querydsldb
username: sa
password: 1234
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
# show_sql: true
format_sql: true
logging.level:
org.hibernate.SQL: debug
# org.hibernate.type: trace
spring.jpa.hibernate.ddl-auto: create
: 이 옵션은 애플리케이션 실행 시점에 테이블을 drop
하고, 다시 생성합니다