Skip to content

Commit

Permalink
[Week9][Chap4] Feat : 스프링 데이터 jpa 페이징 정렬 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
dionisos198 committed Nov 20, 2023
1 parent f38fe6c commit 56126c5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package study.datajpa.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand Down Expand Up @@ -33,9 +37,20 @@ public interface MemberRepository extends JpaRepository<Member,Long> {
Optional<Member> findOptionalByUsername(String username);


Page<Member> findPageByUsername(String name, Pageable pageable);

Slice<Member> findSliceByUsername(String name,Pageable pageable);

List<Member> findListByUsername(String name,Pageable pageable);

List<Member> findByUsername(String name, Sort sort);

Page<Member> findByAge(int age,Pageable pageable);

Slice<Member> findSliceByAge(int age,Pageable pageable);

@Query(value = "select m from Member m left join m.team t",countQuery = "select count(m) from Member m")
Page<Member> findJoinByAge(int age,Pageable pageable);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import org.junit.jupiter.api.Test;
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.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import study.datajpa.dto.MemberDto;
import study.datajpa.entity.Member;
import study.datajpa.entity.Team;

import javax.swing.plaf.metal.MetalMenuBarUI;
import java.lang.management.MemoryManagerMXBean;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -155,5 +160,76 @@ public void returnType(){

}

@Test
public void page() throws Exception{
memberRepository.save(new Member("member1",10));
memberRepository.save(new Member("member2",10));
memberRepository.save(new Member("member3",10));
memberRepository.save(new Member("member4",10));
memberRepository.save(new Member("member5",10));

PageRequest pageRequest = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"username"));
Page<Member> page = memberRepository.findByAge(10,pageRequest);



List<Member> content=page.getContent();
Assertions.assertThat(content.size()).isEqualTo(3);
Assertions.assertThat(page.getTotalElements()).isEqualTo(5);
Assertions.assertThat(page.getNumber()).isEqualTo(0);
Assertions.assertThat(page.getTotalPages()).isEqualTo(2);
Assertions.assertThat(page.isFirst()).isTrue();
Assertions.assertThat(page.hasNext()).isTrue();

}

@Test
public void pageJoin() throws Exception{
memberRepository.save(new Member("member1",10));
memberRepository.save(new Member("member2",10));
memberRepository.save(new Member("member3",10));
memberRepository.save(new Member("member4",10));
memberRepository.save(new Member("member5",10));

PageRequest pageRequest = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"username"));
Page<Member> page = memberRepository.findJoinByAge(10,pageRequest);



List<Member> content=page.getContent();
Assertions.assertThat(content.size()).isEqualTo(3);
Assertions.assertThat(page.getTotalElements()).isEqualTo(5);
Assertions.assertThat(page.getNumber()).isEqualTo(0);
Assertions.assertThat(page.getTotalPages()).isEqualTo(2);
Assertions.assertThat(page.isFirst()).isTrue();
Assertions.assertThat(page.hasNext()).isTrue();

}

@Test
public void slice() throws Exception{
memberRepository.save(new Member("member1",10));
memberRepository.save(new Member("member2",10));
memberRepository.save(new Member("member3",10));
memberRepository.save(new Member("member4",10));
memberRepository.save(new Member("member5",10));

PageRequest pageRequest = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"username"));
Slice<Member> page = memberRepository.findSliceByAge(10,pageRequest);



List<Member> content=page.getContent();
Assertions.assertThat(content.size()).isEqualTo(3);
//Assertions.assertThat(page.getTotalElements()).isEqualTo(5);
Assertions.assertThat(page.getNumber()).isEqualTo(0);
//Assertions.assertThat(page.getTotalPages()).isEqualTo(2);
Assertions.assertThat(page.isFirst()).isTrue();
Assertions.assertThat(page.hasNext()).isTrue();

}




}

0 comments on commit 56126c5

Please sign in to comment.