Created
August 4, 2019 14:38
-
-
Save aljacinto/ee4ef655886c8cbfa1f7c6df755df667 to your computer and use it in GitHub Desktop.
Springboot with multiple datasources (jdbc example but can easily apply to jpa)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| datasource.library.jdbcUrl=jdbc:h2:mem:default | |
| datasource.library.username=sa | |
| datasource.library.password= | |
| datasource.library.driverClassName=org.h2.Driver | |
| datasource.online.jdbcUrl=jdbc:h2:mem:default | |
| datasource.online.username=sa | |
| datasource.online.password= | |
| datasource.online.driverClassName=org.h2.Driver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.aljacinto.multipledatasources; | |
| import org.springframework.beans.factory.annotation.Qualifier; | |
| import org.springframework.boot.context.properties.ConfigurationProperties; | |
| import org.springframework.boot.jdbc.DataSourceBuilder; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.jdbc.core.JdbcTemplate; | |
| import javax.sql.DataSource; | |
| public class DatasourceConfiguration { | |
| @Bean("library-ds") | |
| @ConfigurationProperties(prefix = "datasource.library") | |
| public DataSource vendorDataSource() { | |
| // you can put decrypt here for the password if you don't want to expose plain password | |
| return DataSourceBuilder.create().build(); | |
| } | |
| @Bean("online-ds") | |
| @ConfigurationProperties(prefix = "datasource.online") | |
| public DataSource catalogDataSource() { | |
| return DataSourceBuilder.create().build(); | |
| } | |
| @Bean("library") | |
| public JdbcTemplate vendorJdbcTemplate(@Qualifier("library-ds") DataSource dataSource) { | |
| return new JdbcTemplate(dataSource); | |
| } | |
| @Bean("online") | |
| public JdbcTemplate catalogJdbcTemplate(@Qualifier("online-ds") DataSource dataSource) { | |
| return new JdbcTemplate(dataSource); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.aljacinto.multipledatasources.repositories; | |
| import org.aljacinto.multipledatasources.entities.LibraryBook; | |
| import org.springframework.beans.factory.annotation.Qualifier; | |
| import org.springframework.jdbc.core.BeanPropertyRowMapper; | |
| import org.springframework.jdbc.core.JdbcTemplate; | |
| import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; | |
| import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | |
| import org.springframework.jdbc.core.namedparam.SqlParameterSource; | |
| import org.springframework.stereotype.Repository; | |
| @Repository | |
| public class LibraryRepository { | |
| private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; | |
| public LibraryRepository(@Qualifier("library") JdbcTemplate jdbcTemplate) { | |
| this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); | |
| } | |
| public void query(String isbn) { | |
| SqlParameterSource parameters = new BeanPropertySqlParameterSource(isbn); | |
| namedParameterJdbcTemplate.queryForObject("SELECT * FROM LIBRARY WHERE BOOKS ISBN :isbn", | |
| parameters, new BeanPropertyRowMapper<>(LibraryBook.class)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment