本文介绍了Spring JPA PostgreSQL + MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Spring示例开始,使用REST访问MongoDB数据( https://spring.io/guides/gs/accessing-mongodb-data-rest/)我想集成PostgreSQL数据源并将其链接到MongoDB存储库.
MongoRepository切换到JpaRepository并相应地更改application.properties文件,我已经能够从MongoDB传递到PostgreSQL,反之亦然,但是基本上只有一个活动数据源.

使用MongoDB时 application.properties

Starting from the Spring example Accessing MongoDB Data with REST (https://spring.io/guides/gs/accessing-mongodb-data-rest/) I'd like to integrate a PostgreSQL data source and link it to the MongoDB repository.
By switching from MongoRepository to JpaRepository and accordingly changing the application.properties file I've been able to pass from MongoDB to PostgreSQL and viceversa, but basically having only one active data source at time.

application.properties when using MongoDB

spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://localhost/
spring.data.mongodb.database=myMongoDB_DB
spring.data.mongodb.repositories.enabled=true


使用PostgreSQL时 application.properties


application.properties when using PostgreSQL

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/myPostgreSQL_DB
spring.datasource.username=me
spring.datasource.password=mySuperSecretPassword

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

有没有一种配置Spring的方法(仅使用 Annotation 方式)将两个数据源链接到同一个存储库,这样当我通过HTTP访问我的REST Web服务时,MongoDB和PostgreSQL都是是否以完全相同的方式进行更改?

我在Google周围搜索,发现了一些有关Spring跨商店支持的信息( http://docs.spring.io/spring-data/mongodb/docs/1.5.5.RELEASE/reference/html /mongo.cross.store.html ),但它使用xml进行应用程序配置和AspectJ,是否有更简单的方法来完成此操作?

Is there a way to configure Spring (with an Annotation-only way) to link two data sources to the same Repository so that when I access my REST web service via HTTP both MongoDB and PostgreSQL are changed in exactly the same way?

I googled around and found something about Spring cross-store support (http://docs.spring.io/spring-data/mongodb/docs/1.5.5.RELEASE/reference/html/mongo.cross.store.html) but it uses xml for the application configuration and AspectJ, is there a simpler way to accomplish this?

推荐答案

在本章中,您可以找到答案-(Spring-boot手册-使用Spring Data JPA和Mongo存储库)

In this chapter you can find an answer - (Spring-boot manual - Use Spring Data JPA and Mongo repositories)

这篇关于Spring JPA PostgreSQL + MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:35