我的问题是我可以从数据库中搜索。但是我确实使用findAll
中的JpaSpecificationExecutor
进行搜索。但是,我想使用findById
进行搜索,并将我的specification
,pageable
和id
传递给它,返回page
。但这是行不通的。
这是我的controller
:
@GetMapping(value = "/search")
public ResponseEntity<ResponseDTO> allaccountRightService(
@RequestParam(value = "search",required = false) String search,@RequestParam(value = "page",required = false) Integer page,@RequestParam(value = "size",required = false) Integer size,@RequestParam(value = "order",required = false) String order,@RequestParam(value = "orderBy",required = false) String orderBy) {
ResponseDTO responseDTO = new ResponseDTO("accountRightService List",accountRightService.search(search,page,size,order,orderBy));
return new ResponseEntity<>(responseDTO,HttpStatus.OK);
}
and here is my `service impl` method:
public Map<PageInformation,List<accountRightDTO>> search(String search,Integer page,Integer size,String order,String orderBy) {
Map<PageInformation,List<accountRightDTO>> accountRightList = new HashMap<>();
PageInformation pageInfo = new PageInformation();
if (order == null || order.isEmpty())
order = "DESC";
if (orderBy == null || orderBy.isEmpty())
orderBy = "createdAt";
Pageable pageable = CommonUtil.createPageRequest(page,orderBy);
Specification<accountRight> spec = CommonUtil.buildSearchSpecification(search);
//Page<accountRight> accountRightPage = accountRightRepository.findAllByRightByAppointment(CommonUtil.getappointment().getappointmentID(),spec,pageable);
Page<accountRight> accountRightPage = accountRightRepository.findAll(spec,pageable);
List<accountRight> accountRights = accountRightPage.getcontent();
List<accountRightDTO> accountRightDTOs = new ArrayList<>();
accountRightDTOs = accountRights.stream().map(accountRight -> {
accountRightDTO accountRightDTO = new accountRightDTO();
AppointmentDTO rightToAppointmentDTO = new AppointmentDTO();
AppointmentDTO rightByAppointmentDTO = new AppointmentDTO();
BeanUtils.copyproperties(accountRight,accountRightDTO,"accountRightID");
accountRightDTO.setaccountRightID(Long.toString(accountRight.getaccountRightID()));
BeanUtils.copyproperties(accountRight.getRightToAppointment(),rightToAppointmentDTO,"appointmentID");
rightToAppointmentDTO.setappointmentID(Long.toString(accountRight.getRightToAppointment().getappointmentID()));
BeanUtils.copyproperties(accountRight.getRightByAppointment(),rightByAppointmentDTO,"appointmentID");
rightByAppointmentDTO.setappointmentID(Long.toString(accountRight.getRightToAppointment().getappointmentID()));
accountRightDTO.setRightByAppointment(rightByAppointmentDTO);
accountRightDTO.setRightToAppointment(rightToAppointmentDTO);
return accountRightDTO;
}).collect(Collectors.toList());
pageInfo.setSize(accountRightPage.getSize());
pageInfo.setTotalElements(accountRightPage.getTotalElements());
pageInfo.setTotalPages(accountRightPage.getTotalPages());
accountRightList.put(pageInfo,accountRightDTOs);
return accountRightList;
}
这是我的buildsearchspecification
方法
public static <T> Specification<T> buildSearchSpecification(String search) {
SearchSpecificationsBuilder<T> builder = new SearchSpecificationsBuilder<T>();
if (search != null && !search.isEmpty()) {
String[] str = search.split(",");
if (str != null) {
for (String strTemp : str) {
Pattern pattern = Pattern.compile("(\\p{Punct}?)(.*)(:|!|<|>|~)(.*)(\\p{Punct}?),");
Matcher matcher = pattern.matcher(strTemp + ",");
while (matcher.find()) {
builder.with(matcher.group(1),matcher.group(2),SearchOperation.getSimpleOperation(matcher.group(3).toCharArray()[0]),matcher.group(4));
}
}
}
}
Specification<T> spec = builder.build();
return spec;
}
这是我的findAllByRightByAppointment
存储库方法
@Query("select account from accountRight account where account.rightByAppointment.appointmentID=?1")
Page<accountRight> findAllByRightByAppointment(Long appointmentID,@Nullable Specification<accountRight> spec,Pageable pageable);
如果我使用findAll
方法,则搜索将起作用,否则使用我的自定义方法pagination
会在没有searching
的情况下起作用