Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Servlet
- jpa
- pointcut
- Proxy
- Exception
- 스프링
- 스프링 핵심 기능
- QueryDSL
- java
- 백준
- springdatajpa
- db
- SpringBoot
- AOP
- Thymeleaf
- 자바
- 그리디
- kotlin
- JPQL
- 알고리즘
- 인프런
- Spring Boot
- transaction
- http
- spring
- JDBC
- 스프링 핵심 원리
- Android
- 김영한
- Greedy
Archives
- Today
- Total
개발자되기 프로젝트
[Servlet] 회원 관리 웹 애플리케이션 요구사항 본문
1. 회원 정보
- 이름: username
- 나이: age
2. 기능 요구사항
- 회원 저장
- 회원 목록 조회
3. Member
@Getter
@Setter
public class Member {
private Long id;
private String username;
private int age;
public Member() {
}
public Member(String username, int age) {
this.username = username;
this.age = age;
}
}
4. MemberRepository
/**
* 실무에서는 동시성 문제 고려해야함.
* ConcurrentHashMap, AtomicLong 사용 고려
*/
public class MemberRepository {
private static Map<Long, Member> store = new HashMap<>();
private static long sequence = 0L;
private static final MemberRepository instance = new MemberRepository();
public static MemberRepository getInstance(){
return instance;
}
//생성자 틀어막기 ㅋㅋ
private MemberRepository() {
}
public Member save(Member member){
member.setId(++sequence);
store.put(member.getId(), member);
return member;
}
public Member findById(Long id){
return store.get(id);
}
public List<Member> findAll(){
return new ArrayList<>(store.values());
}
public void clearStore(){
store.clear();
}
}
5. Test
class MemberRepositoryTest {
MemberRepository memberRepository = MemberRepository.getInstance();
@AfterEach
void afterEach(){
memberRepository.clearStore();
}
@Test
void save(){
//given
Member member = new Member("hello", 20);
//when
Member savedMember = memberRepository.save(member);
//then
Member findMember = memberRepository.findById(member.getId());
assertThat(findMember).isEqualTo(member);
}
@Test
void findAll(){
//given
Member member1 = new Member("member1", 20);
Member member2 = new Member("member2", 30);
memberRepository.save(member1);
memberRepository.save(member2);
//when
List<Member> result = memberRepository.findAll();
//then
assertThat(result.size()).isEqualTo(2);
assertThat(result).contains(member1, member2);
}
}
6. GitHub : 210909 Requirement
'인프런 > [인프런] 스프링 MVC 1' 카테고리의 다른 글
[JSP] 회원 관리 웹 애플리케이션 (0) | 2021.09.09 |
---|---|
[Servlet] 회원 관리 웹 애플리케이션 (0) | 2021.09.09 |
HTTP 응답 데이터 - API JSON (0) | 2021.09.08 |
HTTP 응답 데이터 - 단순 텍스트, HTML (0) | 2021.09.08 |
HttpServletResponse - 기본 사용법 (0) | 2021.09.08 |
Comments