데이터 : 회원 ID, 이름
기능 : 회원등록, 조회
아직 데이터 저장소가 선정되지 않았다고 가정하기
스프링의 웹 애플리케이션 계층구조
클래스 의존관계
package hello.hellospring.repository;
import hello.hellospring.domain.Member;
import java.util.*;
public class MemoryMemberRepository implements MemberRepository{ // 멤버레포지에 있는 함수들 다 가져오기 (구현용)
private static Map<Long,Member>store= new HashMap<>();
private static longsequence= 0L; // sequence는 0,1의 키값 생성
@Override
public Member save(Member member) {
member.setId(++sequence);
store.put(member.getId(),member);
return member;
}
@Override
public Optional<Member> findById(Long id) {
return Optional.ofNullable(store.get(id)); // 널 일수도 있으니옵셔널로 감싸서 반환>> 그럼 클라이언트가 조작가능
}
@Override
public Optional<Member> findByName(String name) {
returnstore.values().stream()
.filter(member -> member.getName().equals(name))
.findAny();// 멤버중에 이름이 name과 같은지 확인후 있으면 반환
}
@Override
public List<Member> findAll() {
return new ArrayList<>(store.values());
}
}
optional = 찾아온것을 감싸서 반환 (정해놓은 옵션에따라)
List = 배열로 반환
package hello.hellospring.repository;
import hello.hellospring.domain.Member;
import java.util.List;
import java.util.Optional;
public interface MemberRepository {
Member save(Member member);
Optional<Member> findById(Long id);
Optional<Member> findByName(String name);
List<Member> findAll();
}
//선언부분이다. MemberRepository
-----------------------------------------------------------------------------
package hello.hellospring.repository;
import hello.hellospring.domain.Member;
import java.util.*;
public class MemoryMemberRepository implements MemberRepository{ // 멤버레포지에 있는 함수들 다 가져오기 (구현용)
private static Map<Long,Member> store = new HashMap<>();
private static long sequence = 0L; // sequence는 0,1의 키값 생성
@Override
public Member save(Member member) {
member.setId(++sequence);
store.put(member.getId(),member);
return member;
}
@Override
public Optional<Member> findById(Long id) {
return Optional.ofNullable(store.get(id)); // 널 일수도 있으니옵셔널로 감싸서 반환>> 그럼 클라이언트가 조작가능
}
@Override
public Optional<Member> findByName(String name) {
return store.values().stream()
.filter(member -> member.getName().equals(name))
.findAny();// 멤버중에 이름이 name과 같은지 확인후 있으면 반환
}
@Override
public List<Member> findAll() {
return new ArrayList<>(store.values());
}
}
package hello.hellospring.repository;
import hello.hellospring.domain.Member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.util.List;
public class MemoryMemberRespositoryTest {
MemberRepository repository = new MemoryMemberRepository();
@AfterEach // 테스트들을 실행할떄마다
public void afterEach(){ // 저장소를 지운다.
repository.clearStore();
}
@Test
public void save(){
Member member = new Member();
member.setName("seul");
repository.save(member);
Member result = repository.findById(member.getId()).get(); // get은 옵션을 하나 까서 꺼내준다
Assertions.assertThat(member).isEqualTo(result);
}
@Test
public void findByName(){
Member member1 = new Member();
member1.setName("seul1");
repository.save(member1);
Member member2 = new Member();
member2.setName("seul2");
repository.save(member2);
Member result = repository.findByName("seul1").get();
Assertions.assertThat(result).isEqualTo(member1);
}
@Test
public void findAll(){
Member member1 = new Member();
member1.setName("seul1");
repository.save(member1);
Member member2 = new Member();
member2.setName("seul2");
repository.save(member2);
List<Member> result = repository.findAll();
Assertions.assertThat(result.size()).isEqualTo(2);
}
}
----------------------------------------------------------------------------
//MemoryMemberRepository 자바클래스 안에
//public void clearStore() {
// store.clear();
// }
//이것 작성해주기