Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在字符串列上使用IN或与spring数据一起使用_Spring_Pagination_Spring Data - Fatal编程技术网

在字符串列上使用IN或与spring数据一起使用

在字符串列上使用IN或与spring数据一起使用,spring,pagination,spring-data,Spring,Pagination,Spring Data,我们使用的是spring数据,需要运行一个查询,如果选择了一个状态,我们将针对该状态和'ALL'where state IN('NY','ALL')或where(state='NY'或state='ALL) 在数据库中,stateCode被定义为一个字符串,最多允许3个字符,状态缩写或ALL 我们的实体如下 Documents.java @Entity(name = "documents") @Data public class Documents { @Id private I

我们使用的是spring数据,需要运行一个查询,如果选择了一个状态,我们将针对该状态和'ALL'
where state IN('NY','ALL')
where(state='NY'或state='ALL)

在数据库中,
stateCode
被定义为一个字符串,最多允许3个字符,状态缩写或
ALL

我们的实体如下

Documents.java
@Entity(name = "documents")
@Data
public class Documents {
    @Id
    private Integer docId;
    private String stateCode;
    private String displayName;
    private Integer isEmployee;
    private Integer categoryId;
    private Integer subjectId;
}
在我们的
DocumentService
中,我们使用
ExampleMatcher
返回结果,并将结果传递给
页面
对象,以便与
分页前端
一起使用。问题是,当选择状态时,我们无法修改代码以允许将
All
作为选项。有没有办法做到这一点

我们可以运行两个单独的查询并将它们合并到一个列表中,但是我们的分页会出现混乱,因为它不是以
页面
对象开始的,并且计数无效

public ManualsFrontEnd getAllDocuments(Integer page, Integer total, Documents documentSearchCriteria) {
    if (page == null) page = 0;
    if (total == null) total = 100;
    documentSearchCriteria.setIsActive(1);
    if (documentSearchCriteria.getBrandId() == null) {
        documentSearchCriteria.setBrandId(2);   // Brand = AI
    }

    Pageable pageable = new PageRequest(page, total);
    ExampleMatcher matcher = ExampleMatcher.matching()
            .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
            .withIgnoreCase();
    Example<Documents> example = Example.of(documentSearchCriteria, matcher);

    Page<Documents> pageList = documentsRepository.findAll(example, pageable);
    List<Documents> documentList = pageList.getContent();
    List<DocumentsFrontEnd> frontDocList = DocumentMapper.mapViewDocsTOList(documentList);
    PaginationFrontEnd newPagination = new PaginationFrontEnd(toIntExact(pageList.getTotalElements()), pageList.getPageable().getPageNumber() + 1, pageList.getPageable().getPageSize());

    return new ManualsFrontEnd(newPagination, frontDocList);
}
公共手册前端获取所有文档(整型页面、整型总计、文档搜索条件){
如果(page==null)page=0;
如果(总计=null)总计=100;
documentSearchCriteria.setIsActive(1);
if(documentSearchCriteria.getBrandId()==null){
documentSearchCriteria.setBrandId(2);//品牌=AI
}
Pageable Pageable=新页面请求(页面总数);
ExampleMatcher matcher=ExampleMatcher.matching()
.withStringMatcher(例如matcher.StringMatcher.CONTAINING)
.withIgnoreCase();
Example Example=Example.of(documentSearchCriteria,matcher);
pageList=documentsRepository.findAll(例如,可分页);
List documentList=pageList.getContent();
List frontDocList=DocumentMapper.mapViewDocsTOList(documentList);
PaginationFrontEnd newPagination=new PaginationFrontEnd(toIntExact(pageList.getTotalElements()),pageList.getPageable().getPageNumber()+1,pageList.getPageable().getPageSize());
返回新手册前端(新页码,前端文档列表);
}

您可以通过从
JpaSpecificationExecutor

然后您必须创建一个
规范
对象并实现其

谓词toPredicate(根根、CriteriaQuery查询、CriteriaBuilder cb)
方法以满足您的条件


然后您可以将此
规范
对象传递到
repository.findAll
方法。

您是否尝试过为文档类使用自定义规范?