简体   繁体   English

在一个 Spring Boot 中创建存储库,从另一个 Spring Boot 访问实体和数据库源

[英]Creating repository in one Spring Boot accessing entities and database source from another Spring Boot

I have created Spring Boot no.1 connecting to H2 databse (containing entities).我创建了连接到 H2 数据库(包含实体)的 Spring Boot no.1。 Now I have to create Spring Boot no.2 and Spring Boot no.3 and next Spring Boots which will connect to database Spring Boot no.1.现在我必须创建 Spring Boot no.2 和 Spring Boot no.3 以及下一个 Spring Boot,它们将连接到数据库 Spring Boot no.1。 How can I do that?我怎样才能做到这一点? Morever, is it possible to create repositories and services in Spring Boot no.2 and 3 ?此外,是否可以在 Spring Boot no.2 和 3 中创建存储库和服务? How would they create Spring Data - queries no knowing entities ?他们将如何创建 Spring Data - 查询不知道实体? Or should Spring Boot no.1 contain repositories and services ?或者 Spring Boot no.1 应该包含存储库和服务吗? In that case what's the architecture point of doing 2nd and 3rd Spring Boots to use no.在这种情况下,使用 no 进行第二个和第三个 Spring Boot 的架构点是什么? 1 ? 1 ?

Sping Boot no. Sping Boot 编号contains database connection H2 and entities.包含数据库连接 H2 和实体。

Hi please see this example, they did exactly what you asked嗨,请看这个例子,他们完全按照你的要求去做

https://www.baeldung.com/spring-boot-access-h2-database-multiple-apps https://www.baeldung.com/spring-boot-access-h2-database-multiple-apps

The key point here is overriding the default data source created by Spring Boot's auto-configuration feature by adding a few properties to the application.properties file:这里的关键点是通过向 application.properties 文件添加一些属性来覆盖由 Spring Boot 的自动配置功能创建的默认数据源:

spring.datasource.url=jdbc:h2:mem:mydb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create

note : I will be giving a detailed answer here soon in case the link is broken.注意:如果链接断开,我将很快在这里给出详细答案。

you can: in Spring Boot n1 you create the repository, that communicates with h2 db , and the entities associated, then you create a Rest controller where you set some enpoint that allows you to do the crud operation (insert,delete ecc), in other Spring Boot (n2,n3) you can access that endpoint using Feign rest client.您可以:在 Spring Boot n1 中创建与 h2 db 通信的存储库以及关联的实体,然后创建一个 Rest 控制器,在其中设置一些允许您执行 crud 操作(插入、删除 ecc)的端点,在其他 Spring Boot (n2,n3) 您可以使用Feign rest 客户端访问该端点。 so in n2 or n3 you can for example call the get enpoint for retrieves a specific entity of the db.因此,在 n2 或 n3 中,您可以例如调用 get 端点以检索数据库的特定实体。 of course you can customized your endpoint and add specific logic.当然,您可以自定义端点并添加特定逻辑。 this is one , at my opinion, solution.在我看来,这是一个解决方案。 enjoy.请享用。

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

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