Spring Data的MongoTemplate和MongoRepository有什么区别? [英] What's the difference between Spring Data's MongoTemplate and MongoRepository?
问题描述
我需要编写一个应用程序,使用spring-data和mongodb可以执行复杂的查询。我一直在使用MongoRepository,但在复杂的查询中苦苦寻找示例或实际理解语法。
I need to write an application with which I can do complex queries using spring-data and mongodb. I have been starting by using the MongoRepository but struggled with complex queries to find examples or to actually understand the Syntax.
我在谈论这样的查询:
@Repository
public interface UserRepositoryInterface extends MongoRepository<User, String> {
List<User> findByEmailOrLastName(String email, String lastName);
}
或使用基于JSON的查询,我尝试通过反复试验,因为我不要正确的语法。即使在阅读了mongodb文档(由于语法错误而导致的非工作示例)。
or the use of JSON based queries which I tried by trial and error because I don't get the syntax right. Even after reading the mongodb documentation (non-working example due to wrong syntax).
@Repository
public interface UserRepositoryInterface extends MongoRepository<User, String> {
@Query("'$or':[{'firstName':{'$regex':?0,'$options':'i'}},{'lastName':{'$regex':?0,'$options':'i'}}]")
List<User> findByEmailOrFirstnameOrLastnameLike(String searchText);
}
阅读完所有文档后,似乎 mongoTemplate
远远好于 MongoRepository
。我指的是以下文档:
After reading through all the documentation it seems that mongoTemplate
is far better documented then MongoRepository
. I'm referring to following documentation:
http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/
你能告诉我什么更方便,更强大吗? mongoTemplate
或 MongoRepository
?两者是否相同成熟,或者其中一个缺少更多功能而另一个?
Can you tell me what is more convenient and powerful to use? mongoTemplate
or MongoRepository
? Are both same mature or does one of them lack more features then the other?
推荐答案
方便和功能强大在某种程度上是矛盾的目标。存储库比模板更方便,但后者当然可以让您对执行的内容进行更细粒度的控制。
"Convenient" and "powerful to use" are contradicting goals to some degree. Repositories are by far more convenient that the templates but the latter of course give you more fine-grained control over what to execute.
由于存储库编程模型可用于多个Spring数据模块你可以在Spring Data MongoDB的常规部分找到更深入的文档参考文档。
As the repository programming model is available for multiple Spring Data modules you find more in-depth documentation of it in the general section of the Spring Data MongoDB reference docs.
TL; DR
我们通常建议采用以下方法:
We generally recommend the following approach:
- 从存储库摘要开始,只使用查询派生机制或手动定义的查询声明简单查询。
- 对于更复杂的查询,请将手动实现的方法添加到存储库(如此处所述)。对于实现,使用
MongoTemplate
。
- Start with the repository abstract and just declare simple queries using the query derivation mechanism or manually defined queries.
- For more complex queries, add manually implemented methods to the repository (as documented here). For the implementation use
MongoTemplate
.
详细信息
对于你的例子,这看起来像这样:
For your example this would look something like this:
-
为您的自定义代码定义一个界面:
Define an interface for your custom code:
interface CustomUserRepository {
List<User> yourCustomMethod();
}
为此类添加实现并遵循命名约定确定我们可以找到该类。
Add an implementation for this class and follow the naming convention to make sure we can find the class.
class UserRepositoryImpl implements CustomUserRepository {
private final MongoOperations operations;
@Autowired
public UserRepositoryImpl(MongoOperations operations) {
Assert.notNull(operations, "MongoOperations must not be null!");
this.operations = operations;
}
public List<User> yourCustomMethod() {
// custom implementation here
}
}
现在让您的基础存储库接口扩展自定义存储库接口,基础架构将自动使用您的自定义实现:
Now let your base repository interface extend the custom one and the infrastructure will automatically use your custom implementation:
interface UserRepository extends CrudRepository<User, Long>, CustomUserRepository {
}
这种方式你基本上可以做出选择:易于申报的所有东西都进入 UserRepository
,手动更好实现的一切都进入 CustomUserRepository
。 此处记录了自定义选项。
This way you essentially get the choice: everything that just easy to declare goes into UserRepository
, everything that's better implemented manually goes into CustomUserRepository
. The customization options are documented here.
这篇关于Spring Data的MongoTemplate和MongoRepository有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!