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 {
|
plugins {
|
||||||
id 'org.springframework.boot' version '2.4.2'
|
id 'org.springframework.boot' version '2.4.2'
|
||||||
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
|
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
|
||||||
id 'java'
|
id 'java'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apply plugin: 'io.spring.dependency-management'
|
||||||
|
apply plugin: "com.ewerk.gradle.plugins.querydsl"
|
||||||
|
|
||||||
group = 'com.example'
|
group = 'com.example'
|
||||||
version = '0.0.1-SNAPSHOT'
|
version = '0.0.1-SNAPSHOT'
|
||||||
sourceCompatibility = '11'
|
sourceCompatibility = '11'
|
||||||
@@ -37,8 +47,34 @@ dependencies {
|
|||||||
testImplementation("org.junit.vintage:junit-vintage-engine") {
|
testImplementation("org.junit.vintage:junit-vintage-engine") {
|
||||||
exclude group: "org.hamcrest", module: "hamcrest-core"
|
exclude group: "org.hamcrest", module: "hamcrest-core"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//querydsl 추가
|
||||||
|
implementation 'com.querydsl:querydsl-jpa'
|
||||||
|
implementation 'com.querydsl:querydsl-apt'
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
useJUnitPlatform()
|
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;
|
package com.example.jpashop.repository;
|
||||||
|
|
||||||
import com.example.jpashop.domain.Member;
|
import com.example.jpashop.domain.*;
|
||||||
import com.example.jpashop.domain.Order;
|
import com.example.jpashop.domain.Order;
|
||||||
|
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||||
|
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
@@ -86,14 +88,31 @@ public class OrderRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findAll(OrderSearch orderSearch) {
|
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" +
|
return query
|
||||||
" where o.status = :status" +
|
.select(order)
|
||||||
" and m.name like :name", Order.class)
|
.from(order)
|
||||||
.setParameter("status", orderSearch.getOrderStatus())
|
.join(order.member, member)
|
||||||
.setParameter("name", orderSearch.getMemberName())
|
.where(statusEq(orderSearch.getOrderStatus()), nameLike(orderSearch.getMemberName()))
|
||||||
.setMaxResults(1000) // 최대 1000건건
|
.limit(1000)
|
||||||
.getResultList();
|
.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) {
|
public List<Order> findAllWithMemberDelivery(int offset, int limit) {
|
||||||
|
|||||||
@@ -60,6 +60,6 @@ public class OrderService {
|
|||||||
|
|
||||||
// 검색
|
// 검색
|
||||||
public List<Order> findOrders(OrderSearch orderSearch) {
|
public List<Order> findOrders(OrderSearch orderSearch) {
|
||||||
return orderRepository.findAllByString(orderSearch);
|
return orderRepository.findAll(orderSearch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user