Spring Boot - Entity, Repository
JPA를 쉽게 사용하기 위해 스프링에서 제공하는 프레임워크
Main Keyword : Repository
사용자는 인터페이스, 메소드만 정의하면 Spring data가 이름을 분석하여 JPQL 실행
- JpaRepository -> CRUD 기능을 제공하는 인터페이스해당 인터페이스를 상속받아 Spring data jpa 사용
public interface productRepository extends JPARepository<product,Long>, productRepositorySupport{ //CRUD Method….}
ref : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#preface
쿼리 메소드 기능 Spring Data는 메소드 이름에 있는 특정 키워드들을 인식하여 쿼리를 생성
사용중인 실제코드 예제
findByClientId(Long clientId); -> select c from ClientPlannerBind c where c.clientId = ?1
프로젝트 연습 실습 해보기
new > project
Spring Initializr 선택
Project SDK : 8
java version : 8
spring Boot : 2.4.2
Dependency : JPA, Mysql
domain.product 하위에 2개의 Package 생성
- entity
- repository(DAO)
- DTO
- controller
- service
실제 DB의 테이블과 매칭될 클래스
- 테이블과 Mapping될 클래스임을 나타낸다.
- Entity 클래스 또는 가장 Core한 클래스라고 부른다.
- @Entity, @Column, @Id 등을 이용
Mapping Annotation
Object <-> Table : @Entity, @Table
Primary Key : @Id
Field <-> Column : @Column
Join : @ManyToOne, @JoinColumn
mac : command + N
windows : Alt + Insert
Getter and Setter 선택 => 차후엔 Builder annotation 사용.
(Setter 는 사용 안하는것을 권장)
실제로 DB에 접근하는 객체
- Persistence Layer(DB에 data를 CRUD하는 계층)이다.
Service와 DB를 연결하는 고리의 역할을 한다.
SQL를 사용(개발자가 직접 코딩)하여 DB에 접근한 후 적절한 CRUD API를 제공한다.
- JPA 대부분의 기본적인 CRUD method를 제공하고 있다.
- extends JpaRepository<User, Long>
extends CrudRepository -> JPA CRUD 기능들 사용
PagingAndSortingRepository -> CRUD + Paging, Sorting 기능 제공
DB connection
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
CRUD Operation
Paging, Sort Operation