Configure Multiple Data Sources with Spring Boot and Jooq
· 4 min read
Configuring multiple data sources in Spring Boot and using JOOQ to generate code for each data source.
info
This article uses Gradle
as the project build tool.
In a Spring Boot project, if we have only one data source, springboot
will automatically create a DataSource
, and in this case, JOOQ will use the default data source to generate code. If we encounter a situation where multiple data sources need to be configured in the project, let’s see how to configure multiple data sources in springboot
and use JOOQ
to generate code for each data source.
Environment Information
springboot
:2.7.3
java
:11
gradle
:7.5
Configure Build.gradle
First, we need to configure the dependencies and the Jooq plugin (for code generation).
Apply the JOOQ
Plugin
plugins {
id 'nu.studer.jooq' version '7.1.1'
}
Add Dependencies
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jooq'
implementation 'org.jooq:jooq:3.17.4'
implementation 'org.jooq:jooq-codegen:3.17.3'
implementation 'com.sap.cloud.db.jdbc:ngdbc:2.13.9'
implementation 'org.realityforge.org.jetbrains.annotations:org.jetbrains.annotations:1.7.0'
implementation 'org.postgresql:postgresql:42.5.0'
implementation 'mysql:mysql-connector-java:8.0.30'
jooqGenerator 'com.sap.cloud.db.jdbc:ngdbc:2.13.9'
jooqGenerator 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
jooqGenerator 'org.postgresql:postgresql:42.5.0'
jooqGenerator 'mysql:mysql-connector-java:8.0.30'
}
tip
Configure database drivers as needed.