반응형
-- dependency
<!-- query dsl dependency -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<!-- plugin set-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<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>
</plugins>
-- created databaseConfig.java
@Configuration
public class Databaseconfig {
@Bean
public JPAQueryFactory queryFactory(EntityManager em) {
return new JPAQueryFactory(em);
}
}
-- created entity [author <1 - *> book]
// Entity Author
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table
public class Author{
@Id
private authorSeq;
private long author;
private String name;
}
// Entity Book
@Getter
@EqualsAndHashCode(of = {"bookSeq"}, callSuper = false)
@Entity
@Table
public class Book{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long bookSeq;
private String title;
private String description;
private LocalDate published;
private long rate;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "authorSeq")
private Author author;
}
- book DTO
@Getter
@NoArgsConstructor
public class YoutubeDTO {
private String name;
private String title;
private String description;
private long rate;
private LocalDateTime publiched;
@QueryProjection
@Builder
public BookDTO(String name,
String title,
String description,
long rate,
LocalDate published) {
this.name = name;
this.title = title;
this.description = description;
this.rate = rate;
this.published = published;
}
}
-- author repository
@Repository
public interface AuthorRepository extends JpaRepository<Author, Long>,
QuerydslPredicateExecutor<Author> {
}
-- book repository
public interface BookRepository extends JpaRepository<Book, Long>,
BookRepositoryCustom,QuerydslPredicateExecutor<Book> {
}
-- custom repository
public interface BookRepositoryCustom {
Page<BookDTO> findRateTop(Pageable pageable);
}
-- implementation custom repository
//평점 과 출판 일 을 기준으로 정렬 하여 페이징 처리하여 가져오도록 한다.
@RequiredArgsConstructor
public class BookRepositoryImpl implements BookRepositoryCustom{
private final JPAQueryFactory queryFactory;
@Override
public Page<BookDTO> findRateTop(Pageable pageable) {
QBook book = QBook.book;
QAuthor author = QAuthor.author;
QueryResults<BookDTO> result = queryFactory
.select(
Projections.fields(
BookDTO.class,
author.name,
book.title,
book.description,
book.rate,
book.published))
.from(book)
.leftJoin(author)
.on(author.authorSeq.eq(book.author.authorSeq))
.groupBy(book.author.authorSeq)
.orderBy(book.rate.asc(),book.published.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetchResults();
return result.isEmpty() ? Page.empty() :
new PageImpl<>(result.getResults(),pageable,result.getTotal());
}
}
- Predicate 구현
public class BookPredicate {
public static Predicate byTitle(String title) {
QBook book = QBook.book;
BooleanBuilder builder = new BooleanBuilder();
return builder.and(book.title.likeIgnoreCase(title));
}
}
- Predicate 사용
@Slf4j
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
@Override
public Optional<Book> findByTitle(String title) {
return bookRepository.findOne(BookPredicate.byTitle(title));
}
}
반응형
'SpringBoot' 카테고리의 다른 글
spring controller async request process with executor (0) | 2019.10.05 |
---|---|
Java 8 Stream range (0) | 2019.08.24 |
Spring Batch (정리) (0) | 2019.04.06 |
json array convert to object list in java (0) | 2019.01.30 |
Object List groupping and sorted by fields (0) | 2019.01.09 |