# 2. 회원 도메인과 리포지토리 만들기

1. 회원 도메인 생성\
   main>java폴더>hello.heellospring패키지>**domain패키지 생성>Member 클래스 생성, 정의**

```java
package hello.hellospring.domain;

public class Member {
    private Long id;//시스템이 저장하는 ID.
    private String name;//이름

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

```

2\. 리포지토리 만들기\
main>java폴더>hello.hellospring패키지>domain패키지\
&#x20;                                                                    repository패키지>MemberRepository 인터페이스 생성\
&#x20;                                                                                                MemoryMemberRepository 클래스 생성\
MemberRepository 인터페이스 : 4가지 기능 정의

```java
package hello.hellospring.repository;

import hello.hellospring.domain.Member;

import java.util.List;
import java.util.Optional;

public interface MemberRepository {//4가지 기능 구현:1.저장 2. id로 찾기 3.name으로 찾기 4.저장된 모든 회원 리스트 반환
    Member save(Member member);//
    Optional<Member> findById(Long id);//Java8 : Optional이란 객체가 Null일 수도 있는데 null을 반환하는 방법 중에 Optional로 감싸서 반환하는 방법.
    Optional<Member> findByName(String name);
    List<Member> findAll();
}

```

MemoryMemberRepository 클래스 : 4가지 기능 구현\
clearStore() 메서드는 테스트코드에서 메서드가 한번 실행될 때마다 store를 비워준다!\
Optional은 null을 가질 수도 있는 객체를 받을 때 이 Optional이라는 것으로 한번 감싼다. null 객체를 받는(처리하는) 방법들 중 하나이다.

```java
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<>();//(참고 : 현업=>공유되는변수일 경우 concurrenthashmap을 써야함)
    private static long sequence = 0L;//sequence는 0/1/2 키값 생성하주는 애. 이또한 마찬가지로 현업에서는 동시성 문제로 autumnLong으로 해줘야함.
    @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) {//람다를 써서 루프 돈다.filter()사용.
        //member.getName()이 파라미터로 넘어온 name과 같은지 확인.
        //findAny():하나라도 찾는 것.
        //결과가 Optional로 반환.
        return store.values().stream()
                .filter(member -> member.getName().equals(name))
                .findAny();
    }
    @Override
    public List<Member> findAll() {//루프돌리기도 편해서 실무에서 리스트 많이 쓴다.
        return new ArrayList<>(store.values());
    }
    public void clearStore(){
        store.clear();
    }
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/spring/undefined-1/2..md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
