3장
1분 퀴즈
1. ②
2. ㉠ - @Entity, ㉡ - CrudRepository, ㉢ - @Autowired
3. ㉠ - ②, ㉡ - ⑤, ㉢ - ④, ㉣ - ①, ㉤ - ③
셀프체크
1. 컨트롤러
controller/MemberController.java
package com.example.firstproject.controller;
import com.example.firstproject.entity.Member;
import com.example.firstproject.dto.MemberForm;
import com.example.firstproject.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class MemberController {
@Autowired
MemberRepository memberRepository;
@GetMapping("/signup")
public String signUpPage() {
return "members/new";
}
@PostMapping("/join")
public String join(MemberForm memberForm) {
System.out.println(memberForm.toString());
Member member = memberForm.toEntity();
System.out.println(member.toString());
Member saved = memberRepository.save(member);
System.out.println(saved.toString());
return "";
}
}
2. DTO
dto/MemberForm.java
package com.example.firstproject.dto;
import com.example.firstproject.entity.Member;
public class MemberForm {
private String email;
private String password;
public MemberForm(String email, String password) {
this.email = email;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
public Member toEntity() {
return new Member(null, email, password);
}
}
3. 엔티티
entity/Member.java
package com.example.firstproject.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
@Entity
public class Member {
@Id
@GeneratedValue
Long id;
@Column
String email;
@Column
String password;
public Member(Long id, String email, String password) {
this.id = id;
this.email = email;
this.password = password;
}
@Override
public String toString() {
return "Member{" +
"id=" + id +
", email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
4. 리파지터리
repository/MemberRepository.java
package com.example.firstproject.repository;
import com.example.firstproject.entity.Member;
import org.springframework.data.repository.CrudRepository;
public interface MemberRepository extends CrudRepository<Member, Long> {
}