• Register

Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

0 votes
13 views

If i want to use two databases like mysql and mongodb then Spring boot mysql service response consumption by another mongodb datasource in the same application.

in Spring Boot by

1 Answer

0 votes

Spring boot mysql service response consumption by another mongodb datasource in the same application.

I am also facing the same problem. I want to connect my spring boot application with two different databases (mysql and mongodb).

See the following solution with code by using the both JPA (spring-boot-starter-data-jpa) as well as mongodb (spring-boot-starter-data-mongodb). My project is working  fine.

application.properties file:

# MONGODB (MongoProperties)
spring.data.mongodb.uri=mongodb://localhost:27017/project_db_name

# mysql properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/project_db_name
spring.datasource.username=db_user_name
spring.datasource.password=db_user_password
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

pom.xml file (dependencies):-

<dependencies>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>  
<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

Repositories files:-

MONGO repository file (MRepositories.java):-
import org.springframework.data.mongodb.repository.MongoRepository;
public interface MRepositories extends MongoRepository<Employee, String>{
}

JPA repository (MysqlRepositories.java):-

@Repository public interface MysqlRepositories extends JpaRepository<Employee,Long> {}

by (4.4k points)
...