관리 메뉴

bright jazz music

[bootBoard] N:1(다대일) 연관관계: 11-4. JPQLQuery로 Page<Object[]> 처리 본문

Framework/Spring

[bootBoard] N:1(다대일) 연관관계: 11-4. JPQLQuery로 Page<Object[]> 처리

bright jazz music 2022. 10. 11. 09:02

이전 포스팅에서 튜플 객체를 사용하여 여러 객체를 가져오는 것을 확인하였다.

이번 포스팅에서는 원하는 파라미터(Pageable)을 전송하고, Page<Object[]>를 만들어서 반환하는 것이다.

 

1. searchBoardRepository에 searchPage()를 설계

searchPage() 추가

  • PageRequestDTO 자체를 파라미터로 처리하지 않는 이유는 DTO를 가능하면 Repository 영역에서 다루지 않기 위해서이다.
//SearchBoardRepository.java

package com.example.bootboard.repository.search;

import com.example.bootboard.entity.Board;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface SearchBoardRepository {
    Board search1();
    
    //Page<Object[]> 처리
    Page<Object[]> searchPage(String type, String Keyword, Pageable pageable);
}

 

2. searchBoardRepositoryImpl에서 searchPage()구현

우선은 다른 기능 없이 로그부터 출력해 보자

//SearchBoardRepositoryImpl.java

import static com.example.bootboard.entity.QBoard.board;

@Log4j2
public class SearchBoardRepositoryImpl extends QuerydslRepositorySupport
        implements SearchBoardRepository {

    //생성자
    public SearchBoardRepositoryImpl(){
        super(Board.class); //여기서 super는 querydslRepositorySupport이다.
    }

//...

    @Override
    public Page<Object[]> searchPage(String type, String Keyword, Pageable pageable) {
        log.info("searchPage............................");
        return null;
    }

///...

}

 

4. 테스트

테스트 작업에는 '제목(t)'으로 '1'이라는 단어가 있는 데이터를 검색해 본다.

아직 구현된 내용이 없어서 단순 로그만 출력된다.

//BoardRepositoryTests.java


@SpringBootTest
public class BoardRepositoryTests {

    @Autowired
    private BoardRepository boardRepository;

//.....

    @Test
    public void testSearchPage(){
        Pageable pageable = PageRequest.of(0, 10,
                Sort.by("bno").descending());

        Page<Object[]> result = boardRepository.searchPage("t", "1", pageable);
    }

//.....

}

 

결과

 

콘솔로그

searchPage............................가 출력된느 것을 확인할 수 있다.

2022-10-11 22:30:13.252  INFO 5860 --- [    Test worker] c.e.b.repository.BoardRepositoryTests    : Started BoardRepositoryTests in 3.924 seconds (JVM running for 5.402)
2022-10-11 22:30:13.396  INFO 5860 --- [    Test worker] c.e.b.r.s.SearchBoardRepositoryImpl      : searchPage............................
2022-10-11 22:30:13.424  INFO 5860 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-10-11 22:30:13.426  INFO 5860 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-10-11 22:30:13.432  INFO 5860 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 20s
6 actionable tasks: 5 executed, 1 up-to-date
오후 10:30:13: Task execution finished ':test --tests "com.example.bootboard.repository.BoardRepositoryTests.testSearchPage"'.

 

Comments