将正则表达式数组传递给 spring base mongo @Query

Passing array of reg expressions to spring based mongo @Query

我正在使用 Spring 引导和 mongodb。我扩展了 PagingAndSortingRepository 存储库并添加了以下功能

@Query("{'title':{ $nin: [?0]}}")
List<Item> findItem(String[] exclude);

我希望能够向它传递一个正则表达式数组,例如 /dog/、/cat/、/horse/,以排除标题中可能包含其中一个的任何项目。

上面的函数不起作用,因为exclude被转换成了字符串。如何传递一组正则表达式才能执行上述操作?

您可以通过在您的控制器方法之一中使用 Querydsl 谓词来解决这个问题。

将类似这样的东西添加到您的控制器中:

@RequestMapping(value="/search/findByNameRegexNotIn", method = RequestMethod.GET)
@ResponseBody
public List<Item> findByNameRegexNotIn(@RequestParam(name = "name") List<String> names) {
    // build a query predicate
    BooleanBuilder predicate = new BooleanBuilder();  // comes from the Querydsl library
    for (String name : names) {
            predicate.and(QItem.item.name.contains(name).not()); // the QItem class is generated by Querydsl
    }

    List<Item> items = (List<Item>)repository.findAll(predicate);

    return items;
}

您当然可以添加 Pageable 参数和 return Page 而不是 List。


编辑:如果您将 Querydsl 用于此唯一目的,另一种解决方案是覆盖查询参数的默认绑定。

public interface ItemRepository extends CrudRepository<Item, String>,
    QueryDslPredicateExecutor<Item>, QuerydslBinderCustomizer<QItem> {

    @Override
    default public void customize(QuerydslBindings bindings, QItem item) {
        bindings.bind(item.name).all(
            (path, values) ->  path.matches(StringUtils.collectionToDelimitedString(values, "|")).not());
        // disable query on all parameters but the item name
        bindings.including(item.name);
        bindings.excludeUnlistedProperties(true);
    }
}

控制器方法:

@RequestMapping(value="/search/query", method = RequestMethod.GET)
@ResponseBody
public List<Item> queryItems(
        @QuerydslPredicate(root = Item.class) Predicate predicate) {
        List<Item> items = (List<Item>)repository.findAll(predicate);

        return items;
    }


编辑:如果您不想覆盖默认的 QuerydslBinderCustomizer#customize,您也可以实现自己的绑定器并在控制器方法中指定它。

public interface ItemRepository extends CrudRepository<Item, String>,
    QueryDslPredicateExecutor<Item> {
    ...
}

控制器方法:

@RequestMapping(value="/search/query", method = RequestMethod.GET)
@ResponseBody
public List<Item> queryItems(
        @QuerydslPredicate(root = Item.class, bindings = ItemBinder.class) Predicate predicate) {
        List<Item> items = (List<Item>)repository.findAll(predicate);

        return items;
    }

活页夹class:

class ItemBinder implements QuerydslBinderCustomizer<QItem> {

@Override
public void customize(QuerydslBindings bindings, QItem item) {
    bindings.bind(item.name).all(
            (path, values) ->  path.matches(StringUtils.collectionToDelimitedString(values, "|")).not()
    );
    bindings.including(item.name);
    bindings.excludeUnlistedProperties(true);
}

}


编辑:为了详尽无遗和那些不想听到 Querysl 的人。使用 Spring Data Mongodb Reference.

中提出的解决方案

定义自定义存储库接口:

interface ItemRepositoryCustom {

    public Page<Item> findByNameRegexIn(Collection<String> names, Pageable page);

}

定义自定义存储库实现(Impl 需要后缀!):

public class ItemRepositoryImpl implements ItemRepositoryCustom {

    @Autowired
    private MongoOperations operations;

    @Override
    public Page<Item> findByNameRegexNotIn(Collection<String> names, Pageable pageable) {
        String pattern = StringUtils.collectionToDelimitedString(names, "|");
        // this time we use org.springframework.data.mongodb.core.query.Query instead of Querydsl predicates
        Query query = Query.query(where("name").regex(pattern).not()).with(pageable);

        List<Item> items = operations.find(query, Item.class);
        Page<Item> page = new PageImpl<>(items, pageable, items.size());

        return page;
    }

}

现在只需扩展 ItemRepositoryCustom:

public interface ItemRepository extends MongoRepository<Item, String>, ItemRepositoryCustom {

...

}

大功告成!

您可以将 java.util.regex.Pattern[] 传递给该方法。这将在引擎盖下转换为正则表达式数组:

@Query("{'title':{ $nin: ?0}}")
List<Item> findItem(Pattern[] exclude);