Here in this article i will explain how to access data from multiple datasources, for example accessing data from two different databases like student database and courses database.
To develop this application we are going to use @ConfigurationProperties, @Bean,@Qualifier, @Autowired annotations mainly.
Database connection details are including in application.properties file( which will be in resource folder of application ).
by default
spring.datasource is the prefix for database connection string values like database url, driver class, username and password of db.
so by using spring.datasource we can specify one database connection details, and then we can use @ConfigurationProperties to specify custom prefix for second datasource. here iam trying to show you the content to add into application.properties file with two database connection details.
Here we are going to create a @Configuration class which includes Datasources configuration and JDBCTemplate configuration.
First we are going to inject a bean for DataSource with @Bean annotation at method lever by specifying a name to bean.
In this example we are going to use Streams to concatenate two results from two different data sources.
we are going to use concat() method to concatenate lists and the terminal method collect() to convert Stream into list.
This article is based on JDBCTemplate
To develop this application we are going to use @ConfigurationProperties, @Bean,@Qualifier, @Autowired annotations mainly.
Database connection details are including in application.properties file( which will be in resource folder of application ).
by default
spring.datasource is the prefix for database connection string values like database url, driver class, username and password of db.
so by using spring.datasource we can specify one database connection details, and then we can use @ConfigurationProperties to specify custom prefix for second datasource. here iam trying to show you the content to add into application.properties file with two database connection details.
# These are connection details for student database spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/student spring.datasource.userName=admin spring.datasource.password=123456 spring.datasource.driverClassName=com.mysql.jdbc.Driver # Connection details for courses database spring.coursesDb.jdbcUrl= hjbc:mysql://localhost:3306/courses spring.coursesDb.userName=adminc spring.coursesDb.password=123456 spring.coursesDb.driverClassName=com.mysql.jdbc.Driver
Here we are going to create a @Configuration class which includes Datasources configuration and JDBCTemplate configuration.
First we are going to inject a bean for DataSource with @Bean annotation at method lever by specifying a name to bean.
@Configuration public class DbConfig{ // we are creating a Bean here for DataSource for first database that is Student @Bean(name="studentDataSource") @ConfigurationProperties(prefix="spring.datasource") public DataSource studentDataSource(){ return new DataSourceBuilder().create().build(); } // creating/configuring JDBCTemplate @Bean("studentDbTemplate") public JDBCTemplate getStudentJDBCTemplate(@Qualifier("studentDataSource") DataSource studentDataSource){ return new JDBCTEmplate(studentDataSource); } // creating bean for second datasource with the prefix spring.coursesDb @Bean("courseDataSource") @ConfigurationProperties(prefix="spring.coursesDb") public DataSource courseDataSource(){ return new DataSourceBuilder().create.build(); } // configuring JDBCTemplate for courseDataSource @Bean("coursesDBTemplate") public JDBCTemplate getCoursesJDBCTemplate(@Qualifier("courseDataSource") DataSource courseDataSource) { return new JDBCTemplate(courseDataSource); } }
In this example we are going to use Streams to concatenate two results from two different data sources.
we are going to use concat() method to concatenate lists and the terminal method collect() to convert Stream into list.
Post a Comment
Post a Comment