<aside> 💡 findAll, findById, deleteById, save 네가지만 기본적으로 제공한다.
</aside>
JAP Repository를 상속받는다 (인터페이스는 인터페이스를 익스텐즈(상속) 해야한다)
package shop.mtcoding.blog.user;
import org.springframework.data.jpa.repository.JpaRepository;
// 자동 컴퍼넌트 스캔이 된다.
// findAll, findById, deleteById, save 네가지만 기본적으로 제공한다.
public interface UserJAPRepository extends JpaRepository<User, Integer> {
}
자바에서 메모리에 띄울 수 있는건 클래스 빡에 없다 즉 인터페이스는 new 할 수 없다.
인터페이스를 new 하려면 아무 클래스를 하나 만들어서 임플리먼트 해야 메모리에 띄울 수 있다.
JpaRepository의 <>안에 들어가는 값은
앞쪽은일반적으로 엔티티 클래스를 나타냅니다. 즉, 데이터베이스 테이블과 매핑되는 객체입니다.
뒤쪽은 프라이머리 키값의 타입을 알려줘야 한다.
save 테스트
package shop.mtcoding.blog.user;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
// 임포트가 필요없다. (자동으로 해줌 적으면 오류남)
@DataJpaTest
public class UserJPARepositoryTest {
@Autowired
private UserJAPRepository userJAPRepository;
@Test
public void save_test(){
// given
User user = User.builder()
.username("happy")
.password("1234")
.email("[email protected]")
.build();
// when
userJAPRepository.save(user);
// then
}
}

findById 테스트 (옵셔널)
@Test
public void findById_test(){
// given
int id = 1;
// when
// 존재하지 않는값이 온다면 널을 받는다.
Optional<User> userOp = userJAPRepository.findById(id);
// 만약에 존재 한다면
if (userOp.isPresent()) {
// 겟으로 받아옴.
User user = userOp.get();
System.out.println("findById_test : " + user.getUsername());
}
// then
}

값을 5로 바꾸면 이프문을 거치지 않아서 아무것도 찾지 않는다.