本文介绍了Spring Data 的 MongoTemplate 和 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 than templates but the latter of course give you more fine-grained control over what to execute.

由于存储库编程模型可用于多个 Spring Data 模块,您将在 Spring Data MongoDB 的常规部分中找到更深入的文档 参考文档.

As the repository programming model is available for multiple Spring Data modules, you'll find more in-depth documentation for it in the general section of the Spring Data MongoDB reference docs.

TL;DR

我们通常推荐以下方法:

We generally recommend the following approach:

  1. 从存储库摘要开始,只需使用查询派生机制或手动定义的查询声明简单的查询即可.
  2. 对于更复杂的查询,将手动实现的方法添加到存储库(如此处所述).对于实现,请使用 MongoTemplate.

详情

对于您的示例,这看起来像这样:

For your example this would look something like this:

  1. 为您的自定义代码定义一个接口:

  1. 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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!