7. Spring Data JPA : 데이터 조회 Page<T>
- 📚 Spring/Spring JPA 실습
- 2020. 6. 15. 14:42
Page<T>는 단순 데이터만의 추출하는 용도가 아니라, 웹에서 필요한 데이터들을 추가적으로 처리해 준다.
package com.kyhslam.persistence;
import java.util.Collection;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import com.kyhslam.domain.Board;
public interface BoardRepository extends CrudRepository<Board, Long> {
public Page<Board> findByBnoGreaterThan(Long bno, Pageable paging);
}
테스트 코드
package com.kyhslam;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import com.kyhslam.domain.Board;
import com.kyhslam.persistence.BoardRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
public class boot3ApplicationTest {
@Autowired
private BoardRepository repo;
@Test
public void testBnoPagingSort() {
Pageable paging = PageRequest.of(0, 10, Sort.Direction.ASC, "bno");
Page<Board> result = repo.findByBnoGreaterThan(0L, paging);
System.out.println("PAGE SIZE : " + result.getSize());
System.out.println("TOTAL PAGES : " + result.getTotalPages());
System.out.println("TOTAL COUNT : " + result.getTotalElements());
System.out.println("NEXT : " + result.nextPageable());
List<Board> list = result.getContent();
list.forEach(board -> System.out.println(board.toString()));
//Collection<Board> results = repo.findByBnoGreaterThan(0L, paging);
//results.forEach(board -> System.out.println(board.toString()));
}
}
결과
Hibernate: select * from ( select board0_.bno as bno1_0_, board0_.content as content2_0_,
board0_.regdate as regdate3_0_, board0_.title as title4_0_, board0_.updatedate as
updatedate5_0_, board0_.writer as writer6_0_ from tbl_boards board0_ where board0_.bno>?
order by board0_.bno asc ) where rownum <= ?
Hibernate: select count(board0_.bno) as col_0_0_ from tbl_boards board0_ where board0_.bno>?
PAGE SIZE : 10
TOTAL PAGES : 20
TOTAL COUNT : 200
NEXT : Page request [number: 1, size 10, sort: bno: ASC]
Board [bno=1, title=제목..1, writer=user01, content=내용....1 채우기 , regdate=2020-06-09 14:29:03.466, updatedate=2020-06-09 14:29:03.466]
Board [bno=2, title=제목..2, writer=user02, content=내용....2 채우기 , regdate=2020-06-09 14:29:03.524, updatedate=2020-06-09 14:29:03.524]
Board [bno=3, title=제목..3, writer=user03, content=내용....3 채우기 , regdate=2020-06-09 14:29:03.551, updatedate=2020-06-09 14:29:03.551]
Board [bno=4, title=제목..4, writer=user04, content=내용....4 채우기 , regdate=2020-06-09 14:29:03.571, updatedate=2020-06-09 14:29:03.571]
Board [bno=5, title=제목..5, writer=user05, content=내용....5 채우기 , regdate=2020-06-09 14:29:03.59, updatedate=2020-06-09 14:29:03.59]
Board [bno=6, title=제목..6, writer=user06, content=내용....6 채우기 , regdate=2020-06-09 14:29:03.616, updatedate=2020-06-09 14:29:03.616]
Board [bno=7, title=제목..7, writer=user07, content=내용....7 채우기 , regdate=2020-06-09 14:29:03.671, updatedate=2020-06-09 14:29:03.671]
Board [bno=8, title=제목..8, writer=user08, content=내용....8 채우기 , regdate=2020-06-09 14:29:03.694, updatedate=2020-06-09 14:29:03.694]
Board [bno=9, title=제목..9, writer=user09, content=내용....9 채우기 , regdate=2020-06-09 14:29:03.712, updatedate=2020-06-09 14:29:03.712]
Board [bno=10, title=제목..10, writer=user00, content=내용....10 채우기 , regdate=2020-06-09 14:29:03.73, updatedate=2020-06-09 14:29:03.73]
결과를 보면 SQL문이 두 번 실행된다는 점이다.(만일 데이터 수가 적어서 지정된 사이즈 이하인 경우에는 한 번만 실행 된다.)
'📚 Spring > Spring JPA 실습' 카테고리의 다른 글
엔티티 설계시 주의점 (0) | 2020.08.16 |
---|---|
[Spring JPA 실습 #8] Spring JPA @Query (0) | 2020.06.16 |
6. Spring Data JPA : 데이터 조회 페이징 (Pageable) (0) | 2020.06.15 |
5. Spring Data JPA : 데이터 조회 (쿼리 메소드) (0) | 2020.06.09 |
4. Spring DATA JPA 데이터 수정 및 삭제 (0) | 2020.06.09 |