How to connect Relational and NoSQL databases with Spring Boot


This tutorial covers snippets and not complete code. It is assumed that you already have a downloaded and setup your Spring Boot + JDBC project using Spring Initializr in your IDE.

We extensively covered how to setup up Spring Boot + Maven + JDBC + MySQL database in this tutorial: https://code2care.org/tutorial/setting-up-spring-boot-3-maven-mysql-jdbc-example

Let's take a look at how various popular Databases can be configured using Spring Boot.


1. MySQL

applications.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mysqldb
spring.datasource.username=root
spring.datasource.password=msqyl-server-password
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
Example MySQL Server Configuratio with Spring Boot
Maven pom.xml dependency
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.26</version>
</dependency>

2. PostgreSQL

applications.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/postgredb
spring.datasource.username=user
spring.datasource.password=postgresql-server-password
Maven dependency pom.xml
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>43.0.1</version>
</dependency>

3. Oracle Server

applications.properties
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:oracledb
spring.datasource.username=oracle-db-user
spring.datasource.password=oracle-server-password
Maven dependency pom.xml
<dependency>
    <groupId>com.oracle.ojdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>9.3.0.0</version>
</dependency>

4. Microsoft SQL Server

applications.properties
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=sqldb
spring.datasource.username=sql-db-user
spring.datasource.password=sql-db-password
Maven dependency pom.xml
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>9.4.0.jre8</version>
</dependency>

We took a look at some Relational Databases configurations, now lets take a look at some NoSQL database configurations with Spring Boot.


1. DynamoDB

applications.properties
spring.data.mongodb.uri=mongodb://localhost:27017/dynamodb
Maven dependency pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>2.6.3</version>
</dependency>

2. Cassandra

applications.properties
spring.data.cassandra.contact-points=localhost
spring.data.cassandra.port=9042
spring.data.cassandra.keyspace-name=cassandradb
Maven dependency pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-cassandra</artifactId>
    <version>2.6.3</version>
</dependency>

3. Redis

applications.properties
spring.redis.host=localhost
spring.redis.port=6379
Maven dependency pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.6.3</version>
</dependency>

4. Apache HBase

applications.properties
spring.data.hbase.quorum=localhost
spring.data.hbase.zk-port=2181
Maven dependency pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-hadoop</artifactId>
    <version>2.6.3</version>
</dependency>

5. Couchbase

applications.properties
spring.couchbase.bootstrap-hosts=localhost
spring.couchbase.bucket.name=bucket-name
spring.couchbase.bucket.password=server-password
Maven dependency pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-couchbase</artifactId>
    <version>2.6.3</version>
</dependency>
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap