카테고리 없음

Paging처리

challnum 2022. 9. 9. 19:51
@RestController
@RequiredArgsConstructor
public class BookController {
    private final BookService bookService;

    @ApiOperation("페이징")
    @GetMapping("/book/paging")
    public Page<BookResponseDto> getList(
            @RequestParam("page") int page
    ){
        return bookService.getList(page);
    }
}

@Service
@RequiredArgsConstructor
public class BookServicelmpl implements BookService{
    private final BookRepository bookRepository;
    @Transactional(readOnly = true)
    public Page<BookResponseDto> getList(int page){
        Pageable pageable = PageRequest.of(page,10);
        return bookRepository.findAll(pageable).map(BookResponseDto::new);
    }
}

public interface BookService {
    Page<BookResponseDto> getList(int page);
    }
출력 값
url : localhost:8080/book/paging?id=0

{
    "content": [
        {
            "bookId": 1,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 2,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 3,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 4,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 5,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 6,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 7,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 8,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 9,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        },
        {
            "bookId": 10,
            "bookname": "1",
            "extinction": true,
            "isbn": "1",
            "bookpage": 1,
            "age": 1,
            "price": 1,
            "currency": 1,
            "author": "1"
        }
    ],
    "pageable": {
        "sort": {
            "sorted": false,
            "unsorted": true,
            "empty": true
        },
        "pageSize": 10,
        "pageNumber": 0,
        "offset": 0,
        "paged": true,
        "unpaged": false
    },
    "last": false,
    "totalElements": 14,
    "totalPages": 2,
    "first": true,
    "numberOfElements": 10,
    "size": 10,
    "number": 0,
    "sort": {
        "sorted": false,
        "unsorted": true,
        "empty": true
    },
    "empty": false
}
리펙토링 사유 : Paging 정렬 시 id 값의 정렬이 ASC의 정렬이 되어 생성된 지 오래된 순으로 출력이 되는 것을 확인 하며 코드 자체의 가독성이 떨어지므로 리펙토링

리펙 토링 코드
@RestController
@RequiredArgsConstructor
public class BookController {
    private final BookService bookService;

    @ApiOperation("페이징")
    @GetMapping("/book/paging")
    public Page<BookResponseDto> getList(@PageableDefault(sort="id", direction = Sort.Direction.DESC) Pageable pageable){
        return bookService.getList(pageable);
    }
}

@Service
@RequiredArgsConstructor
public class BookServicelmpl implements BookService{
    private final BookRepository bookRepository;
    @Transactional(readOnly = true)
    public Page<BookResponseDto> getList(Pageable pageable){
        return bookRepository.findAll(pageable).map(BookResponseDto::new);
    }
}

public interface BookService {
    Page<BookResponseDto> getList(Pageable pageable);
}

 

url : localhost:8080/book/paging?page=0&size=5

{
    "content": [
        {
            "bookId": 12,
            "bookname": "1",
            "extinction": true,
            "isbn": 1259060977,
            "bookpage": 1,
            "age": 1,
            "price": 0.11,
            "currency": "1₩",
            "authorList": null,
            "authors": "1,2"
        },
        {
            "bookId": 11,
            "bookname": "1",
            "extinction": true,
            "isbn": 1259060977,
            "bookpage": 1,
            "age": 1,
            "price": 0.11,
            "currency": "1₩",
            "authorList": null,
            "authors": "1,2"
        },
        {
            "bookId": 10,
            "bookname": "1",
            "extinction": true,
            "isbn": 1259060977,
            "bookpage": 1,
            "age": 1,
            "price": 0.11,
            "currency": "1₩",
            "authorList": null,
            "authors": "1,2"
        },
        {
            "bookId": 9,
            "bookname": "1",
            "extinction": true,
            "isbn": 1259060977,
            "bookpage": 1,
            "age": 1,
            "price": 0.11,
            "currency": "1₩",
            "authorList": null,
            "authors": "1,2"
        },
        {
            "bookId": 8,
            "bookname": "1",
            "extinction": true,
            "isbn": 1259060977,
            "bookpage": 1,
            "age": 1,
            "price": 0.11,
            "currency": "1₩",
            "authorList": null,
            "authors": "1,2"
        }
    ],
    "pageable": {
        "sort": {
            "sorted": true,
            "unsorted": false,
            "empty": false
        },
        "pageNumber": 0,
        "pageSize": 5,
        "offset": 0,
        "paged": true,
        "unpaged": false
    },
    "last": false,
    "totalPages": 3,
    "totalElements": 12,
    "number": 0,
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "first": true,
    "numberOfElements": 5,
    "size": 5,
    "empty": false
}​