Spring Boot @Query:按命名参数排序

时间:2019-01-21 07:39:41

标签: java spring spring-boot jpql

我希望我的REST-Api端点可以按不同的模型属性进行排序。我在Google上搜索了很多solutions,但都不符合我的要求。

PagingAndSortingRepository界面中,我喜欢拥有以下内容:

    @Query(value = "SELECT v FROM vwFact v WHERE v.applicationId like %:applicationId% " +
            "and (v.mdName01 like :mdName% or v.mdName02 like :mdName% or v.mdName03 like :mdName% or v.mdName04 like :mdName%) " +
            "ORDER BY :sortParam :sortDir"
    )
    Page<vwFact> findAllByApplicationIdAndMdName(
            @Param(value = "applicationId") String applicationId,
            @Param(value = "mdName") String mdName,
            @Param(value = "sortParam") String sortParam,
            @Param(value = "sortDir") String sortDir,
            Pageable pageable
    );

是否有实现的机会,还是我必须以另一种方式做到这一点。您有什么建议?

1 个答案:

答案 0 :(得分:0)

Pageable具有一个选项,用于指定结果的排序方式(方向和参数)。

select 
  case when Occupation='Doctor' then concat(concat(concat(Name,'('),'D'),')') 
       when Occupation='Actor' then concat(concat(concat(Name,'('),'A'),')') 
       when Occupation='Professor' then concat(concat(concat(Name,'('),'P'),')')
       when Occupation='Singer' then concat(concat(concat(Name,'('),'S'),')')end as name
from OCCUPATIONS
order by name asc;

select concat(concat(concat(concat('There are a total of ',count(Occupation)),' '),lower(Occupation)),'s.')
from OCCUPATIONS
group by Occupation
order by Count(Occupation) asc,lower(Occupation);