提问者:小点点

如何使用Spring data Rest对数据进行不区分大小写排序?


我喜欢在我的SortimentRespository中有一个findAll方法,它允许排序和分页,并且我可以在其中传递一个参数,该参数包含当前页面和页面的大小以及我想要排序的列的名称(不区分大小写)。

请求应类似于:

http://localhost:8081/x/rest/sorti ments?尺寸=20

或 http://localhost:8081/x/rest/sortiments?size=20

我在我的存储库中尝试了以下方法

    @Transactional
    public interface SortimentRepository extends JpaRepository<Sortiment,         RootKey>, SortimentRepositoryCustom {
        List<Sortiment> findAll();            
        // Query 1
        Page<Sortiment> findAllIgnoreCase(Pageable pageable);

        // Query 2
        Page<Sortiment> findAllByOrderByNameAscIgnoreCase(Pageable pageable);

        // Query 3
        @Query("select s from Sortiment s order by LOWER(s.name)")
        Page<Sortiment> findAllOrderByNameIgnoreCase(Pageable p);
    }

结果

查询1:运行org.springframework.beans.factory时出错。BeanCreationException:创建名为“sortmentRepository”的bean时出错:初始化方法调用失败;嵌套异常为org.springframework.data.mapping。PropertyReferenceException:找不到排序类型的属性!

查询2:org . spring framework . beans . factory . beancreationexception:创建名为“sortimentRepository”的bean时出错:调用init方法失败;嵌套异常为org . spring framework . data . mapping . propertyreferenceexception:找不到Sortiment类型的属性ignoreCase!

查询3:我什至在链接搜索下的hal浏览器中看不到此方法

有没有人知道,我怎样才能实现无组织的案件排序?如果我有全局性的东西,那就太好了,因为几个存储库需要不区分大小写的findAll方法。。。


共1个答案

匿名用户

首先,确保<code>Sortment

Spring数据遵循Repository方法名称的一些规则(在下面的链接中给出),您需要更改查询方法,如下所示:

在服务层中:

Pageable pageable = new PageRequest(pageNumber,pageSize,Sort.Direction.ASC,name);

存储库类:

@Transactional
public interface SortimentRepository extends JpaRepository<Sortiment,RootKey>, 
     SortimentRepositoryCustom { 
        Page<Sortiment> findAll(Pageable pageable);
  }

您可以参考此处以获取示例。