Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- JPA Insert
- Spring JPA
- javascrpit 기초
- react link
- JPA Update\
- editor Quill
- Spring Controller return
- Spring Entity
- spring security
- step 테이블
- Docker Windows 설치
- react Quill
- Spring DTO
- Javascript
- react forwardRef
- 코드 중복제거
- Spring CORS
- javascript 기초
- springbatch
- spring builder
- react Page
- react
- 텍스트가 많은 경우
- javascript 함수
- react react-router-dom v6
- spring
- SpringBatch 스키마
- react jsx if
- react quill custom
- springbatch chunk
Archives
- Today
- Total
천천히 알아보는 코딩공부
[Spring] API로 페이징 처리 Pageable 본문
게시판들을 보면 게시글을 전체 조회 하지않고 개수를 정해서 조회를 하고 페이지 별로 나눠져서 조회를 하고 있다
구현해보자!!
JPA로 구현하였다.
Controller
@RestController
@RequestMapping("/adminKisline/notice")
public class AdminNoticeController{
@Autowired
private AdminMasterService service;
@CrossOrigin(origins = "*")
@GetMapping("/noticeSelect")
public Page<AdminNoticeEntity> NoticeSelect(
@PageableDefault(size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable)
{
return service.getAdminNoticeList(pageable);
}
}
pageable -> org.springframework.data.domain
@PageableDefault 파라미터 입력 안할 시 size(페이지당 개수), sort 해당 컬럼정렬 설정
Service(Interface)
public interface AdminMasterService {
public Page<AdminNoticeEntity> getAdminNoticeList(Pageable pageable);
}
impl
public class AdminMasterService implements AdminMasterService {
@Override
Public Page<AdminNoticeEntity> getAdminNoticeList(Pageable pageable)
{
return (Page<AdminNoticeEntity>) adminNotice,findAll(pageable);
}
}
Entity
@Data
@Entity
@Table(name="AdminNotice")
public class AdminNoticeEntity {
@Id
private int id;
private String serviceName;
private String serviceGroup;
private String title;
private String content;
private String writeId;
private String writeDate;
private Stromg uptUsrid;
private String uptDtm;
}
Repository
@EnableJpaRepositories
public interface AdminNoticeRepository extends CrudRepository<AdminNoticeEntity, Long> {
Page<AdminNoticeEntity> findAll(Pageable pageable);
}
호출 URL : localhost:포트/adminKisline/notice/noticeSelect
파라미터 입력시 localhost:포트/adminKisline/notice/noticeSelect?page=2&size=10
출력결과 :
{
"content": [
결과값
],
"pageable": {
"sort": { "sorted": false, "unsorted": true, "empty": true },
"pageNumber": 3,
"pageSize": 4,
"offset": 12,
"paged": true,
"unpaged": false
},
"totalPages": 25,
"totalElements": 100,
"last": false,
"numberOfElements": 4,
"number": 3,
"sort": { "sorted": false, "unsorted": true, "empty": true },
"size": 4,
"first": false,
"empty": false
}
반환 타입에 따른 페이징 결과
Spring Data JPA 에는 반환 타입에 따라서 각기 다른 결과를 제공한다.
- Page<T> 타입
- Slice<T> 타입
- List<T> 타입
각자 다른 결과를 반환해준다.
Page<T> 타입
Page<T> 타입을 반환 타입으로 받게 된다면 offset과 totalPage 를 이용하여 서비스를 제공할 수 있게된다.
Page<T> 는 일반적인 게시판 형태의 페이징에서 사용된다.
'Java > SpringBoot' 카테고리의 다른 글
[Spring] Lombok 자주 사용하는 어노테이션 정리 (0) | 2022.12.02 |
---|---|
[spring] DTO, Entity 차이 및 전달 로직 (0) | 2022.12.01 |
[Spring] JPA Select Join 일대일 일대다 (0) | 2022.10.24 |
[Spring] JPA Select (0) | 2022.10.17 |
[Spring] API 호출 시 CORS 에러 (0) | 2022.10.06 |
Comments