spring:
datasource:
url: jdbc:h2:./db_test;MODE=MySQLprofiles:
active: test
프로파일 활성화 방법:
application.yml에서 활성화:
profiles: active: test
테스트 클래스에서 활성화:
@SpringBootTest @ActiveProfiles("test") public class PostServiceTest { @Test public void testDBProfile() { System.out.println("Test DB Profile Active"); } }
.gitignore 설정:
테스트 DB 관련 파일 제외.
db_test.mv.db
db_test.trace.db
자동 쿼리 메서드
정의
JPA 리포지토리는 메서드 이름만으로 SQL을 자동 생성.
기본적으로 JpaRepository를 상속받아 사용.
페이징 처리 예제
int itemsPerPage = 2; // 한 페이지에 보여줄 아이템 수int pageNumber = 1; // 현재 페이지 (0부터 시작)
Pageable pageable = PageRequest.of(pageNumber, itemsPerPage, Sort.by(Sort.Direction.DESC, "id"));
Page<Post> postPage = postService.findAll(pageable);
List<Post> posts = postPage.getContent();