TIL

Spring Data Repository 추상화

RepositoryItemReader

interface HackNoteRepository : ElasticsearchRepository<HackNote, String> {
    fun searchNotesByMessageAndSentimentIsNull(keyword: String, pageable: Pageable): Page<HackNote>
}
fun reader(): RepositoryItemReader<HackNote> =
    RepositoryItemReaderBuilder<HackNote>()
        .name("hackNoteItemReader")
        .repository(hackNoteRepository)
        .methodName("searchNotesByMessageAndSentimentIsNull")
        .arguments(mutableListOf<String>("PWN"))
        .pageSize(10)
        .sorts(mapOf("timestamp" to Sort.Direction.DESC))
        .build()

RepositoryItemReaderPagingAndSortingRepository를 사용하기에 오프셋 방식의 페이징을 사용한다. 데이터가 많아질수록 성능에 주의해야 한다.

RepositoryItemWriter

@Bean
fun logWriter(): RepositoryItemWriter<HackNote> {
    return RepositoryItemWriterBuilder<HackNote>()
        .repository(hackNoteRepository)
        .methodName("save")
        .build()
}