티스토리 뷰

 

강의 : 인프런 스프링 핵심 원리 - 기본편

- 인텔리제이 처음 써 봄

- java11

- 프로젝트는 start.spring.io에서 생성(압축해제-> build.gradle 오픈)

 

 

 

회원 도메인 / 순수 자바로만 구현했을 때(spring x)

 

 

Grade.enum / 회원등급

package hello.core.member;

public enum Grade {
    BASIC,
    VIP
}

- enum 참고 : itmining.tistory.com/149 , www.tcpschool.com/java/java_api_enum

 

 

 

Member.java / 회원 - id, 이름, 등급

package hello.core.member;

public class Member {
    private Long id;
    private String name;
    private Grade grade;

    public Member(Long id, String name, Grade grade) {
        this.id = id;
        this.name = name;
        this.grade = grade;
    }

    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;
    }

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }
}

- alt + insert : generate(getter, setter, constrctor 등등)

 

 

 

MemberRepository.interface / 회원 저장소 인터페이스 - 저장, 조회

package hello.core.member;

public interface MemberRepository {
    /// 여기는 인터페이스, 구현체는 따로.
    void save(Member member);

    Member findById(Long memberId);
}

 

 

 

MemoryMemberRepository.java / 저장소 구현 - 저장, 조회

package hello.core.member;

import java.util.HashMap;
import java.util.Map;

public class MemoryMemberRepository implements MemberRepository {
   
    private static Map<Long, Member> store = new HashMap<>(); ///저장소를 만들어 놓음

    @Override
    public void save(Member member) {
        store.put(member.getId(), member);
    }

    @Override
    public Member findById(Long memberId) {
        return store.get(memberId);
    }
}

- 디비가 아직 확정 안됐다는 가정 하에 HashMap으로 단순한 저장소 구현

 

 

 

member.interface / 회원서비스 -가입, 조회

package hello.core.member;

public interface MemberService {
    void join(Member member);

    Member findMember(Long memberId);
}

 

 

 

MemberServiceImpl.java / 회원 서비스 구현 -가입, 조회

package hello.core.member;

public class MemberServiceImpl implements MemberService {

    private final MemberRepository memberRepository = new MemoryMemberRepository();


    @Override
    public void join(Member member) {
            memberRepository.save(member);
    }

    @Override
    public Member findMember(Long memberId) {
        return memberRepository.findById(memberId);
    }
}

 

 

 

MemberApp.java / 테스트 1

package hello.core;

import hello.core.member.Grade;
import hello.core.member.Member;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;

public class MemebrApp {

    public static void main(String[] args) {

        MemberService memberService = new MemberServiceImpl();
        ///ctrl alt v
        Member member = new Member(1L, "memberA", Grade.VIP);
        memberService.join(member);

        Member findMember = memberService.findMember(1L);
        System.out.println("member = " + member.getName());
        System.out.println("findMember : " + findMember.getName());

    }

}

-psvm : public static void main 생성

- ctrl + alt + v : 변수추출 개편함

-soutv : Systm.out.println

 

 

 

MemberServiceTest.java / 테스트2 - JUnit 사용

package hello.core.member;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class MemebrServiceTest {

    MemberService memberService = new MemberServiceImpl();
    
    @Test
    void join(){
        //given ~~~~ 한게 주어졌을때
        Member member = new Member(1L,"memberA",Grade.VIP);

        //when ~~~~~ 이렇게 됐을때
        memberService.join(member);
        Member findMember = memberService.findMember(1L);

        //then  ~~~~~ 이렇게 된다
        Assertions.assertThat(member).isEqualTo(findMember);

    }
}

- System.out.println웬만하면 쓰지 말고 Assertion으로 검증하라고 함

 

 

 

- 가입멤이랑 조회멤이랑 같을 때

 

 

 

- 가입멤이랑 조회멤이랑 다를 때

 

 

 

결론 : 인텔리제이 짱좋다

 

댓글
최근에 올라온 글
최근에 달린 댓글
링크
Total
Today
Yesterday