因为我们配置的是多个数据源所有其中一个数据源必须加入这个注解@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);}}}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 彻底理解什么是同步和异步!
- K8s 多集群实践思考和探索
- 关于接口测试,你了解多少?
- 多样性视觉常识推理数据集GD-VCR
- 回顾:都说娱乐圈钱好挣,这4位明星的家底,确实让很多人羡慕不已
- 布丁多肉怎么养才长得好
- 紫牡丹多肉夏天怎么养 紫牡丹多肉植物怎么养
- TVB知名老戏骨罕见现身,满头白发烟不离手,结婚30多年坚持丁克
- 以爱为营开播在即!冲着白鹿去的,意外被20多岁小姐姐吸引
- 玛格丽特怎么养? 玛格丽特多肉怎么养才长得好