简体   繁体   English

是否可以将 Query 对象作为 MongoRepository 方法的参数传递?

[英]Is it possible to pass Query object as parameter of MongoRepository method?

I create mongodb Query object in service layer, then I call repository method and pass Query to it.我在服务层创建 mongodb Query对象,然后调用存储库方法并将 Query 传递给它。 I don't want to use annotation @Query.我不想使用注释@Query。 So, repository method should look something like this:因此,存储库方法应如下所示:

List<Person> findByQuery(Query query, Pageable pageable);

or simply find(...) .或者干脆find(...) But when I build the project, there are errors in tests.但是当我构建项目时,测试中有错误。 It says:它说:

No property 'query' found for type 'Person'找不到类型“人”的属性“查询”

or或者

No property 'find' found for type 'Person'!没有找到类型“人”的属性“查找”! Did you mean ''id''?您指的是 ''id'' 吗?

Obviously, the name of method is incorrect.显然,方法的名称不正确。 How should I name the method to make Spring generate code?我应该如何命名让 Spring 生成代码的方法?

As Query is a persistence technology-specific abstraction (in this case MongoDB), it is supposed to be used within the repository.由于Query是一种特定于持久性技术的抽象(在本例中为 MongoDB),因此它应该存储库中使用。 The service layer shouldn't actually need to refer such a type.服务层实际上不需要引用这种类型。 This means that query methods need to be composed of domain types and either bound to a derived or declared query.这意味着查询方法需要由域类型组成,并且必须绑定到派生或声明的查询。

If you want to create the Query instance yourself, the way to go is a custom repository method implementation.如果您想自己创建Query实例,则可以使用自定义存储库方法实现。 How to declare them is described in the reference documentation . 参考文档中描述了如何声明它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM