【问题标题】:case insensitive sort using spring data使用弹簧数据进行不区分大小写的排序
【发布时间】:2013-04-03 02:02:03
【问题描述】:

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

我的存储库中有这个方法

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 期望那里有一个属性。这将被翻译成类似:

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

这当然是行不通的,因为对象上没有 'lower' 属性。

有没有办法让它工作?

【问题讨论】:

    标签: spring-data-jpa


    【解决方案1】:

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

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

    https://github.com/kraymondksc/spring-data-jpa/commit/c3adce0bd36799d3754a5f4c61aee64982abd7e0

    一旦你有了合适的 spring-data-jpa 版本(我想从 1.4 M1 开始,我就有 1.4.1),你可以这样写:

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

    【讨论】:

    • 是的,我最终就是这样做的。当时该选项存在错误。那是我报告说要修复:)
    • new Sort(order) 构造函数被弃用,使用这个new PageRequest(0, 10, Sort.by(order))
    • 是否有可能在排序前修剪空格?我需要按自动添加的顺序修剪和忽略大小写。
    【解决方案2】:

    以下是我用来排序的代码:

    db.getCollection('employee').aggregate([
      {
        $project: {
             fieldToSortWith: { $toLower: "$lastName" },
             subject: 1
                 }
     },
    {
        $sort: { fieldToSortWith: 1 },
    }
    ])
    

    这就是你在 Spring Data 中的做法

    DBCollection coll = mongoTemplate.getCollection("employee");
           List<DBObject> pipe = new ArrayList<>();
            DBObject prjflds = new BasicDBObject();
            prjflds.put("id", 1);
            prjflds.put("firstName", 2);
            prjflds.put("lastName", new BasicDBObject("$toLower", "$lastName"));
            DBObject project = new BasicDBObject();
            project.put("$project", prjflds);
            pipe.add(project);
            DBObject sort = new BasicDBObject();
            sort.put("$sort", new BasicDBObject("lastName", 1));
            pipe.add(sort);
            BasicDBObject skip = new BasicDBObject("$skip", searchCriteria.getCurrentPage() * searchCriteria.getPageSize());
            BasicDBObject limit = new BasicDBObject("$limit", searchCriteria.getPageSize());
            pipe.add(skip);
            pipe.add(limit);
            BasicDBObject employeeRoleFilter = new BasicDBObject("$match", new BasicDBObject("employeeRoles", new BasicDBObject("$in", searchCriteria.getEmployeeRolesNames())));
            pipe.add(employeeRoleFilter);
            BasicDBObject employeeStatusFilter = new BasicDBObject("$match", new BasicDBObject("active", new BasicDBObject("$eq", Boolean.valueOf(searchCriteria.getEmployeeStatus()))));
            pipe.add(employeeStatusFilter);
            searchCriteria.isEmployeeNameSearchIndicator());
            pipe.add(searchTextFilter);
            coll.aggregate(pipe).results();
    

    之后,您将获得需要在您的域中转换的本机 BasicDBObjects。如果您需要任何帮助,请告知此代码。

    【讨论】:

    • spring-data 怎么样?
    • @Stef 是否希望通过排序忽略大小写和过滤器进行分页?
    • 这就是你在春天的做法
    • 你的意思是如果你使用 Mongo,这不是我的情况。但可能对其他人有用,谢谢。
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多