Application.properties
Spring.datasource.primary.driver-class-name=oracle.jdbc.driver.oracledriverspring.datasource.primary.url=jdbc : oracle:thin:@10.110.110.110:1521:orclspring.datasource.primary.username= hbenergyspring.datasource.primary.password=hbenergyspring.datasource.secondary.url=jdbc:sqlserver:// 10.110.110.110;databasename=dbspring.datasource.secondary.username=spring.datasource.secondary.password= Spring.datasource.secondary.driver-class-name=com.microsoft.sqlserver.jdbc.sqlserverdriverspring.jpa.show-sql= truespring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.dialect= org.hibernate.dialect.oracledialectspring.jpa.hibernate.naming.physical-strategy= Org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Pom.xml
<Dependency> <groupId>Com.oracle</groupId> <Artifactid>Ojdbc6</Artifactid> <version>11.2.0.4.0-atlassian-hosted</version> </Dependency> <Dependency> <groupId>Com.microsoft.sqlserver</groupId> <Artifactid>Sqljdbc4</Artifactid> <version>4.0</version> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-jdbc</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-data-jpa</Artifactid> </Dependency>
Datasourceconfig.java Configuration of JDBC
@Configuration Public classdatasourceconfig {@Bean (name= "Primarydatasource") @Qualifier ("Primarydatasource") @ConfigurationProperties (prefix= "Spring.datasource.primary") PublicDataSource Primarydatasource () {returndatasourcebuilder.create (). build (); } @Bean (Name= "Secondarydatasource") @Qualifier ("Secondarydatasource") @Primary @ConfigurationProperties (prefix= "Spring.datasource.secondary") PublicDataSource Secondarydatasource () {returndatasourcebuilder.create (). build (); } @Bean (Name= "Primaryjdbctemplate") PublicJdbcTemplate primaryjdbctemplate (@Qualifier ("Primarydatasource") DataSource DataSource) {return NewJdbcTemplate (DataSource); } @Bean (Name= "Secondaryjdbctemplate") PublicJdbcTemplate secondaryjdbctemplate (@Qualifier ("Secondarydatasource") DataSource DataSource) {return NewJdbcTemplate (DataSource); }}
Primaryconfig.java Configuration of JPA
@Configuration @enabletransactionmanagement@enablejparepositories (entitymanagerfactoryref= "Entitymanagerfactoryprimary", Transactionmanagerref= "Transactionmanagerprimary", Basepackages= { "com.repository" }) Public classprimaryconfig {@Autowired @Qualifier ("Primarydatasource") PrivateDataSource Primarydatasource; @Primary @Bean (Name= "Entitymanagerprimary") PublicEntitymanager Entitymanager (Entitymanagerfactorybuilder builder) {returnentitymanagerfactoryprimary (builder). GetObject (). Createentitymanager (); } @Primary @Bean (name= "Entitymanagerfactoryprimary") PublicLocalcontainerentitymanagerfactorybean entitymanagerfactoryprimary (Entitymanagerfactorybuilder builder) {returnBuilder. DataSource (Primarydatasource). Properties (Getvendorproperties (PRIMARYDATASOURC e). Packages ("com.entity"). Persistenceunit ("Primarypersistenceunit"). build (); } @AutowiredPrivatejpaproperties jpaproperties; PrivateMap<string, string>getvendorproperties (DataSource DataSource) {returnjpaproperties.gethibernateproperties (DataSource); } @Primary @Bean (name= "Transactionmanagerprimary") PublicPlatformtransactionmanager transactionmanagerprimary (Entitymanagerfactorybuilder builder) {return NewJpatransactionmanager (Entitymanagerfactoryprimary (builder). GetObject ()); }}
Secondconfig.java to JPA Configuration
@Configuration @enabletransactionmanagement@enablejparepositories (entitymanagerfactoryref= "Entitymanagerfactorysecondary", Transactionmanagerref= "Transactionmanagersecondary", Basepackages= { "com.repository" })//Set Repository location Public classsecondaryconfig {@Autowired @Qualifier ("Secondarydatasource") PrivateDataSource Secondarydatasource; @Bean (Name= "Entitymanagersecondary") PublicEntitymanager Entitymanager (Entitymanagerfactorybuilder builder) {returnentitymanagerfactorysecondary (builder). GetObject (). Createentitymanager (); } @Bean (Name= "Entitymanagerfactorysecondary") PublicLocalcontainerentitymanagerfactorybean entitymanagerfactorysecondary (Entitymanagerfactorybuilder builder) { /c2>returnBuilder. DataSource (Secondarydatasource). Properties (Getvendorproperties (Secondarydatas Ource)). Packages ("com.entity")//set where entity classes are located. Persistenceunit ("Secondarypersistenceunit"). build (); } @AutowiredPrivatejpaproperties jpaproperties; PrivateMap<string, string>getvendorproperties (DataSource DataSource) {returnjpaproperties.gethibernateproperties (DataSource); } @Bean (Name= "Transactionmanagersecondary") Platformtransactionmanager transactionmanagersecondary (Entitymanagerfactorybuilder builder) {return NewJpatransactionmanager (Entitymanagerfactorysecondary (builder). GetObject ()); }}
JDBC Calls:
@Autowired @Qualifier ("Primaryjdbctemplate") private JdbcTemplate JdbcTemplate;
Multi-data source configuration in Springboot