gpt4 book ai didi

java - spring boot 按字段搜索规范查找

转载 作者:行者123 更新时间:2023-12-01 19:19:38 26 4
gpt4 key购买 nike

我的问题是我可以从数据库中搜索。但我使用 JpaSpecificationExecutorfindAll 进行了搜索。但是,我想使用 findById 进行搜索,并将我的 specificationpageableid 传递给它 return 页面。但它不起作用。

这是我的 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, 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;
}

这是我的buildsearchspecation方法

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 无需搜索

最佳答案

我通过使用 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

关于java - spring boot 按字段搜索规范查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59374281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com