plugins {
//querydsl
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
dependencies {
//querydsl
implementation 'com.querydsl:querydsl-jpa'
}
//-----------querydsl-----------//
def querydslDir = "$buildDir/generated/querydsl"
querydsl {
jpa = true
querydslSourcesDir = querydslDir
}
sourceSets {
main.java.srcDir querydslDir
}
configurations {
querydsl.extendsFrom compileClasspath
}
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
//--------------------------------//
Spring Data Jpa Custom Repository 적용
@Getter
@Entity
@NoArgsConstructor
public class BookReview {
@Id
@GeneratedValue
private long reviewNo;
private int rating;
private String reviewContents;
private LocalDateTime createDate;
private int declaration;
//NORMAl, BLIND
@Enumerated(EnumType.STRING)
private ReviewStatus reviewStatus;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_no")
private UserInfo userInfo;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "isbn" )
private Book book;
@Builder
public BookReview(int rating, String reviewContents, LocalDateTime createDate, int declaration, Book book, UserInfo userInfo) {
this.rating = rating;
this.reviewContents = reviewContents;
this.createDate = createDate;
this.declaration = declaration;
this.book = book;
this.userInfo = userInfo;
}
public void changeBookReview(int rating, String reviewContents) {
this.rating = rating;
this.reviewContents = reviewContents;
}
}
@Repository
public interface BookReviewRepository extends CrudRepository<BookReview, String>, BookReviewRepositoryCustom {
boolean existsByReviewNo(long reviewNo); //querydsl
}
public interface BookReviewRepositoryCustom {
boolean existsByReviewNo(long reviewNo);
}
public class BookReviewRepositoryImpl implements BookReviewRepositoryCustom{
private final JPAQueryFactory queryFactory;
public BookReviewRepositoryImpl(JPAQueryFactory queryFactory) {
this.queryFactory = queryFactory;
}
@Override
public boolean existsByReviewNo(long reviewNo) {
Integer fetchOne = queryFactory
.selectOne()
.from(bookReview)
.where(bookReview.reviewNo.eq(reviewNo))
.fetchFirst();
return fetchOne != null;
}
}