Home > Back-end >  How to use two data sources at the same time?
How to use two data sources at the same time?

Time:09-16

I want to use two data sources at the same time, some used to add and delete the data source, others use the second data source, I hope com. LDW. Mapper. Under the secondary interface using the second data source, but now all interface is to use the main data source, com. LDW. Mapper. The secondary is below, this is how to return a responsibility? How to realize the com. LDW. Mapper. Under the secondary interface USES the second data source?

Below is DruidConfig configuration class
 
@ Configuration
Public class DruidConfig {

//configuration data source parameter
/*
@ ConfigurationProperties (prefix="spring. The datasource")
@ Bean
Public DataSource druid () {
Return new DruidDataSource ();
}
*/

@ Bean (name="primaryDataSource")
@ the Qualifier (" primaryDataSource ")
@ Primary
@ ConfigurationProperties (prefix="spring. The datasource. The primary")
Public DataSource primaryDataSource () {
Return new DruidDataSource ();
}


@ Bean (name="secondaryDataSource")
@ the Qualifier (" secondaryDataSource ")
//@ Primary
@ ConfigurationProperties (prefix="spring. The datasource. Secondary")
Public DataSource secondaryDataSource () {
Return new DruidDataSource ();
}
}


The following is the main data source configuration classes:
 
@ Configuration
@ EnableTransactionManagement
@ EnableJpaRepositories (
EntityManagerFactoryRef="entityManagerFactoryPrimary",
TransactionManagerRef="transactionManagerPrimary",
BasePackages={" com. LDW. Mapper. The primary "})
//specify the data source of the DAO interface package
Public class PrimaryConfig {

The @autowired
@ the Qualifier (" primaryDataSource ")
Private DataSource primaryDataSource;

@ Primary
@ Bean (name="entityManagerPrimary")
Public EntityManager EntityManager (EntityManagerFactoryBuilder builder) {
Return entityManagerFactoryPrimary (builder). GetObject () createEntityManager ();
}

@ Primary
@ Bean (name="entityManagerFactoryPrimary")
Public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
Return builder
. The dataSource (primaryDataSource)
//. The properties (getVendorProperties ())
Packages (" com. LDW. Model. Primary ")
//set the position of the entity class
. PersistenceUnit (" primaryPersistenceUnit ")
.build();
}

/*
The @autowired
Private Environment env.

Private Map getVendorProperties () {

HashMap The properties=new HashMap<> (a);
The properties, put (" hibernate. The dialect, "
Env. GetProperty (" hibernate. The dialect "));
The properties. The put (" hibernate. DDL - auto ",
"Create");
The properties, put (" hibernate. Physical_naming_strategy, "
"Org. Springframework. Boot. The orm. Jpa, hibernate. SpringPhysicalNamingStrategy");
The properties, put (" hibernate. Implicit_naming_strategy, "
"Org. Springframework. Boot. The orm. Jpa, hibernate. SpringImplicitNamingStrategy");
Return the properties;

}
*/

@ Primary
@ Bean (name="transactionManagerPrimary")
Public PlatformTransactionManager transactionManagerPrimary (EntityManagerFactoryBuilder builder) {
Return new JpaTransactionManager (entityManagerFactoryPrimary (builder). GetObject ());
}

}


Here is the second data source configuration classes:
 

@ Configuration
@ EnableTransactionManagement
@ EnableJpaRepositories (
EntityManagerFactoryRef="entityManagerFactorySecondary",
TransactionManagerRef="transactionManagerSecondary",
BasePackages={" com. LDW. Mapper. Secondary "})
//set the DAO interface layer locations package
Public class SecondaryConfig {

The @autowired
@ the Qualifier (" secondaryDataSource ")
Private DataSource secondaryDataSource;

@ Bean (name="entityManagerSecondary")
Public EntityManager EntityManager (EntityManagerFactoryBuilder builder) {
Return entityManagerFactorySecondary (builder). GetObject () createEntityManager ();
}

@ Bean (name="entityManagerFactorySecondary")
Public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
Return builder
. The dataSource (secondaryDataSource)
//. The properties (getVendorProperties ())
Packages (" com. LDW. Model. Secondary ")
//set the entity classes in the location of the package
. PersistenceUnit (" primaryPersistenceUnit ")
.build();
}

/*
The @autowired
Private Environment env.
Private Map getVendorProperties () {
HashMap The properties=new HashMap<> (a);
Properties. Put (hibernate. Hbm2ddl. "auto",
Env. GetProperty (hibernate. Hbm2ddl. "auto"));
The properties. The put (" hibernate. DDL - auto ",
Env. GetProperty (" update "));
The properties, put (" hibernate. The dialect, "
Env. GetProperty (" hibernate. The dialect "));
The properties, put (" hibernate. Physical_naming_strategy, "
"Org. Springframework. Boot. The orm. Jpa, hibernate. SpringPhysicalNamingStrategy");
The properties, put (" hibernate. Implicit_naming_strategy, "
"Org. Springframework. Boot. The orm. Jpa, hibernate. SpringImplicitNamingStrategy");
Return the properties;
}
*/

@ Bean (name="transactionManagerSecondary")
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related