6. Spring Data JPA : 데이터 조회 페이징 (Pageable)

package com.kyhslam.persistence;

import java.util.Collection;
import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;

import com.kyhslam.domain.Board;

public interface BoardRepository extends CrudRepository<Board, Long> {

	
	// bno > ? ORDER BY bno DESC limit ?,?   
	public List<Board> findByBnoGreaterThanOrderByBnoDesc(Long bno, Pageable paging);
	
	// no order 
	public List<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.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 testBnoOrderByPaging() {
		
		Pageable paging = PageRequest.of(0,10);
		Collection<Board> results =	repo.findByBnoGreaterThanOrderByBnoDesc(0L, paging);
		results.forEach(board -> System.out.println(board.getBno()));
	}

	@Test
	public void testBnoPagingSort() {
    
    	// 0 page의 데이터 10개, 오름차순, bno 기준으로
		Pageable paging = PageRequest.of(0, 10, Sort.Direction.DESC, "bno");
		
		Collection<Board> results = repo.findByBnoGreaterThan(0L, paging);
		results.forEach(board -> System.out.println(board.toString()));
	}
	
}

 

findByBnoGreaterThan의 테스트 결과를 보면 rownum으로 데이터를 가져오는 걸 볼수 있다.

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 desc ) where rownum <= ?

Board [bno=200, title=제목..200, writer=user00, content=내용....200 채우기 , regdate=2020-06-09 14:29:07.338, updatedate=2020-06-09 14:29:07.338]
Board [bno=199, title=제목..199, writer=user09, content=내용....199 채우기 , regdate=2020-06-09 14:29:07.322, updatedate=2020-06-09 14:29:07.322]
Board [bno=198, title=제목..198, writer=user08, content=내용....198 채우기 , regdate=2020-06-09 14:29:07.303, updatedate=2020-06-09 14:29:07.303]
Board [bno=197, title=제목..197, writer=user07, content=내용....197 채우기 , regdate=2020-06-09 14:29:07.288, updatedate=2020-06-09 14:29:07.288]
Board [bno=196, title=제목..196, writer=user06, content=내용....196 채우기 , regdate=2020-06-09 14:29:07.271, updatedate=2020-06-09 14:29:07.271]
Board [bno=195, title=제목..195, writer=user05, content=내용....195 채우기 , regdate=2020-06-09 14:29:07.255, updatedate=2020-06-09 14:29:07.255]
Board [bno=194, title=제목..194, writer=user04, content=내용....194 채우기 , regdate=2020-06-09 14:29:07.24, updatedate=2020-06-09 14:29:07.24]
Board [bno=193, title=제목..193, writer=user03, content=내용....193 채우기 , regdate=2020-06-09 14:29:07.221, updatedate=2020-06-09 14:29:07.221]
Board [bno=192, title=제목..192, writer=user02, content=내용....192 채우기 , regdate=2020-06-09 14:29:07.204, updatedate=2020-06-09 14:29:07.204]
Board [bno=191, title=제목..191, writer=user01, content=내용....191 채우기 , regdate=2020-06-09 14:29:07.185, updatedate=2020-06-09 14:29:07.185]

 

댓글

Designed by JB FACTORY