1 configuration Files
Wisely.primary.datasource.driverclassname=oracle.jdbc.oracledriverwisely.primary.datasource.url=jdbc\:oracle\ :thin\:@192. 168.1.103\:1521\: Xewisely.primary.datasource.username=giswisely.primary.datasource.password=gis Wisely.secondary.datasource.driverclassname=oracle.jdbc. oracledriverwisely.secondary.datasource.url=jdbc\:oracle\:thin\:@192. 168.1.103\:1522\: Xewisely.secondary.datasource.username=giswisely.secondary.datasource.password=gis
2 DataSource ConfigurationFirst Data source
@Configuration public class datasourceprimaryconfig { @Bean (name = " Primaryds ") @Qualifier (" primaryds ") @Primary @ConfigurationProperties (Prefix= "Wisely.primary.datasource") public datasource Primarydatasource () { return Datasourcebuilder.create (). build (); } /span>
second data source
@Configuration public class datasourcesecondaryconfig { @Bean (name = " Secondaryds ") @Qualifier (" secondaryds ") @ConfigurationProperties (Prefix= "Wisely.secondary.datasource") public DataSource secondarydatasource () { return datasourcebuilder.create (). build (); }}
3 entity Manager and transaction manager configurationFirst Data source
Import Java.util.Map;
Import Javax.persistence.EntityManager;
Import Javax.sql.DataSource;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Qualifier;
Import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
Import Org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.Primary;
Import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
Import Org.springframework.orm.jpa.JpaTransactionManager;
Import Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
Import Org.springframework.transaction.PlatformTransactionManager;
Import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration@EnableTransactionManagement@EnableJpaRepositories (entitymanagerfactoryref="Entitymanagerfactoryprimary", transactionmanagerref="Transactionmanagerprimary", basepackages= {"Com.wisely.demo.dao.one"})//Set the location of DAO (repo)PublicClassRepositoryprimaryconfig { @Autowired Private Jpaproperties jpaproperties; @Autowired@Qualifier ("Primaryds") Private DataSource primaryds; @Bean (name ="Entitymanagerprimary") @Primary Public Entitymanager Entitymanager (Entitymanagerfactorybuilder builder) { return Entitymanagerfactoryprimary (builder). GetObject (). Createentitymanager ();} @Bean (name ="Entitymanagerfactoryprimary") @Primary Public Localcontainerentitymanagerfactorybean entitymanagerfactoryprimary (Entitymanagerfactorybuilder builder) { Return builder. DataSource (Primaryds). Properties (Getvendorproperties (primaryds)). Packages ("Com.wisely.demo.domain.one")Set where entity classes are located Persistenceunit ( "Primarypersistenceunit") } private map<string, string> Getvendorproperties (DataSource DataSource) { return Jpaproperties.gethibernateproperties (DataSource); } @Bean (name = " Transactionmanagerprimary ") @Primary Platformtransactionmanager transactionmanagerprimary (Entitymanagerfactorybuilder builder) { span class= "keyword" >return new jpatransactionmanager (Entitymanagerfactoryprimary (builder). GetObject ()); } /span>
a second data source
@Configuration@EnableTransactionManagement@EnableJpaRepositories (entitymanagerfactoryref="Entitymanagerfactorysecondary", transactionmanagerref="Transactionmanagersecondary", basepackages= {"Com.wisely.demo.dao.two"})PublicClassRepositorysecondaryconfig { @Autowired Private Jpaproperties jpaproperties; @Autowired@Qualifier ("Secondaryds") Private DataSource secondaryds; @Bean (name ="Entitymanagersecondary") Public Entitymanager Entitymanager (Entitymanagerfactorybuilder builder) { return Entitymanagerfactorysecondary (builder). GetObject (). Createentitymanager ();} @Bean (name ="Entitymanagerfactorysecondary") Public Localcontainerentitymanagerfactorybean entitymanagerfactorysecondary (Entitymanagerfactorybuilder builder) { Return builder. DataSource (Secondaryds). Properties (Getvendorproperties (secondaryds)). Packages ( "Com.wisely.demo.domain.two") Persistenceunit ( "Secondarypersistenceunit") } private map<string, string> Getvendorproperties (DataSource DataSource) { return Jpaproperties.gethibernateproperties (DataSource); } @Bean (name = " Transactionmanagersecondary ") Platformtransactionmanager transactionmanagersecondary ( Entitymanagerfactorybuilder builder) { return new Jpatransactionmanager (Entitymanagerfactorysecondary (builder). GetObject ()); } /span>
4 Use
DAO (repo) from different databases can be injected into any other bean at this time.
@ControllerPublicclass testcontroller { @ Autowired SYSROLEREPO1 sysrolerepo1; @Autowired SysRoleRepo2 sysrolerepo2; @RequestMapping ( "/test") Public @ResponseBody String Test () { System.out.println (Lists.newarraylist (Sysrolerepo1.findall ()). Size ()); System.out.println (Lists.newarraylist (Sysrolerepo2.findall ()). Size () ); return "OK";
Spring boot,spring Data JPA Multi-data source support configuration