일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- SWEA
- 코드스쿼드 마스터즈
- algorithm
- 쉽게 배우는 운영체제
- 2021.01.14
- 백준 9093
- 백준 1149
- 알고리즘데이
- 알고리즘
- 2021.01.12
- 괄호
- 백준
- baekjoon1541
- Til
- 자바
- 2020.01.08
- spring-boot
- 2021.01.13
- 2021.01.21
- java
- 2021.01.22
- 2021.01.18
- 2021.01.17
- 잃어버린 괄호
- 코드스쿼드
- 박재성
- 2021.01.11
- 2021.01.06
- 2021.01.19
- 마스터즈 2주차 회고
- Today
- Total
Cooper's devlog
3-2. 자바 객체와 테이블 매핑, 회원가입 기능 구현 본문
3-2. 자바 객체와 테이블 매핑, 회원가입 기능 구현
1. 강의 링크
https://www.youtube.com/watch?v=69tNvDm-iiI&list=PLqaSEyuwXkSppQAjwjXZgKkjWbFoUdNXC&index=17
2. 학습 목표
- 데이터베이스의 테이블과 자바 객체를 매핑
- 회원가입 기능을 DB를 사용하도록 수정
- 회원목록 기능을 DB를 사용하도록 수정
3. 과정
(1) spring-boot-jpa dependency 주입
[1] maven repository에 spring-boot-jpa 검색
[2]pom.xml에 dependency 추가
(spring-boot-starter-parent에 버전이 써져있으므로 해당 내용만 입력)
(2) domain 설정하기
[1] User.class 내용 수정
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
32
33
34
35
36
37
38
39
|
package net.slipp.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity //해당 class를 Entity(table)로 사용하겠다는 의미
public class User {
@Id //primary_key로 설정
@GeneratedValue //database에서 자동으로 값을 1씩 증가시키는 역할
private Long id;
//(1) nullable = false : not null 설정 (2) length = 20 : id 문자열 길이
@Column(nullable = false, length = 20)
private String userId;
private String password;
private String name;
private String email;
public void setUserId(String userId) {
this.userId = userId;
}
public void setPassword(String password) {
this.password = password;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [userId=" + userId + ", password=" + password + ", name=" + name + ", email=" + email + "]";
}
}
|
[2]application.properties 설정 변경
[3]브라우저로 loacalhost:8787/h2-console 접속
-test connection으로 test success 확인 후, connection
[4] User table 및 column 내용 확인
(3) UserRepository 설정
1
2
3
4
5
6
7
8
|
package net.slipp.domain;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long>{
}
|
cs |
-JpaRepository<class type, id 필드타입>
(4) UserController 변경
- ArrayList users 제거
- UserRepository추가
-@GetMapping("list")에 addAllAttribute로 변경
<기존 코드>
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
32
33
34
35
36
37
|
package net.slipp.web;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import net.slipp.domain.User;
import net.slipp.domain.UserRepository;
@Controller
public class UserController {
private List<User> users = new ArrayList<User>();
@Autowired
private UserRepository userRepository;
@PostMapping("/create")
public String create(User user) {
System.out.println("user: " + user);
users.add(user);
userRepository.save(user);
return "redirect:/list";
}
@GetMapping("/list")
public String list(Model model) {
model.addAttribute("users", users);
return "list";
}
}
|
<변경 코드>
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
32
|
package net.slipp.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import net.slipp.domain.User;
import net.slipp.domain.UserRepository;
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
@PostMapping("/create")
public String create(User user) {
System.out.println("user: " + user);
userRepository.save(user);
return "redirect:/list";
}
@GetMapping("/list")
public String list(Model model) {
model.addAttribute("users", userRepository.findAll());
return "list";
}
}
|
cs |
※서버 종료 시, table drop 후, 재시작 시, table create작업을 진행한다.
-> 이후 수업헤서 설정을 변경할 예정