Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/query dsl 도입 및 적용하기 #433

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ tasks {
}
/******* End Spring Rest Docs *******/

/******* Start Query DSL Build Options *******/

val querydslDir = "src/main/generated"

sourceSets {
getByName("main").java.srcDirs(querydslDir)
}

tasks.withType<JavaCompile> {
options.generatedSourceOutputDirectory.set(file(querydslDir))
}

tasks.named("clean") {
doLast {
file(querydslDir).deleteRecursively()
}
}

/******* End Query DSL Build Options *******/

dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
Expand All @@ -87,6 +107,12 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-mail")
implementation("com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.1")
implementation("org.springframework.boot:spring-boot-starter-aop")
val queryDslVersion = "5.0.0"
implementation("com.querydsl:querydsl-jpa:${queryDslVersion}:jakarta")

annotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}:jakarta")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")
annotationProcessor("jakarta.persistence:jakarta.persistence-api")

asciidoctorExt("org.springframework.restdocs:spring-restdocs-asciidoctor")

Expand Down
Empty file modified gradlew
100755 → 100644
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gradlew, gradlew.bat 파일들이 올라갔는데 의도하신 건지 궁금합니당!

Empty file.
182 changes: 91 additions & 91 deletions gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface AttendanceRepository extends JpaRepository<Attendance, Long> {
public interface AttendanceRepository extends JpaRepository<Attendance, Long>, AttendanceRepositoryCustom {

boolean existsByMemberAndDate(Member member, LocalDate date);

Expand All @@ -23,9 +21,4 @@ public interface AttendanceRepository extends JpaRepository<Attendance, Long> {

List<Attendance> findAllByDateOrderByContinuousDayDesc(LocalDate date);

@Query("SELECT a FROM Attendance a "
+ "WHERE a.member = :member "
+ "AND a.date >= :localDate")
List<Attendance> findAllRecent(@Param("member") Member member, @Param("localDate") LocalDate localDate);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.keeper.homepage.domain.attendance.dao;

import com.keeper.homepage.domain.attendance.entity.Attendance;
import com.keeper.homepage.domain.member.entity.Member;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;

public interface AttendanceRepositoryCustom {

List<Attendance> findAllRecent(@Param("member") Member member, @Param("localDate") LocalDate localDate);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.keeper.homepage.domain.attendance.dao;


import com.keeper.homepage.domain.attendance.entity.Attendance;
import com.keeper.homepage.domain.member.entity.Member;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;

import static com.keeper.homepage.domain.attendance.entity.QAttendance.*;

@RequiredArgsConstructor
public class AttendanceRepositoryImpl implements AttendanceRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<Attendance> findAllRecent(@Param("member") Member member, @Param("localDate") LocalDate localDate) {
return queryFactory
.selectFrom(attendance)
.where(
attendance.member.eq(member)
.and(attendance.date.goe(localDate))
)
.fetch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CommentRepository extends JpaRepository<Comment, Long> {
public interface CommentRepository extends JpaRepository<Comment, Long>, CommentRepositoryCustom {

@Modifying
@Query("UPDATE Comment c "
+ "SET c.member = :virtualMember "
+ "WHERE c.member = :member")
void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.keeper.homepage.domain.comment.dao;

import com.keeper.homepage.domain.member.entity.Member;
import org.springframework.data.repository.query.Param;

public interface CommentRepositoryCustom {

void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.keeper.homepage.domain.comment.dao;

import com.keeper.homepage.domain.member.entity.Member;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.query.Param;

import static com.keeper.homepage.domain.comment.entity.QComment.*;

@RequiredArgsConstructor
public class CommentRepositoryImpl implements CommentRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Modifying
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 어노테이션은 어떤 역할을 하나요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조회를 제외한 쿼리를 직접 작성할 경우에 붙여줘야 하는 걸로 압니다! (맞나용?)

@Override
public void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember) {
jpaQueryFactory
.update(comment)
.set(comment.member, virtualMember)
.where(comment.member.eq(member))
.execute();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ElectionRepository extends JpaRepository<Election, Long> {
public interface ElectionRepository extends JpaRepository<Election, Long>, ElectionRepositoryCustom {

Optional<Election> findByIdAndIdNot(Long electionId, Long virtualId);

Page<Election> findAllByIdNot(long virtualId, Pageable pageable);

@Modifying
@Query("UPDATE Election e "
+ "SET e.member = :virtualMember "
+ "WHERE e.member = :member")
void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.keeper.homepage.domain.election.dao;

import com.keeper.homepage.domain.member.entity.Member;
import org.springframework.data.repository.query.Param;

public interface ElectionRepositoryCustom {

void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.keeper.homepage.domain.election.dao;

import com.keeper.homepage.domain.member.entity.Member;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.query.Param;

import static com.keeper.homepage.domain.election.entity.QElection.*;

@RequiredArgsConstructor
public class ElectionRepositoryImpl implements ElectionRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Modifying
@Override
public void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember) {
jpaQueryFactory
.update(election)
.set(election.member, virtualMember)
.where(election.member.eq(member))
.execute();
}

}
Loading