在使用spring-data-jpa时,如果要配置多数据源,一般为以下三个步骤:
- 在配置文件配置多数据源
- 不同源的 repository 放入不同包路径
- 声明不同的包路径下使用不同的数据源、事务支持
1.配置文件配置多数据源
spring.primary.datasource.url=jdbc:mysql://localhost:3306/testspring.primary.datasource.username=rootspring.primary.datasource.password=rootspring.primary.datasource.driver-class-name=com.mysql.jdbc.Driverspring.secondary.datasource.url=jdbc:mysql://localhost:3306/test1spring.secondary.datasource.username=rootspring.secondary.datasource.password=rootspring.secondary.datasource.driver-class-name=com.mysql.jdbc.Driver
2.不同源的 repository 放入不同包路径
3.声明不同的包路径下使用不同的数据源
将数据源注入到 Factory,配置 repository、domian 的位置,需要设置一个默认的数据源
@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 MapgetVendorProperties(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 MapgetVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); }}
读取两个配置源,构建两个数据源:
@Configurationpublic 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(); }}