profiles 를 활용하여 설정 값 변경하기.



스프링 개발을 하다보면 환경, 앱을 돌리고자 하는 목적에 따라 설정 값을 환경에 맞춰 변경해줘야 하는 경우가 생깁니다.

이럴 때 일일이 바꿀 필요 없이 편하게 설정 값을 변경하는 방법이 있습니다.


profiles 를 활용하는 방법입니다.





우선 환경에 따라 다른 설정 값을 가진 설정 파일을 여러개 만듭니다.

(저는 yml 을 사용했지만 properties를 사용하셔도 됩니다. )





그 후 위와 같이 Edit Configurations... 를 클릭합니다.





그 후 위 사진처럼 VM options 입력란-Dspring.profiles.active=적용하고자하는 환경 을 입력하고 앱을 run 시키면 설정한 환경의 설정 값을 가지고 있는 설정파일을 바탕으로 돌고 있는 것을 확인할 수 있습니다.





또는 터미널에서 실행시키고 싶다면 shell script 혹은 직접 위와같이 직접 쳐서 환경 설정 파일을 바꾸어 실행할 수 있습니다.




'Programming > Spring' 카테고리의 다른 글

Profile 에 따른 Logback 분리  (0) 2020.03.04



QueryDsl like 와 contains 의 차이



QueryDsl 을 사용하다가 like 기능이 필요한 일이 있었습니다. 



query.where(qTest.testName.like(searchOptions.getTestName()));



위와 같이 코드를 사용하고 테스트를 하니 원하는 결과가 나오지 않았습니다.

그래서 검색과 코드를 들어가서 살펴본 결과 이유를 알 수 있었습니다.


위의 like 는 완전히 일치하는 경우를 찾을 때 사용합니다.



query.where(qTest.testName.contains(searchOptions.getTestName()));



위의 contains 는 Mysql 에서의 like 처럼 일부를 통해 검색할 때 사용합니다.




'Database > QueryDsl' 카테고리의 다른 글

QueryDsl 사용하기.  (0) 2018.08.13



QueryDsl 사용하기.



이번에 QueryDsl 을 사용할 기회와 필요성이 있어 적용하고 사용해봤습니다.

그 과정이 쉽지는 않다는 것을 느꼈기에 정리하고자 합니다.


우선 QueryDsl 을 사용하기 위해 dependency 를 추가할때 어떤 글은 com.querydsl 이고 어떤 글은 com.mysema.querydsl 으로 나와있습니다.

이유는 QueryDsl 이 4.x 버전으로 올라가며 package 명이 바뀌어서 그 이전글은 그대로 남아 있는 것으로 생각됩니다.


처음으로 build.gradle 파일에 아래와 같이 내용을 써줍니다.



dependencies {
compile("mysql:mysql-connector-java")
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile ("com.querydsl:querydsl-core")
compile ("com.querydsl:querydsl-apt")
compile ("com.querydsl:querydsl-jpa")
testCompile group: 'junit', name: 'junit', version: '4.12'
}

sourceSets {
main {
java {
srcDirs 'src/main/java', 'src/main/generated'
}
}
}

task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
file(new File(projectDir, "/src/main/generated")).deleteDir()
file(new File(projectDir, "/src/main/generated")).mkdirs()
source = sourceSets.main.java
classpath = configurations.compile + configurations.compileOnly
options.compilerArgs = [
"-proc:only",
"-processor", "com.querydsl.apt.jpa.JPAAnnotationProcessor",
"-processor", 'com.querydsl.apt.jpa.JPAAnnotationProcessor,lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
]
destinationDir = file('src/main/generated')
}

compileJava {
dependsOn generateQueryDSL
}

clean.doLast {
file(new File(projectDir, "/src/main/generated")).deleteDir()
}



위에서 clean.doLast 라는 부분이 의아하실 것 같은데 컴파일할 때 이전에 생성되었던 Q Class 가 남아있다면 에러를 발생시키기 때문에 이를 방지하기 위해 clean.doLast 에서 지우고 있습니다.





위 사진에 보이는 Tasks - application - bootRun 을 실행하면 





위와 같이 지정한 위치에 안에는 Q Class 들이 들어가 있는 상태의 generated 폴더가 생성됩니다.

이젠 CustomRepository 와 CustomRepositoryImpl 을 만들어야 합니다.



public interface CustomTestRepository {
Page<Test> findTestBySearchFilter(Long testId, SearchOptions searchOptions, Pageable pageable);

}



@Component
public class TestRepositoryImpl extends QueryDslRepositorySupport implements CustomTestRepository {

public TestRepositoryImpl() {
super(Test.class);
}

@Override
public Page<Test> findTestBySearchFilter(Long testId, SearchOptions searchOptions, Pageable pageable) {
QTest qTest = QTest.test;
JPQLQuery query = from(qTest);

if(testId != null) {
query.where(qTest.testId.eq(testId));
}

do something...

final List<Test> tests = getQuerydsl().applyPagination(pageable, query).fetch();
final long totalCount = query.fetchCount();

return new PageImpl<>(tests, pageable, totalCount);
}



위의 코드는 예시로 필요에 따라 추가, 수정하여 사용하면 됩니다.





'Database > QueryDsl' 카테고리의 다른 글

QueryDsl like 와 contains 의 차이  (0) 2018.08.13

+ Recent posts