[JPA] 페이징 API

  • 사용자에게 데이터를 일정량씩 나눠 보여주는 기법
  • SQL 에서 페이징은 DB마다 다름..
  • 복잡성을 추상화 해 일관된 방식으로 페이징 처리를 할 수 있도록 도와주는 API 제공

JPA 페이징 API

@BeforeEach
    void setUp() {
        for (int i = 0; i < 50; i++) {
            Member member = new Member();
            member.setName("ipeac" + i);
            member.setAge(i);
            em.persist(member);
        }
    }
    
    @Test
    void testPaging() {
        TypedQuery<Member> query = em.createQuery(
            "select  m from Member m order by m.age desc", Member.class
        );
        query.setFirstResult(10); // 11번째 행 부터 시작
        query.setMaxResults(20); // 최대 20 개 행
        List<Member> resultList = query.getResultList();
        for (Member member : resultList) {
            System.out.println(member);
        }
    }
  • API 설명
    • setFirstResult(int startPosition): 조회를 시작할 행의 위치를 지정합니다. 위치는 0부터 시작합니다.
    • setMaxResults(int maxResult): 한 번에 조회할 최대 행 수를 지정합니다.
  • 수행되는 쿼리
/*
         * select m1_0.id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.age,m1_0.name 
         * from member m1_0 
				*		order by m1_0.age 
         * desc limit 10,20;
         
         *  */
  • 출력 내용
Member(id=11, name=ipeac10, age=10, address=null, favoriteFoods=[], addressHistory=[])
Member(id=12, name=ipeac11, age=11, address=null, favoriteFoods=[], addressHistory=[])
Member(id=13, name=ipeac12, age=12, address=null, favoriteFoods=[], addressHistory=[])
Member(id=14, name=ipeac13, age=13, address=null, favoriteFoods=[], addressHistory=[])
Member(id=15, name=ipeac14, age=14, address=null, favoriteFoods=[], addressHistory=[])
Member(id=16, name=ipeac15, age=15, address=null, favoriteFoods=[], addressHistory=[])
Member(id=17, name=ipeac16, age=16, address=null, favoriteFoods=[], addressHistory=[])
Member(id=18, name=ipeac17, age=17, address=null, favoriteFoods=[], addressHistory=[])
Member(id=19, name=ipeac18, age=18, address=null, favoriteFoods=[], addressHistory=[])
Member(id=20, name=ipeac19, age=19, address=null, favoriteFoods=[], addressHistory=[])
Member(id=21, name=ipeac20, age=20, address=null, favoriteFoods=[], addressHistory=[])
Member(id=22, name=ipeac21, age=21, address=null, favoriteFoods=[], addressHistory=[])
Member(id=23, name=ipeac22, age=22, address=null, favoriteFoods=[], addressHistory=[])
Member(id=24, name=ipeac23, age=23, address=null, favoriteFoods=[], addressHistory=[])
Member(id=25, name=ipeac24, age=24, address=null, favoriteFoods=[], addressHistory=[])
Member(id=26, name=ipeac25, age=25, address=null, favoriteFoods=[], addressHistory=[])
Member(id=27, name=ipeac26, age=26, address=null, favoriteFoods=[], addressHistory=[])
Member(id=28, name=ipeac27, age=27, address=null, favoriteFoods=[], addressHistory=[])
Member(id=29, name=ipeac28, age=28, address=null, favoriteFoods=[], addressHistory=[])
Member(id=30, name=ipeac29, age=29, address=null, favoriteFoods=[], addressHistory=[])


Uploaded by N2T

'자바 > JPA' 카테고리의 다른 글

[JPA] JPQL 파라미터 바인딩  (0) 2023.11.09
[JPA] 프로젝션  (0) 2023.11.09
[JPA] 집합 함수  (0) 2023.11.09
[JPA] JPQL 조인  (0) 2023.11.09
[JPA] 페치 조인  (0) 2023.11.09