通过实地搜索规范查找弹簧靴

我的问题是我可以从数据库中搜索。但是我确实使用findAll中的JpaSpecificationExecutor进行搜索。但是,我想使用findById进行搜索,并将我的specificationpageableid传递给它,返回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的情况下起作用

dfsfjhsd 回答:通过实地搜索规范查找弹簧靴

我通过使用Specification.Where(your_specification).and(your_search_specification).

找到了答案

现在这是我的更新代码:

Specification<AccountRight> searchSpec = CommonUtil.buildSearchSpecification(search); //this specification needs my search string.
        SearchSpecification<AccountRight> rightByAppointmentID = 
                  new SearchSpecification<AccountRight>(new SearchCriteria("rightByAppointment.appointmentID",SearchOperation.EQUALITY,CommonUtil.getAppointment().getAppointmentID())); //this specification accepts search criteria with key,operation and value.


        Page<AccountRight> accountRightPage = accountRightRepository.findAll(Specification.where(rightByAppointmentID).and(searchSpec),pageable); 

//here you will just tell findAll method to findAll entities where rightByAppointmentID is equal to
   //CommonUtil.getAppointment().getAppointmentID() and search query is searchSpec
本文链接:https://www.f2er.com/2906521.html

大家都在问