使用 spring 数据不区分大小写排序 [英] case insensitive sort using spring data

查看:27
本文介绍了使用 spring 数据不区分大小写排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Spring-data Pageable 进行不区分大小写的排序?

how can I do case insensitive sorting using Spring-data Pageable?

我的存储库中有这个方法

I have this method in my Repository

public interface ItemRepository extends QueryDslPredicateExecutor<Item>{
    @Query("SELECT o FROM Item o WHERE o.status = ?1")
    Page<Item> findByStatus(Item.Status status, Pageable pageable);
}

我希望能够调用它:

itemRepository.findByStatus(Status.completed, new PageRequest(0, 10, Direction.ASC, "lower(name)")

注意属性字符串中的 lower 函数.这不起作用,因为 Spring-data 期望那里有一个属性.这将被翻译成类似的东西:

Note the lower function in the property string. That doesn't work as Spring-data expects a property there. That will get translated to something like:

SELECT o FROM Item o WHERE o.status = ?1 ORDER BY o.lower(name)

这当然不起作用,因为对象上没有 'lower' 属性.

which of course won't work as there is no 'lower' property on the object.

有没有办法让这个工作?

Is there a way to make this work?

推荐答案

Sort.Order.ignoreCase() 大约 8 个月前被引入 spring-data-jpa,看这里:

Sort.Order.ignoreCase() was introduce around 8 months ago into spring-data-jpa, look here:

https://jira.springsource.org/browse/DATAJPA-296

https://github.com/kraymondksc/spring-data-jpa/commit/c3adce99bd6465c3adce99bd376e一个>

一旦你有一个合适的 spring-data-jpa 版本(我认为从 1.4 M1 开始,我有 1.4.1)你可以写这样的东西:

Once you have an appropriate version of spring-data-jpa (I think since 1.4 M1, I have 1.4.1) you can write something like this:

Sort.Order order = new Sort.Order(Sort.Direction.ASC, "name").ignoreCase();
itemRepository.findByStatus(Status.completed, new PageRequest(0, 10, new Sort(order));

这篇关于使用 spring 数据不区分大小写排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆