在使用spring-data-jpa時,若是要配置多數據源,通常爲如下三個步驟:mysql
1.配置文件配置多數據源spring
spring.primary.datasource.url=jdbc:mysql://localhost:3306/test spring.primary.datasource.username=root spring.primary.datasource.password=root spring.primary.datasource.driver-class-name=com.mysql.jdbc.Driver spring.secondary.datasource.url=jdbc:mysql://localhost:3306/test1 spring.secondary.datasource.username=root spring.secondary.datasource.password=root spring.secondary.datasource.driver-class-name=com.mysql.jdbc.Driver
2.不一樣源的 repository 放入不一樣包路徑sql
3.聲明不一樣的包路徑下使用不一樣的數據源dom
將數據源注入到 Factory,配置 repository、domian 的位置,須要設置一個默認的數據源ui
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {"com.zb.boot.repository.primary"}//聲明路徑 ) public class PrimaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier("primaryDataSource") private DataSource primaryDataSource; public EntityManager entityManager(EntityManagerFactoryBuilder builder){ return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); } @Bean(name = "entityManagerFactoryPrimary") @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder){ return builder.dataSource(primaryDataSource) .properties(getVendorProperties(primaryDataSource)) .packages("com.zb.boot.model")//實體類的路徑 .persistenceUnit("primaryPersistenceUnit") .build(); } public Map<String, String> getVendorProperties(DataSource dataSource){ return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerPrimary") @Primary PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder){ return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); } }
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary", basePackages= { "com.zb.boot.repository.secondary" }) public class SecondaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @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 .dataSource(secondaryDataSource) .properties(getVendorProperties(secondaryDataSource)) .packages("com.zb.boot.model") .persistenceUnit("secondaryPersistenceUnit") .build(); } private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); } }
讀取兩個配置源,構建兩個數據源:url
@Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.primary.datasource") public DataSource primaryDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @Primary @ConfigurationProperties(prefix = "spring.secondary.datasource") public DataSource secondaryDataSource(){ return DataSourceBuilder.create().build(); } }