Skip to content

Commit

Permalink
[Week9][Chap4] Feat : 메소드 이름으로 쿼리 생성 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
dionisos198 committed Nov 20, 2023
1 parent e030e31 commit 733dc69
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public Member find(Long id){
return em.find(Member.class,id);
}

public List<Member> findByUsernameAndAgeGreaterThan(String username, int age){
return em.createQuery("select m from Member m where m.username = :username and m.age > :age")
.setParameter("username",username)
.setParameter("age",age)
.getResultList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import study.datajpa.entity.Member;

import java.util.List;

public interface MemberRepository extends JpaRepository<Member,Long> {
public List<Member> findByUsernameAndAgeGreaterThan(String username,int age);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,29 @@ public void basicCRUD() {

long count = memberJpaRepository.count();
Assertions.assertThat(count).isEqualTo(2);

memberJpaRepository.delete(member1);
memberJpaRepository.delete(member2);
long deletedCount = memberJpaRepository.count();
Assertions.assertThat(deletedCount).isEqualTo(0);
}


@Test
public void findByUsernameAndAgeGreaterThan(){
Member m1= new Member("AAA",10);
Member m2 = new Member("AAA",20);
memberJpaRepository.save(m1);
memberJpaRepository.save(m2);

List<Member> result=
memberJpaRepository.findByUsernameAndAgeGreaterThan("AAA",15);
Assertions.assertThat(result.get(0).getUsername()).isEqualTo("AAA");
Assertions.assertThat(result.get(0).getAge()).isEqualTo(20);
Assertions.assertThat(result.size()).isEqualTo(1);


}


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,20 @@ public void basicCRUD() {
}


@Test
public void findByUsernameAndAgeGreaterThan(){
Member m1= new Member("AAA",10);
Member m2 = new Member("AAA",20);
memberRepository.save(m1);
memberRepository.save(m2);

List<Member> result= memberRepository.findByUsernameAndAgeGreaterThan("AAA",15);
Assertions.assertThat(result.get(0).getUsername()).isEqualTo("AAA");
Assertions.assertThat(result.get(0).getAge()).isEqualTo(20);
Assertions.assertThat(result.size()).isEqualTo(1);


}


}

0 comments on commit 733dc69

Please sign in to comment.