Index
1. REST API |
2. 영속성 컨텍스트 (@Transactional) |
3. 쿼리 메서드 (Query Method) |
1. REST API |
(1) 동사보단 명사를, 단수보단 복수를
/member/get/item/hello (X)
/members/items (O)
(2) 마지막에 / 넣지 않기
/members/ (X)
/members (O)
(3) _ 대신 - 사용, 그리고 대문자 사용하지 않기
/restful_services (X)
/restful-services (O)
(4) 확장자 포함하지 않기 (svg, png, exe 등)
/image.svg (X)
/images (O)
(5) 계층화
/items/{memberId}/members/{itemId} (X)
/members/{memberId}/items/{itemId} (O)
2. 영속성 컨텍스트 (@Transactional) |
@Transactional 이 걸린 Context 를 말하고, Context 는 문맥을 의미한다. 영속성 컨텍스트 내에서 DB를 한 번이라도 갔다온 Entity 의 변경사항은 실제로 DB 에 반영된다. 이것을 더티체킹이라고 한다.
3. 쿼리 메서드 (Query Method) |
1) 제목 기반 쿼리
// 특정 제목을 가진 게시물 찾기
List<Board> findByTitle(String title);
// 제목이 특정 단어로 시작하는 게시물 찾기
List<Board> findByTitleStartingWith(String prefix);
// 제목에 특정 단어가 포함된 게시물 찾기
List<Board> findByTitleContaining(String keyword);
// 제목이 특정 단어로 끝나는 게시물 찾기
List<Board> findByTitleEndingWWith(String suffix);
2) 내용 기반 쿼리
// 내용이 비어있는 게시물 찾기
List<Board> findByContentIsNull();
// 제목과 내용이 모두 일치하는 게시물 찾기
List<Board> findByTitleAndContent(String title, String content);
3) 날짜 기반 쿼리
// 게시물 생성일이 특정 날짜 이후인 게시물 찾기
List<Board> findByCreatedAtAfter(LocalDateTime date);
// 게시물 생성일이 특정 날짜인 게시물 찾기
List<Board> findByCreatedAtBefore(LocalDateTime date);
// 게시물 생성일이 특정 날짜 사이에 있는 게시물 찾기
List<Board> findByCreatedAtBetween(LocalDateTime startDate, LocalDateTime endDate);
4) 크기 비교를 활용한 쿼리
// ID가 특정 값보다 큰 게시물 찾기
List<Board> findByIdGreaterThan(Long Id);
// ID가 특정 값보다 작은 게시물 찾기
List<Board> findByIdLessThan(Long Id);
// ID가 특정 값 이상인 게시물 찾기
List<Board> findByIdGreaterThanEqual(Long Id);
// ID가 특정 값 이하인 게시물 찾기
List<Board> findByIdLessThanEqual(Long id);
5) 페이징 및 정렬
// 제목으로 오름차순 정렬된 게시물 찾기
List<Board> findByOrderByTitleAsc();
// ID로 내림차순 정렬된 게시물 찾기
List<Board> findByOrderByIdDesc();
// 페이징 처리된 게시물 찾기
Page<Board> findAll(Pageable pageable);
// 제목이 특정 단어로 시작하는 게시물의 페이징 처리
Page<Board> findByTitleStartingWith(String title, Pageable pageable);
6) 논리 연산자를 활용한 쿼리
// 제목에 특정 단어가 포함된 게시물 찾기
List<Board> findByTitleContaining(String titleKeyword);
// 제목 또는 내용에 특정 단어가 포함된 게시물 찾기
List<Board> findByTitleContainingOrContentContaining(String titleKeyWord, String contentKeyword);
// 제목과 내용이 둘다 특정 단어를 포함하는 게시물 찾기
List<Board> findByTitleContainingAndContentContaining(String titleKeyword, String contentKeyword);
7) 댓글 관련 쿼리 메서드
// 특정 게시물에 달린 댓글 찾기
List<Comment> findByBoard(Board board);
// 특정 게시물 ID로 댓글 찾기
List<Comment> findByBoardId(Long boardId);
// 특정 게시물의 댓글 중 특정 단어가 포함된 댓글 찾기
List<Comment> findByBoardAndContentContaining(Board board, String keyword);
// 특정 내용이 특정 단어로 시작하는 댓글 찾기
List<Comment> findByContentStartingWith(String keyword);
// 댓글 내용이 특정 단어로 끝나는 댓글 찾기
List<Comment> findByContentEndingWith(String keyword);
8) 크기 비교를 활용한 댓글 쿼리
// 댓글 ID 가 특정 값보다 큰 댓글 찾기
List<Comment> findByIdGreaterThan(Long id);
// 댓글 ID 가 특정 값보다 작은 댓글 찾기
List<Comment> findByIdLessThan(Long id);
// 댓글 ID 가 특정 값 이상인 댓글 찾기
List<Comment> findByIdGreaterThanEqual(Long id);
// 댓글 ID 가 특정 값 이하인 댓글 찾기
List<Comment> findByIdLessThanEqual(Long id);
9) 날짜 기반 댓글 쿼리
// 댓글 생성일이 특정 날짜 이후인 댓글 찾기
List<Comment> findByCreatedAfter(LocalDateTime date);
// 댓글 생성일이 특정 날짜 이전인 댓글 찾기
List<Comment> findByCreatedAtBefore(LocalDateTime date);
// 댓글 생성일이 특정 날짜 사이에 있는 댓글 찾기
List<Comment> findByCreatedAtBetween(LocalDateTime startDate, LocalDateTime endDate);
10) 특정 개수 이상의 댓글을 가진 게시물 찾기
List<Board> findByComments_SizeGreaterThanEqual(int count);
11) 특정 길이 이상의 제목을 가진 게시물 찾기
List<Board> findByTitleLengthGreaterThan(int length);