[Spring] Paging 처리 코드
Controller
@RequestMapping(value="/getLogPage")
public ResponseEntity<Page<AccessLog>> getLogPage(
@RequestParam("hostIp") String hostIp
,@RequestParam("vmId") String vmId
, @RequestParam("range") Integer range
, @RequestParam("page") Integer page) {
Page<AccessLog> acsLog = apiService.getLogPage(hostIp, vmId, range, (page-1<0)?0:page-1);
return new ResponseEntity<Page<AccessLog>>(acsLog, HttpStatus.OK);
}
===========================================================================================================
Service public Page<AccessLog> 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<AccessLog> findByHostIpAndVmId(String hostIp, String vmId, Pageable pageable);
public Page<AccessLog> findByHostIp(String hostIp, Pageable pageable);
public Page<AccessLog> findByVmId(String vmId, Pageable pageable);