I have a problem with Spring Boot application. I want to connect a MongoDB database and a MySql database in my Spring boot application. I would to know if it is possible, in positive case How I can make this multiple connection. I had made a try based on an example with Mysql and Post without success. So I'm wondering if someone have an easy example to know the method. thanks
You really don't need to make additional config and property files because MongoDB has different property names than sql so all you will need is an application.properties file
application.properties
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/dbName?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=
spring.data.mongodb.uri=mongodb://localhost:27017
spring.data.mongodb.database=dbName
example models
MongoDB document
import org.springframework.data.mongodb.core.mapping.Document;
import javax.persistence.Id;
@Document("Gyros")
public class Gyros {
public Gyros(String description) {
this.description = description;
}
@Id
public String id;
public String description;
}
Mysql JPA entity
import javax.persistence.*;
@Entity
@Table(name = "Kebab")
public class Kebab {
public Kebab(String description) {
this.description = description;
}
public Kebab() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
public String description;
}
MongoDB repository
@Repository
public interface GyrosRepository extends MongoRepository<Gyros, String> {
}
Mysql Jpa repository
@Repository
public interface KebabRepository extends JpaRepository<Kebab, Integer> {
}
TestService
@org.springframework.stereotype.Service
public class Service {
private final GyrosRepository gyrosRepository;
private final KebabRepository kebabRepository;
@Autowired
public Service(GyrosRepository gyrosRepository, KebabRepository kebabRepository) {
this.gyrosRepository = gyrosRepository;
this.kebabRepository = kebabRepository;
}
@PostConstruct
void test() {
this.gyrosRepository.insert(new Gyros("ham ham"));
this.kebabRepository.saveAndFlush(new Kebab("yum yum"));
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>stack</artifactId>
<version>1.0-SNAPSHOT</version>
<name>stackoverflow</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With