Java spring引导按字段查找搜索规范

Java spring引导按字段查找搜索规范,java,spring,spring-boot,spring-data-jpa,specifications,Java,Spring,Spring Boot,Spring Data Jpa,Specifications,我的问题是我可以从数据库中搜索。但是我使用JpaSpecificationExecutor的findAll进行了搜索。但是,我想使用findById进行搜索,并将我的规范、pageable和id传递给它返回页面。但它不起作用 这是我的控制器: @GetMapping(value = "/search") public ResponseEntity<ResponseDTO> allAccountRightService( @Req

我的问题是我可以从数据库中搜索。但是我使用
JpaSpecificationExecutor
findAll
进行了搜索。但是,我想使用
findById
进行搜索,并将我的
规范
pageable
id
传递给它返回
页面
。但它不起作用

这是我的
控制器

    @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, size, order, 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;
    }
这是我的
findallByAppointment
存储库方法

@Query("select account from AccountRight account where account.rightByAppointment.appointmentID=?1")
    Page<AccountRight> findAllByRightByAppointment(Long appointmentID, @Nullable Specification<AccountRight> spec, Pageable pageable);
@Query(“从AccountRight account中选择account,其中account.rightByAppointment.appointmentID=?1”)
Page findAllByRightByAppointment(长appointmentID,@Nullable规范规范,Pageable Pageable);

如果我使用
findAll
方法,则使用我的自定义方法进行搜索
pagination
不进行
搜索
我使用
规范找到了答案。Where(您的搜索规范)。and(您的搜索规范)。

这是我现在更新的代码:

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
Specification searchSpec=CommonUtil.buildSearchSpecification(搜索)//此规范需要我的搜索字符串。
SearchSpecification RightByAppointId=
新的搜索规范(新的搜索条件(“rightByAppointment.appointmentID”、SearchOperation.EQUALITY、CommonUtil.getAppointment().GetAppointId())//本规范接受带有键、操作和值的搜索条件。
Page accountRightPage=accountRightRepository.findAll(Specification.where(RightByAppointId))和(searchSpec),可分页;
//在这里,您只需将findAll方法告知rightByAppointmentID等于的findAll实体
//CommonUtil.getAppointment().getAppointmentID()和搜索查询为searchSpec
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