Notice
Recent Posts
Recent Comments
Link
개발 무지렁이
[Spring Boot] 리포지터리 CRUD 메서드 테스트 본문
리포지터리
public interface QuestionRepository extends JpaRepository<Question, Integer> {
// 리포지터리로 만들기 위해 JpaRepository 인터페이스를 상속
// < 대상이 되는 엔티티타입, PK 속성 타입 >
}
Create
save()
@SpringBootTest class App59ApplicationTests { @Autowired // 객체 주입 private QuestionRepository questionRepository; @Test void testJpa() { Question q = new Question(); q.setSubject("안녕하세요"); q.setContent("가입인사드립니다^^"); q.setCreateDate(LocalDateTime.now()); this.questionRepository.save(q); } }
Read
findAll()
@SpringBootTest class App59ApplicationTests { @Autowired private QuestionRepository questionRepository; @Test void testJpa() { List<Question> all = this.questionRepository.findAll(); assertEquals(1, all.size()); // JUnit, (기대값, 실제값) } }
findById()
@SpringBootTest class App59ApplicationTests { @Autowired private QuestionRepository questionRepository; @Test void testJpa() { Optional<Question> oq = this.questionRepository.findById(1); // 리턴타입 Optional<> if(oq.isPresent()) { Question q = oq.get(); assertEquals("안녕하세요.", q.getSubject()); } } }
❓Optional
: null 처리를 유연하기 하기 위해 사용하는 클래스 (간편한 기능이 섞인 포장지)findBySubject() (findBy대상엔티티속성())
public interface QuestionRepository extends JpaRepository<Question, Integer> { Question findBySubject(String subject); }
@SpringBootTest class App59ApplicationTests { @Autowired private QuestionRepository questionRepository; @Test void testJpa() { Question q = this.questionRepository.findBySubject("안녕하세요."); assertEquals(1, q.getId()); } }
findBySubjectAndContent() (두개의 속성을 And 조건으로 조회)
public interface QuestionRepository extends JpaRepository<Question, Integer> { Question findBySubject(String subject); Question findBySubjectAndContent(String subject, String content); }
@SpringBootTest class App59ApplicationTests { @Autowired private QuestionRepository questionRepository; @Test void testJpa() { Question q = this.questionRepository.findBySubjectAndContent("안녕하세요.", "가입인사드립니다.^^"); assertEquals(1, q.getId()); } }
※ Or / Between / Like / In / OrderBy
Update
@SpringBootTest class App59ApplicationTests { @Autowired private QuestionRepository questionRepository; @Test void testJpa() { Optional<Question> oq = this.questionRepository.findById(1); if(oq.isPresent()) { Question q = oq.get(); q.setSubject("반갑습니다."); this.questionRepository.save(q); } } }
Delete
delete()
@SpringBootTest class App59ApplicationTests { @Autowired private QuestionRepository questionRepository; @Test void testJpa() { assertEquals(1, this.questionRepository.count()); Optional<Question> oq = this.questionRepository.findById(1); if(oq.isPresent()) { Question q = oq.get(); this.questionRepository.delete(q); assertEquals(0, this.questionRepository.count()); } } }
'Backend > 스프링부트' 카테고리의 다른 글
[Spring Boot] File 객체와 MultipartFile 인터페이스 (0) | 2022.11.26 |
---|---|
[Spring Boot] 기본세팅 (0) | 2022.11.26 |
[Spring Boot] 쿠키(Cookie)와 세션(Session), CSRF (0) | 2022.11.26 |
[Spring Boot] ORM의 표준스펙 JPA 및 엔티티(Entity) (0) | 2022.11.25 |
[Spring Boot] 스프링부트 프로젝트의 구조와 컨트롤러 (0) | 2022.11.25 |
Comments