<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
server:
port: 8085
servlet:
context-path: /
encoding:
charset: utf-8
enabled: true
spring:
datasource:
driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mariadb://db.rigel.kr:3306/kang?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true
username: kang
password: 1234
jpa:
open-in-view: true
hibernate:
ddl-auto: update
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
logging:
level:
org.hibernate:
SQL: debug
type.descriptor.sql: trace
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-log4j12</artifactId>-->
<!-- <version>1.6.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
------ 생략 -------
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
springBoot 최신 버전부터는 라이브러리 version을 생략해도 알맞게 갖고오나보다..
가끔 intellij 버전에 따라 run maven compile 대신 Maven-> Generate Sources and Update Folders
@Configuration
public class QueryDslConfig {
@PersistenceContext
private EntityManager entityManager;
@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
→ 이제 프로젝트 어디에서든 JPAQuery 주입받아 querydsl 사용 가능
public interface UserRepository extends JpaRepository<User, Long>{
@Query(value = "select * from User where procYn= 'N'", nativeQuery=true)
List<User> mFindByProcyYnIsN();
}
인터페이스 생성
public interface UserCustomRepository {
List<User> findProcYNIsN();
}
상속 클래스 생성
@RequiredArgsConstructor
@Repository //QueryDsl 용도
public class UserCustomRepositoryImpl implements UserCustomRepository{
private final JPAQueryFactory jpaQueryFactory;
@Override
public List<User> findProcYNIsN() {
//QUser user= new QUser("m"); //직접 만들기 싫다면 static 으로 import 하셔도 됩니다.
return jpaQueryFactory
.selectFrom(QUser.user)
.where(QUser.user.procYn.eq("N"))
.fetch();
}
}
public interface UserRepository extends JpaRepository<User, Long>, UserCustomRepository {
//queryDsl 사용하기 싫으시면 native Quey 사용하세요.
@Query(value = "select * from User where procYn= 'N'", nativeQuery=true)
List<User> mFindByProcyYnIsN();
}
@Transactional //UPDATE, DELETE 경우 @Transactional을 추가해줘야 에러가 발생 X
public void test(){
//전체는 찾지말고, procYn = n인 애들만 찾아서. y로 바꿔주자..
List<User> users = UserRepository.findProcYNIsN();
//List<User> users = UserRepository.mFindByProcyYnIsN();
}
implementation 'com.querydsl:querydsl-jpa'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
https://tecoble.techcourse.co.kr/post/2021-08-08-basic-querydsl/
https://velog.io/@woo0_hooo/Spring-Boot와-QueryDSL-연동하기
https://tecoble.techcourse.co.kr/post/2021-08-08-basic-querydsl/
https://querydsl.com/static/querydsl/3.4.1/reference/ko-KR/html/ch02.html