Sure, here’s the translation: Java Spring MongoDB Repository Interface

huangapple go评论92阅读模式
英文:

Java Spring MongoDB Repository Interface

问题

我有一个名为AppUser的类:

    @Data
    @Builder
    @Document(collection = "app_users")
    @Component
    @AllArgsConstructor
    @NoArgsConstructor
    @Import(AppConfig.class)
    public class AppUser {
    
      @Id
      @NotBlank(message = ErrorConstants.ANDROID_USER_ACCOUNT_MANAGER_ID_IS_NULL)
      private String androidUserAccountManagerId;
    
      @NotBlank(message = ErrorConstants.NULL_NAME)
      private String name;
    
      private Friend bestFriend;
    
      @Setter(AccessLevel.NONE)
      private FriendList friendList;
    
      private boolean manualBestFriendOverride;
    
      public Optional<Friend> getFriend(String friendName) {
        return friendList.getFriend(friendName);
      }
    
      public void calculateBestFriend() {
        if (!manualBestFriendOverride) {
          bestFriend = friendList.calculateAndReturnBestFriend();
        }
      }
    }

我创建了一个扩展了MongoRepositoryAppUserRepository接口:

    @Repository
    public interface AppUserRepository extends MongoRepository<AppUser, String> {}

我有一个名为WebController的类与该接口交互。这个类中的AppUserRepository字段被@Autowired注解。这一切似乎都能工作,但是我对如何配置以及如何继续编写集成测试有一些问题:

  1. 如何配置已创建的AppUserRepository?我可以在特定端口上运行它吗?
  2. 为什么Autowiring起作用,因为我并没有像在我的应用程序中的其他Autowired的Bean一样在AppConfig中创建AppUserRepository Bean。
  3. 如果我要创建一个Bean,我不也必须实现类并返回实例化对象吗?我开始这样做,但我不得不实现所有MongoRepository类的方法,我不确定这是否正确。
  4. 如何编写与AppUserRepository配合的集成测试?我需要一个AppUserRepository用于请求交互,但当服务运行时,我不希望它是与实时应用程序数据库相同的数据库。我可以在集成测试类中@Autowire数据库,然后在集成测试运行后关闭数据库吗?如果我要继续这样做,我认为我还需要完成上面第3点的内容。

在此先感谢您的帮助,我已经尝试阅读了一些文档,但我觉得我可能缺少一些关键知识,导致一切都变得非常令人不知所措和混淆。

谢谢!

英文:

So I have an AppUser class:

    @Data
    @Builder
    @Document(collection = &quot;app_users&quot;)
    @Component
    @AllArgsConstructor
    @NoArgsConstructor
    @Import(AppConfig.class)
    public class AppUser {
    
      @Id
      @NotBlank(message = ErrorConstants.ANDROID_USER_ACCOUNT_MANAGER_ID_IS_NULL)
      private String androidUserAccountManagerId;
    
      @NotBlank(message = ErrorConstants.NULL_NAME)
      private String name;
    
      private Friend bestFriend;
    
      @Setter(AccessLevel.NONE)
      private FriendList friendList;
    
      private boolean manualBestFriendOverride;
    
      public Optional&lt;Friend&gt; getFriend(String friendName) {
        return friendList.getFriend(friendName);
      }
    
      public void calculateBestFriend() {
        if (!manualBestFriendOverride) {
          bestFriend = friendList.calculateAndReturnBestFriend();
        }
      }
    }

I have created an AppUserRepository interface that extends MongoRepository:

    @Repository
    public interface AppUserRepository extends MongoRepository&lt;AppUser, String&gt; {}

I have a WebController class that interacts with the interface. The AppUserRepository field in this class is @Autowired. This all seems to work but I have a few questions regarding how, and how I go forward and write integration tests for this:

  1. How do I configure this AppUserRepository that has been created? Can I run it on a specific port etc?
  2. Why has the Autowiring worked as I have not created this AppUserRepository Bean in an AppConfig like I have other Beans that are Autowired in my application.
  3. If I was to create a Bean, wouldn't I also have to implement the class and return the instantiation? I started doing this but I had to implement all of the MongoRepository classes methods which I wasn't sure seemed quite right.
  4. How do I write integration tests with an AppUserRepository? I need an AppUserRepository for my requests to interact with, but I do not want this to be the same DB as the real-time application DB when the service is up and running. Can I @Autowire the database into the integration test class and then close the DB after the integration tests run? If this is how I go forward, I think I then need to do point 3 above.

Thanks for your help in advance, I have tried reading some documentation but I think I am missing some key knowledge that means it is all quite overwhelming and confusing.

Thanks!

答案1

得分: 1

这实际上是一个相当庞大的故事。这个主题被称为Spring Data JPA,Hibernate。你可能想要对此进行一些研究,观看一些教程等等。

简而言之,这个MongoRepository只是为你提供了许多可以使用的方法。你也可以定义自己的方法,添加查询等等。

你的起点:https://www.baeldung.com/spring-boot-hibernate

https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa

https://www.baeldung.com/spring-data-jpa-query

当然,你可以通过application.properties文件设置端口号(以及其他一些属性)。这是一份最常见属性的列表,你可以在其中找到mongodb的属性:
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

现在关于bean。你实际上是使用@Repository注解创建了一个bean。因此,Spring上下文会在应用程序启动时加载它。你可以自动装配它。

英文:

That's actually quite a big story to tell. This topic is called Spring Data JPA, Hibernate. You might wanna do a research on that, and watch some tutorials and so on.

Briefly, that MongoRepository just gives you a lot of methods which you can use. You can also define your own methods, add queries and etc.

Your starting points: https://www.baeldung.com/spring-boot-hibernate

https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa

https://www.baeldung.com/spring-data-jpa-query

Of course you can set a port number (and some other properties) via application.properties file. This is a list of most common properties, you can find properties for mongodb on it:
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

Now about bean. You basically created one with @Repository annotation actually. So Spring Context loads it on the start of application. You can autowire it.

huangapple
  • 本文由 发表于 2020年4月3日 22:38:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61014296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定