Memo.java // jpa를 통해 Entity(sql의 table)로 사용
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;
}
public Memo(MemoRequestDto requestDto) {
this.username = requestDto.getUsername();
this.contents = requestDto.getContents();
}
}
Timestamped.java // 데이터의 기본인 생성일자와 수정일자를 추상메서드로 만들어 상속을 통해 사용
@MappedSuperclass // Entity가 자동으로 컬럼으로 인식합니다.
@EntityListeners(AuditingEntityListener.class) // 생성/변경 시간을 자동으로 업데이트합니다.
public class Timestamped {
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime modifiedAt;
}
MemoRepository.java // Entity를 이 Repository를 통해 table로 바꿔 sql에 삽입
public interface MemoRepository extends JpaRepository<Memo, Long> {
List<Memo> findAllByOrderByModifiedAtDesc();
// All 전부 /OrderBy 정렬한다 /Desc 내림차순으로(최신순)
// find...By(첫번째 By ) ...을 찾아줘/ ModifiedAt Timestamped.java 의 수정 일자
}
'타임라인 만들기 Project' 카테고리의 다른 글
HTML, CSS (0) | 2020.11.22 |
---|---|
Controller (0) | 2020.11.22 |
Service (0) | 2020.11.20 |
프로젝트 만들고 API 설계하기 (0) | 2020.11.18 |
타임라인 만들기 Start (0) | 2020.11.17 |