π 2024λ 01μ 15μΌ
Directory
Spring_basic/src/main/java/com/encore/Spring_basic/
- domain
- Member
- MemberRequestDTO
- MemberResponseDTO
- controller
- MemberController
- service
- MemberService
- repository
- MemberRepository
- MemoryMemberRepository
Member (Entity)
Entity μμ Setterλ₯Ό μ μ¬μ©νμ§ μλ μ΄μ
- λΆλ³μ±: Setterλ₯Ό μ¬μ©νμ§ μμΌλ©΄ κ°μ²΄κ° μμ±λ νμ μνλ₯Ό λ³κ²½ν μ μμ΄μ μμ μ±μ΄ λμμ§λ€.
- μ€λ λ μμ μ±: λΆλ³ κ°μ²΄λ μ¬λ¬ μ€λ λμμ μμ νκ² μ¬μ©ν μ μλ€. λκΈ°νμ λν κ±±μ μ΄ μ€μ΄λ€κ³ μ€λ λ κ° κ²½μ 쑰건μ λ°©μ§νλ€.
- ν μ€νΈ μ©μ΄μ±: λΆλ³ κ°μ²΄λ ν μ€νΈνκΈ° μ½λ€. κ°μ²΄μ μνκ° λ³κ²½λμ§ μκΈ° λλ¬Έμ νΉμ μνμ λν ν μ€νΈκ° κ°λ¨ν΄μ§λ€.
- λΆμ ν¨κ³Ό λ°©μ§: Setterλ₯Ό μ¬μ©ν μν λ³κ²½μ λΆμ ν¨κ³Όλ₯Ό μ΄λν μ μλ€.
import lombok.*; import java.time.LocalDateTime; /* build.gradle μμ dependencies μμ‘΄μ± μ£Όμ μμ§ λ§ κ²! lombok λΌμ΄λΈλ¬λ¦¬ μ€μ ν @Getter : getter μλ μμ± μ΄λ Έν μ΄μ @AllArgsConstructor : κ°μ²΄μ λͺ¨λ νλλ₯Ό 맀κ°λ³μλ‘ λ°λ μμ±μλ₯Ό λ§λ€μ΄μ£Όλ μ΄λ Έν μ΄μ */ @Getter @AllArgsConstructor public class Member { private long id; private String name; private String email; private String password; private LocalDateTime create_at; }
MemberRequestDTO (DTO)
@Data // public class MemberRequestDTO { private String name; private String email; private String password; }
MemberResponseDTO (DTO)
@Data public class MemberResponseDTO { private long id; private String name; private String email; private String password; private LocalDateTime create_at; }
@Data : 1. toString(): κ°μ²΄μ λ¬Έμμ΄ ννμ μμ±. 2. equals(): κ°μ²΄μ λλ±μ± λΉκ΅λ₯Ό μν λ©μλλ₯Ό μμ±. 3. hashCode(): κ°μ²΄μ ν΄μ μ½λλ₯Ό μμ±. 4. Getter: λͺ¨λ νλμ λν Getter λ©μλλ₯Ό μμ±. 5. Setter: λͺ¨λ non-final νλμ λν Setter λ©μλλ₯Ό μμ±. λ€μ μλ μμ±ν΄μ€λ€. DTOμ μ¬μ© μ΄μ : 1. λ°μ΄ν° μ μ‘: μλ‘ λ€λ₯Έ κ³μΈ΅ κ°μ λ°μ΄ν°λ₯Ό ν¨κ³Όμ μΌλ‘ μ λ¬νκΈ° μν κ°μ²΄. 2. λ°μ΄ν° νμ λ³ν: μλ‘ λ€λ₯Έ μμ€ν μ΄λ κ³μΈ΅ κ°μ λ°μ΄ν° νμμ΄ λ€λ₯Ό λ, νμν λ°μ΄ν°λ§μ μ μ‘νκ³ νμμ λ³νν μ μμ. 3. 보μ λ° νλΌμ΄λ²μ: μν°ν° κ°μ²΄μ λ―Όκ°ν μ 보λ₯Ό μ§μ λ ΈμΆνμ§ μκ³ , νμν μ 보λ§μ κ°μ§ κ°μ²΄λ₯Ό μ μ‘νμ¬ λ³΄μκ³Ό νλΌμ΄λ²μλ₯Ό κ°νν¨. 4. μμ‘΄μ± κ°μ: μν°ν° ꡬ쑰 λ³κ²½ μ ν΄λΌμ΄μΈνΈ μ½λμ λ―ΈμΉλ μν₯μ μ΅μνν¨. 5. μ±λ₯ μ΅μ ν: νμν λ°μ΄ν°λ§μ μ μ‘νμ¬ λ€νΈμν¬ λΉμ©κ³Ό μ μ‘ μκ°μ μ΅μ νν¨. 6. ν΄λΌμ΄μΈνΈ νΉν λ°μ΄ν° λͺ¨λΈλ§: ν΄λΌμ΄μΈνΈκ° νμλ‘ νλ λ°μ΄ν° μ€μ¬μΌλ‘ λͺ¨λΈλ§νμ¬ μ°¨μ΄λ₯Ό 극볡ν¨. 7. ν μ€νΈ μ©μ΄μ±: νμν λ°μ΄ν°λ§μ ν¬ν¨νλ DTOλ₯Ό μ¬μ©νμ¬ ν μ€νΈλ₯Ό κ°νΈνκ² μνν¨.
MemberController (Controller)
/* μλΉμ€ μ΄λ Έν μ΄μ μ ν΅ν΄ μ±κΈν€ μ»΄ν¬λνΈλ‘ μμ± -> μ€νλ§ λΉμΌλ‘ λ±λ‘ μ€νλ§ λΉμ΄λ μ€νλ§μ΄ μμ±νκ³ κ΄λ¦¬νλ κ°μ²΄λ₯Ό μλ―Έ μ μ΄μ μμ (Inversion of Control) -> IOC컨ν μ΄λκ° μ€νλ§λΉμ κ΄λ¦¬(λ²λ€ μμ±, μμ‘΄μ± μ£Όμ ) */ @Controller public class MemberController { /* // μμ‘΄μ± μ£Όμ(DI) λ°©λ² 1 : νλ μ£Όμ λ°©μ @Autowired private MemberService memberService; */ /* // μμ‘΄μ± μ£Όμ(DI) λ°©λ² 2 : μμ±μ μ£Όμ λ°©μ (λ§μ΄ μ¬μ©) finalμ ν΅ν΄ μμλ‘ μ¬μ©κ°λ₯, λ€νμ± κ΅¬ν κ°λ₯, μνμ°Έμ‘°λ°©μ§ */ private final MemberService memberService; @Autowired public MemberController(MemberService memberService){ this.memberService = memberService; } /* // μμ‘΄μ± μ£Όμ(DI) λ°©λ² 3 : @RequiredArgsConstructor @NonNull μ΄λ Έν μ΄μ μ΄ λΆμ΄μλ νλ λλ μ΅κ°νκ° λμ΄ μμ§ μμ final νλ λμμΌλ‘ μμ±μ μμ±. private final MemberService memberService; public MemberController(MemberService memberService){ this.memberService = memberService; } */ @GetMapping("/") public String home(){ return "member/header"; } @GetMapping("/members") public String getMembers(Model model) { model.addAttribute( "memberList", memberService.members()); return "member/member-list"; } @GetMapping("/member/create") public String getMemberRegister(){ return "member/member-create"; } @PostMapping("/member/create") public String MemberAdd(MemberRequestDTO memberRequestDTO, Model model){ memberService.memberCreate(memberRequestDTO); model.addAttribute( "memberList", memberService.members()); return "redirect:/members"; } @GetMapping("/member/find") public String getMemberDetail(@RequestParam long id, Model model){ model.addAttribute( "member", memberService.memberFindById(id)); return "member/member-detail"; } }
MemberService (Service)
@Service public class MemberService { private long total_id = 1; private final MemberRepository memberRepository; @Autowired public MemberService(MemoryMemberRepository memoryMemberRepository){ this.memberRepository = memoryMemberRepository; } public List<MemberResponseDTO> members (){ List<Member> memberList = memberRepository.members(); List<MemberResponseDTO> dtolist = new ArrayList<>(); MemberResponseDTO dto; for(Member m :memberList){ dto = new MemberResponseDTO(); dto.setId(m.getId()); dto.setName(m.getName()); dto.setEmail(m.getEmail()); dto.setPassword(m.getPassword()); dtolist.add(dto); } return dtolist; } public void memberCreate(MemberRequestDTO memberRequestDTO){ Member member = new Member( total_id++, memberRequestDTO.getName(), memberRequestDTO.getEmail(), memberRequestDTO.getPassword(), LocalDateTime.now() ); memberRepository.memberCreate(member); } public MemberResponseDTO memberFindById(long id) { Member m = memberRepository.memberFindById(id); MemberResponseDTO dto = new MemberResponseDTO(); dto.setId(m.getId()); dto.setName(m.getName()); dto.setEmail(m.getEmail()); dto.setPassword(m.getPassword()); dto.setCreate_at(m.getCreate_at()); return dto; } }
MemberRepository (Repository)
public interface MemberRepository { public List<Member> members(); public void memberCreate(Member member); public Member memberFindById(long id); }
MemoryMemberRepository (Repository)
@Repository public class MemoryMemberRepository implements MemberRepository{ private List<Member> memberDB = new ArrayList<>(); // LocalDB @Override public List<Member> members(){ return this.memberDB; } @Override public void memberCreate(Member member){ memberDB.add(member); } @Override public Member memberFindById(long id) { Member member = null; for(Member m : memberDB){ if(m.getId() == id){ return m; } } return member; } }