SpringBoot实现多数据源配置详解( 三 )

因为我们配置的是多个数据源所有其中一个数据源必须加入这个注解@Primary
接下来是jpa的EntityManagerFactory工厂的配置了

  • EntityManagerFactory配置
public class EntityManagerFactoryConfig {@Configuration@EnableJpaRepositories(basePackages = {"${master.jpa.repos}" }, entityManagerFactoryRef = "masterEntityManagerFactory", transactionManagerRef = "masterJPATransactionManager")static class MasterEntityManagerFactory {@Resource(name = "masterDataSource")private DataSource masterDataSource;@Value("${master.jpa.domain}")private String masterDomainPkg;@Bean@Primarypublic LocalContainerEntityManagerFactoryBean masterEntityManagerFactory(EntityManagerFactoryBuilder builder) {Map<String, Object> properties = new HashMap<>();properties.put("hibernate.hbm2ddl.auto", "update");properties.put("hibernate.id.new_generator_mappings", true);properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName()) ;return builder.dataSource(masterDataSource).packages(masterDomainPkg).persistenceUnit("master").properties(properties).build();}@Bean@Primarypublic PlatformTransactionManager masterJPATransactionManager(EntityManagerFactoryBuilder builder) {JpaTransactionManager tm = new JpaTransactionManager(masterEntityManagerFactory(builder).getObject());return tm;}}@Configuration@EnableJpaRepositories(basePackages = {"${slave.jpa.repos}" }, entityManagerFactoryRef = "slaveEntityManagerFactory", transactionManagerRef = "slaveJPATransactionManager")@ConditionalOnProperty(name = "multiple.ds.enabled", havingValue = https://www.isolves.com/it/cxkf/jiagou/2023-09-07/"true")static class SlaveEntityManagerFactory {@Resource(name = "slaveDataSource")private DataSource slaveDataSource;@Value("${slave.jpa.domain}")private String slaveDomainPkg;@Beanpublic LocalContainerEntityManagerFactoryBean slaveEntityManagerFactory(EntityManagerFactoryBuilder builder) {Map properties = new HashMap<>();properties.put("hibernate.hbm2ddl.auto", "update");properties.put("hibernate.id.new_generator_mappings", true);properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName()) ;return builder.dataSource(slaveDataSource).packages(slaveDomainPkg).persistenceUnit("slave").properties(properties).build();}@Beanpublic PlatformTransactionManager slaveJPATransactionManager(EntityManagerFactoryBuilder builder) {JpaTransactionManager tm = new JpaTransactionManager(slaveEntityManagerFactory(builder).getObject());return tm;}}}
  • mybatis SqlSessionFactory工厂配置
public class SqlSessionFactoryConfig {@Configurationstatic class MasterSqlSessionFactory {@Resourceprivate MasterMybatisProperties properties;@Autowired(required = false)private Interceptor[] interceptors;@Autowiredprivate ResourceLoader resourceLoader = new DefaultResourceLoader();@Autowired(required = false)private DatabaseIdProvider databaseIdProvider;@Beanpublic SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource)throws Exception {SqlSessionFactoryBean factory = new SqlSessionFactoryBean();factory.setDataSource(dataSource);factory.setVfs(SpringBootVFS.class);if (StringUtils.hasText(this.properties.getConfigLocation())) {factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));}factory.setConfiguration(properties.getConfiguration());if (!ObjectUtils.isEmpty(this.interceptors)) {factory.setPlugins(this.interceptors);}if (this.databaseIdProvider != null) {factory.setDatabaseIdProvider(this.databaseIdProvider);}if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());}if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());}if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {factory.setMapperLocations(this.properties.resolveMapperLocations());}return factory.getObject();}@Beanpublic DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Beanpublic SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {ExecutorType executorType = this.properties.getExecutorType();if (executorType != null) {return new SqlSessionTemplate(sqlSessionFactory, executorType);} else {return new SqlSessionTemplate(sqlSessionFactory);}}}@Configuration@ConditionalOnProperty(name = "multiple.ds.enabled", havingValue = https://www.isolves.com/it/cxkf/jiagou/2023-09-07/"true")static class SlaveSqlSessionFactory {@Resourceprivate SlaveMybatisProperties properties;@Autowired(required = false)private Interceptor[] interceptors;@Autowiredprivate ResourceLoader resourceLoader = new DefaultResourceLoader();@Autowired(required = false)private DatabaseIdProvider databaseIdProvider;@Beanpublic SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean factory = new SqlSessionFactoryBean();factory.setDataSource(dataSource);factory.setVfs(SpringBootVFS.class);if (StringUtils.hasText(this.properties.getConfigLocation())) {factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));}factory.setConfiguration(properties.getConfiguration());if (!ObjectUtils.isEmpty(this.interceptors)) {factory.setPlugins(this.interceptors);}if (this.databaseIdProvider != null) {factory.setDatabaseIdProvider(this.databaseIdProvider);}if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());}if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());}if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {factory.setMapperLocations(this.properties.resolveMapperLocations());}return factory.getObject();}@Beanpublic DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Beanpublic SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {ExecutorType executorType = this.properties.getExecutorType();if (executorType != null) {return new SqlSessionTemplate(sqlSessionFactory, executorType);} else {return new SqlSessionTemplate(sqlSessionFactory);}}}}


推荐阅读