英文:
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();
}
}
}
我创建了一个扩展了MongoRepository
的AppUserRepository
接口:
@Repository
public interface AppUserRepository extends MongoRepository<AppUser, String> {}
我有一个名为WebController
的类与该接口交互。这个类中的AppUserRepository
字段被@Autowired
注解。这一切似乎都能工作,但是我对如何配置以及如何继续编写集成测试有一些问题:
- 如何配置已创建的
AppUserRepository
?我可以在特定端口上运行它吗? - 为什么
Autowiring
起作用,因为我并没有像在我的应用程序中的其他Autowired
的Bean一样在AppConfig
中创建AppUserRepository
Bean。 - 如果我要创建一个Bean,我不也必须实现类并返回实例化对象吗?我开始这样做,但我不得不实现所有
MongoRepository
类的方法,我不确定这是否正确。 - 如何编写与
AppUserRepository
配合的集成测试?我需要一个AppUserRepository
用于请求交互,但当服务运行时,我不希望它是与实时应用程序数据库相同的数据库。我可以在集成测试类中@Autowire
数据库,然后在集成测试运行后关闭数据库吗?如果我要继续这样做,我认为我还需要完成上面第3点的内容。
在此先感谢您的帮助,我已经尝试阅读了一些文档,但我觉得我可能缺少一些关键知识,导致一切都变得非常令人不知所措和混淆。
谢谢!
英文:
So I have an AppUser class:
@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();
}
}
}
I have created an AppUserRepository
interface that extends MongoRepository
:
@Repository
public interface AppUserRepository extends MongoRepository<AppUser, String> {}
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:
- How do I configure this
AppUserRepository
that has been created? Can I run it on a specific port etc? - Why has the
Autowiring
worked as I have not created thisAppUserRepository
Bean in anAppConfig
like I have other Beans that areAutowired
in my application. - 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. - How do I write integration tests with an
AppUserRepository
? I need anAppUserRepository
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论