π©π»βπ¦° κ²μκΈ μ 체 μ‘°ν APIλ₯Ό μ¬μ©νλ ν λ²μ λ λΌμ€λ λ°μ΄ν°μ κ°μκ° λ무 λ§μ μλ΅μλκ° λ립λλ€.
- μ‘°ν μ κ°μ μ ν(
limit
)μ΄ νμν΄μ.μ λ ¬
κΈ°λ₯ μΆκ°κ²μ
κΈ°λ₯ μΆκ°
μμ± μκ°
λ κ°μ΄ μ μ₯ βμμ± μκ°
ν¬ν¨ βμμ± μκ°
ν¬ν¨ βκ²μ ν€μλ
λ‘ κ²μκΈμ κ²μ βκ²μ ν€μλ
κ° ν¬ν¨λ μ λͺ©
μ κ°μ§ κ²μκΈμ μ λΆ μ‘°ν (μ΅λ 100κ°) βupdate
μ UpdatedAt
μ μ΄μ μκ°μ΄ μ μ₯λ¨.update
μ²λΌ λ³κ²½ κ°μ§κ° μΌμ΄λ λλ ν΄λΉ μν°ν°λ₯Ό μ
λ°μ΄νΈ νκΈ° μ΄μ μ μκ°μ΄ μ μ₯λλ€.flush
λ₯Ό λ λ €μ€λ€. boardRepository.flush();
1. main
@SpringBootApplication
@EnableJpaAuditing // JPA Auditing νμ±ν
public class BibiApplication {
2. BaseEntity.class
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
@Getter
public class BaseEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
3. Post.class
@NoArgsConstructor
public class Post extends DataTime { // μμ
4. DTO λ³κ²½
public class PostResponseDTO {
...
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
public PostResponseDTO(Post post) {
...
this.createdAt=post.getCreatedAt();
this.updatedAt = post.getUpdatedAt();
}
// BoardRepository
List<Post> findTop100ByOrderByCreatedAtDesc();
// Service
public List<PostResponseDTO> findAll(){
List<Post> posts = boardRepository.findTop100ByOrderByCreatedAtDesc();
@GetMapping
public ResponseEntity<List<PostResponseDTO>> getPostAll(@RequestParam(required = false) String keyword){
List<PostResponseDTO> allPostDto = boardService.findAll(keyword);
return ResponseEntity.ok(allPostDto);
}
// Repository
@Query("SELECT p FROM Post p " +
"WHERE :keyword IS NULL OR :keyword = '' OR LOWER(p.title) LIKE LOWER(CONCAT('%', :keyword, '%')) " +
"ORDER BY p.createdAt DESC")
List<Post> findRecentPosts(@Param("keyword") String keyword);
// Service
public List<PostResponseDTO> findAll(String keyword){
List<Post> posts = boardRepository.findRecentPosts(keyword);
β‘ νΉμ μν°ν°μμ title
νλμ λν λΆλΆ μΌμΉ κ²μμ μννκ³ , createdAt
νλλ₯Ό κΈ°μ€μΌλ‘ λ΄λ¦Όμ°¨μμΌλ‘ μ΅λ 100κ°μ κ²°κ³Όλ₯Ό λ°ννλ€.
ν€μλκ° μμ κ²½μ° μ 체 μ‘°ν.