[Spring] Paging 처리 코드

[Spring] Paging 처리 코드

Controller

@RequestMapping(value="/getLogPage")

public ResponseEntity> getLogPage(

@RequestParam("hostIp") String hostIp

,@RequestParam("vmId") String vmId

, @RequestParam("range") Integer range

, @RequestParam("page") Integer page) {

Page acsLog = apiService.getLogPage(hostIp, vmId, range, (page-1<0)?0:page-1);

return new ResponseEntity>(acsLog, HttpStatus.OK);

}

===========================================================================================================

Service public Page getLogPage(String hostIp, String vmId, int range, int page) {

int size = range;

PageRequest pageRequest = PageRequest.of(page, size, Sort.by("connectTime").descending());

if( (hostIp != null && !hostIp.isEmpty()) && (vmId != null && !vmId.isEmpty()) )

return accessLogRepository.findByHostIpAndVmId(hostIp, vmId, pageRequest);

else if( (hostIp != null && !hostIp.isEmpty()) )

return accessLogRepository.findByHostIp(hostIp, pageRequest);

else if( (vmId != null && !vmId.isEmpty()) )

return accessLogRepository.findByVmId(vmId, pageRequest);

else

return accessLogRepository.findAll(pageRequest);

}

===========================================================================================================

Repository

public Page findByHostIpAndVmId(String hostIp, String vmId, Pageable pageable);

public Page findByHostIp(String hostIp, Pageable pageable);

public Page findByVmId(String vmId, Pageable pageable);

728x90

from http://junjunrecord.tistory.com/142 by ccl(A) rewrite - 2021-12-14 14:01:01