diff --git "a/\352\271\200\354\247\200\354\234\244/data-jpa/src/main/java/study/datajpa/repository/MemberJpaRepository.java" "b/\352\271\200\354\247\200\354\234\244/data-jpa/src/main/java/study/datajpa/repository/MemberJpaRepository.java" index d86148ae..8c83cd29 100644 --- "a/\352\271\200\354\247\200\354\234\244/data-jpa/src/main/java/study/datajpa/repository/MemberJpaRepository.java" +++ "b/\352\271\200\354\247\200\354\234\244/data-jpa/src/main/java/study/datajpa/repository/MemberJpaRepository.java" @@ -54,4 +54,18 @@ public List findByUsername(String username) { .setParameter("username", username) .getResultList(); } + + public List findByPage(int age, int offset, int limit) { + return em.createQuery("select m from Member m where m.age = :age order by m.username desc") + .setParameter("age", age) + .setFirstResult(offset) + .setMaxResults(limit) + .getResultList(); + } + + public long totalCount(int age) { + return em.createQuery("select count(m) from Member m where m.age = :age", Long.class) + .setParameter("age", age) + .getSingleResult(); + } } diff --git "a/\352\271\200\354\247\200\354\234\244/data-jpa/src/test/java/study/datajpa/repository/MemberJpaRepositoryTest.java" "b/\352\271\200\354\247\200\354\234\244/data-jpa/src/test/java/study/datajpa/repository/MemberJpaRepositoryTest.java" index badbeafb..53f2daf5 100644 --- "a/\352\271\200\354\247\200\354\234\244/data-jpa/src/test/java/study/datajpa/repository/MemberJpaRepositoryTest.java" +++ "b/\352\271\200\354\247\200\354\234\244/data-jpa/src/test/java/study/datajpa/repository/MemberJpaRepositoryTest.java" @@ -87,4 +87,30 @@ public void testNamedQuery() { Member findMember = result.get(0); assertThat(findMember).isEqualTo(m1); } + + @Test + public void paging() { + // given + memberJpaRepository.save(new Member("member1", 10)); + memberJpaRepository.save(new Member("member2", 10)); + memberJpaRepository.save(new Member("member3", 10)); + memberJpaRepository.save(new Member("member4", 10)); + memberJpaRepository.save(new Member("member5", 10)); + + int age = 10; + int offset = 0; + int limit = 3; + + // when + List members = memberJpaRepository.findByPage(age, offset, limit); + long totalCount = memberJpaRepository.totalCount(age); + + // 페이지 계산 공식 적용 + // totalPage = totalCount / size + + // then + assertThat(members.size()).isEqualTo(3); + assertThat(totalCount).isEqualTo(5); + + } } \ No newline at end of file