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 () {
HashMapThe 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 () {
HashMapThe 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