JPA는 인터페이스로서 자바 표준 명세서
JPA를 사용하기 위해서 구현체가 필요 - Hibernate, Eclipse link
구현체를 쉽게 사용하고자 추상화 시킨 Spring Data JPA라는 모듈을 이용해서 JPA 기술을 다룸
Spring Data JPA 등장 이유
구현체 교체의 용이성
Hiberate 외에 다른 구현체로 쉽게 교체하기 위함
저장소 교체의 용이성
관계형 데이터베이스 외에 다른 저장소로 쉽게 교체하기 위함
2 프로젝트에 Spring Data Jpa 적용
//Entity클래스에는 절대 Setter메소드를 만들지 않음.
//DB와 실제 매칭되는 클래스 Entity클래스.
@Getter
@NoArgsConstructor //기본 생성자 자동추가 public Posts(){}
@Entity //JPA의 어노테이션
public class Posts {
@Id //Pk
@GeneratedValue( strategy = GenerationType.IDENTITY) //Pk 생성규칙 auto_increment
private Long id;
@Column(length = 500, nullable = false) //꼭 선언안해도 컬럼으로 인식, 기본값 이외의 추가사항이 있는 경우 사용
private String title;
@Column(columnDefinition = "TEXT", nullable = false)
private String content;
private String author;
@Builder //생성자 대신 사용
public Posts(String title, String content, String author){
this.title = title;
this.content = content;
this.author = author;
}
}
JpaRepository
도메인 클래스로 DB를 접근하게 해줌
public interface PostsRepository extends JpaRepository<Posts, Long> {
}
출처 : [도서] 스프링부트와 AWS로 혼자 구현하는 웹서비스