jpashop : querydsl
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
//querydsl 추가
|
||||
buildscript {
|
||||
dependencies {
|
||||
classpath("gradle.plugin.com.ewerk.gradle.plugins:querydsl-plugin:1.0.10")
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id 'org.springframework.boot' version '2.4.2'
|
||||
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
|
||||
id 'java'
|
||||
}
|
||||
|
||||
apply plugin: 'io.spring.dependency-management'
|
||||
apply plugin: "com.ewerk.gradle.plugins.querydsl"
|
||||
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '11'
|
||||
@@ -37,8 +47,34 @@ dependencies {
|
||||
testImplementation("org.junit.vintage:junit-vintage-engine") {
|
||||
exclude group: "org.hamcrest", module: "hamcrest-core"
|
||||
}
|
||||
|
||||
//querydsl 추가
|
||||
implementation 'com.querydsl:querydsl-jpa'
|
||||
implementation 'com.querydsl:querydsl-apt'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
//querydsl 추가
|
||||
//def querydslDir = 'src/main/generated'
|
||||
def querydslDir = "$buildDir/generated/querydsl"
|
||||
querydsl {
|
||||
library = "com.querydsl:querydsl-apt"
|
||||
jpa = true
|
||||
querydslSourcesDir = querydslDir
|
||||
}
|
||||
sourceSets {
|
||||
main {
|
||||
java {
|
||||
srcDirs = ['src/main/java', querydslDir]
|
||||
}
|
||||
}
|
||||
}
|
||||
compileQuerydsl{
|
||||
options.annotationProcessorPath = configurations.querydsl
|
||||
}
|
||||
configurations {
|
||||
querydsl.extendsFrom compileClasspath
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.example.jpashop.repository;
|
||||
|
||||
import com.example.jpashop.domain.Member;
|
||||
import com.example.jpashop.domain.*;
|
||||
import com.example.jpashop.domain.Order;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -86,14 +88,31 @@ public class OrderRepository {
|
||||
}
|
||||
|
||||
public List<Order> findAll(OrderSearch orderSearch) {
|
||||
JPAQueryFactory query = new JPAQueryFactory(em);
|
||||
QOrder order = QOrder.order;
|
||||
QMember member = QMember.member;
|
||||
|
||||
return em.createQuery("select o from Order o join o.member m" +
|
||||
" where o.status = :status" +
|
||||
" and m.name like :name", Order.class)
|
||||
.setParameter("status", orderSearch.getOrderStatus())
|
||||
.setParameter("name", orderSearch.getMemberName())
|
||||
.setMaxResults(1000) // 최대 1000건건
|
||||
.getResultList();
|
||||
return query
|
||||
.select(order)
|
||||
.from(order)
|
||||
.join(order.member, member)
|
||||
.where(statusEq(orderSearch.getOrderStatus()), nameLike(orderSearch.getMemberName()))
|
||||
.limit(1000)
|
||||
.fetch();
|
||||
}
|
||||
|
||||
private BooleanExpression nameLike(String memberName) {
|
||||
if (!StringUtils.hasText(memberName)) {
|
||||
return null;
|
||||
}
|
||||
return QMember.member.name.like(memberName);
|
||||
}
|
||||
|
||||
private BooleanExpression statusEq(OrderStatus statusCond) {
|
||||
if (statusCond == null) {
|
||||
return null;
|
||||
}
|
||||
return QOrder.order.status.eq(statusCond);
|
||||
}
|
||||
|
||||
public List<Order> findAllWithMemberDelivery(int offset, int limit) {
|
||||
|
||||
@@ -60,6 +60,6 @@ public class OrderService {
|
||||
|
||||
// 검색
|
||||
public List<Order> findOrders(OrderSearch orderSearch) {
|
||||
return orderRepository.findAllByString(orderSearch);
|
||||
return orderRepository.findAll(orderSearch);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user