Multiple Database Configuration In Spring Boot Application

To access multiple database in application sprint boot makes easy way to do this. In this article, You will get idea to configure it, here we are going to configure two mysql databases for spring boot application.

Steps: -

1.Add multiple datasource configuration in to your application.properties.
2.Create Configuration class with providers @Bean i.e connection to database.
3. Create Base service to select datasource i.e Generate query.
4. Call Base from DAO to get result from two database

1.Add multiple datasource configuration in to your application.properties

Create two database names as employee and student and add its credintial details in same file.

spring.datasource.employee.jdbc-url=jdbc:mysql://localhost/employee?autoReconnect=true&useSSL=false
spring.datasource.employee.username=empuser
spring.datasource.employee.password=emppwd
spring.datasource.employee.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.student.jdbc-url=jdbc:mysql://localhost/student?autoReconnect=true&useSSL=false
spring.datasource.student.username=studuser
spring.datasource.student.password=studpassword
spring.datasource.student.driver-class-name=com.mysql.jdbc.Driver
`

2.Create Configuration class with providers @Bean i.e connection to database.

mysqlConfiguration
@Configuration
public class MySqlConfiguration {
@Bean (name = “employeeDatasource”)
@ConfigurationProperties(prefix = “spring.datasource.employee”)
@Primary
public DataSource employeeDatasource()
{
return DataSourceBuilder.create().build();
}
@Bean(name = “employeeTemplate”)
@Primary
public JdbcTemplate employeeTemplate(@Qualifier(“employeeDatasource”) DataSource emp) {
return new JdbcTemplate(emp);
}
@Bean (name = “studentDatasource”)
@ConfigurationProperties(prefix = “spring.datasource.student”)
public DataSource studentDatasource()
{
return DataSourceBuilder.create().build();
}


@Bean(name = “studentTemplate”)
public JdbcTemplate studentTemplate(@Qualifier(“studentDatasource”) DataSource stud) {
return new JdbcTemplate(stud);
}
}

3.Create Base service to select datasource i.e Generate query.

base-service
//@Service
public class BaseService {
@Autowired
@Qualifier(“employeeTemplate”)
private JdbcTemplate employeeTemplate;

//get templates
@Autowired
@Qualifier(“studentTemplate”)
private JdbcTemplate studentTemplate;

public JdbcTemplate getTemplate(String ) {
if(.equals(“employee”))
return employeeTemplate;
else if(.equals(“student”))
return studentTemplate;
else
return null;
};

protected List<Map<String, Object>> parseQueryResult(String query,String databaseName,String ) {
List<Map<String, Object>> parsedList = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> queryResult = getTemplate().queryForList(query,databaseName);
return queryResult;
}
}

4.Call Base from DAO to get result from two database

public LinkedHashMap<String, Object> getTableOfdb(String databaseName,String ) { 

List<Map<String, Object>> employeeList = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> studentList = new ArrayList<Map<String, Object>>();
LinkedHashMap<String, Object> responseObjectsMap = new LinkedHashMap<String, Object>();

String queryEmp = “SELECT * FROM EMPLOYEE”;
EmpList = parseQueryResult(queryEmp,databaseName,);
String queryStud = “SELECT * FROM STUDENT”;
StudList = parseQueryResult(queryStud,databaseName,);

responseObjectsMap.put(“employee”, EmpList);
responseObjectsMap.put(“students”, StudList);

return responseObjectsMap;
}

Thank you for reading and feel free add comments👋🏽
Hope this helps you.✌️

Love Programming and Writing, Software Engineer, Pune