728x90
반응형
## 게시판 만들기
1.Memo 클래스 만들기
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
40
41
42
43
44
45
46
|
package com.sparta_spring.sparta_spring_week03.domain;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@NoArgsConstructor // 빈생성자
@Getter
@Entity // 테이블과 연계됨을 스프링에게 알려줍니다.
public class Memo extends Timestamped{
// 아이디
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;
// 열
// 사용자 이름
@Column(nullable = false)
private String username;
// 메모 내용
@Column(nullable = false)
private String contents;
// 생성자
// 유저이름, 메모내용으로 생성자
public Memo(String username,String contents){
this.username = username;
this.contents = contents;
}
// DTO를 이용해서 생성
public Memo(MemoRequestDto requestDto){
this.username = requestDto.getUsername();
this.contents = requestDto.getContents();
}
// 업데이트 메서드
public void update(MemoRequestDto memoRequestDto){
this.username = memoRequestDto.getUsername();
this.contents = memoRequestDto.getContents();
}
}
|
cs |
2. Timestamped 생성시간, 수정시간 클래스 만들기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.sparta_spring.sparta_spring_week03.domain;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;
@MappedSuperclass // Entity가 자동으로 컬럼으로 인식합니다.
@EntityListeners(AuditingEntityListener.class) // 생성/변경 시간을 자동으로 업데이트합니다.
public abstract class Timestamped {//abstract 추상형: new로 생성 못함
@CreatedDate // 생성날짜
private LocalDateTime createdAt;
@LastModifiedDate// 마지막 수정낳=ㄹ짜
private LocalDateTime modifiedAt;
}
|
cs |
3.MemoRepository 인터페이스 만들기
1
2
3
4
5
6
7
8
9
10
11
|
package com.sparta_spring.sparta_spring_week03.domain;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface MemoRepository extends JpaRepository<Memo,Long> {
List<Memo> findAllByOrderByModifiedAtDesc(); // 수정 날짜로 정렬된 조회
// jpa 공식문서에 여러 jpa 기능 있음
}
|
cs |
4. 정보의 매개인자 MemoRequestDto 클래스 만들기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.sparta_spring.sparta_spring_week03.domain;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@RequiredArgsConstructor // 의존주입 생성자
@Getter
public class MemoRequestDto {
// 멤버 변수
private String username;
private String contents;
}
|
cs |
반응형
'SpringBoot > 스파르타 웹개발의 봄 spring' 카테고리의 다른 글
[스파르타 웹개발의 봄 spring] 03.05 controller 만들기 (0) | 2021.07.12 |
---|---|
[스파르타 웹개발의 봄 spring] 02.09 API-GET (0) | 2021.07.12 |
[스파르타 웹개발의 봄 spring] 03.04 Service 만들기 (0) | 2021.07.12 |
[스파르타 웹개발의 봄 spring] 02.10 API-POST,PUT,DELETE (0) | 2021.07.11 |
[스파르타 웹개발의 봄 spring] 01주차 필기 (0) | 2021.07.09 |