spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
브라우저: localhost:8080/h2-console
console url: 1번 두번째 코드 url
jdbc:h2:mem:testdb
Spring | MySQL |
---|---|
Domain | Table |
Repository | SQL |
src>main>resources>application.properties 에다가 아래와 같이 입력 추가
spring.jpa.show-sql:true
@Getter //
@MappedSuperclass // 상속했을 때, 컬럼으로 인식 -> table(=Entity) 의 칼럼요소로 인식
@EntityListeners(AuditingEntityListener.class) // 생성/수정 시간을 자동으로 반영하도로 설정
public abstract class TimeStamped {
@CreatedDate // 생성일자
private LocalDateTime creadtedAt;
@LastModifiedBy // 마지막 수정일자
private LocalDateTime modifiedAt;
}
Week02Application 에 @SpringBootApplication 과 함께 @EnableJpaAuditing 입력
Course 클래스 TimeStamped 상속 받음 -> DB 에 자동으로 CREATED_AT, MODIFIED_AT coulumn 생성
내용 | JPA | 메소드 위치 |
---|---|---|
Create | repository.save(object) | repository 객체 |
Read | repsotory.findById(id) repository.findAll() | repository 객체 |
Update | service.update(id, object) | entity에 생성, 서비스에서 이용 |
Delete | repository.deleteById(id) repository.deleteAll() | repository 객체 |
스프링의 3영역
영역 | 내용 |
---|---|
Controller | 가장 바깥쪽 부분, 요청/응답을 처리함, 자동응답기? |
Service | 중간 부분, 실제 중요한 동작이 많이 일어나는 부분 |
Repository | 가장 안쪽 부분, DB와 맞닿아 있음 |
udpdate() 메소드는 Entity(Course.class) 내부에 작성
@Getter: getter 만들기
@NoArgsConstructor: 기본생성자 만들기
예) pubic ClassName() {}
@RequiredArgsConstructor: service 에서 생성자(constructor)를 통해 final 변수(repository)를 초기화한다.
데이터를 update 할 때는 직접 Entity 접근하지 않고,
DTO 라는 매개객체를 이용하여 update 한다.
package com.sparta.week02.domain;
import lombok.Getter;
@Getter
public class CourseRequestDto {
private final String title;
private final String tutor;
}
👉 REST란, 주소에 명사, 요청 방식에 동사를 사용함으로써 의도를 명확히 드러냄을 의미합니다.