Hi Harshit S.

project structure:

multiple datasource config as follows:

step 1:

step 2:add a datasource configuration class

@Configuration
public class DataSourceConfig {
// ape datasource config
@Value("${spring.ape-datasource.driver-class-name}")
private String apeDriverClassName; @Value("${spring.ape-datasource.url}")
private String apeDBUrl; @Value("${spring.ape-datasource.username}")
private String apeDBUsername; @Value("${spring.ape-datasource.password}")
private String apeDBPassword; // DBQ datasource config
@Value("${spring.dbq-datasource.url}")
private String dbqDBUrl; @Value("${spring.dbq-datasource.username}")
private String dbqDBUsername; @Value("${spring.dbq-datasource.password}")
private String dbqDBPassword; @Value("${spring.dbq-datasource.driver-class-name}")
private String dbqDriverClassName; @Primary
@Bean(name = "apeDataSource")
@Qualifier("apeDataSource")
public DataSource apeDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setPoolName("ape pool");
dataSource.setDriverClassName(apeDriverClassName);
dataSource.setJdbcUrl(apeDBUrl);
dataSource.setUsername(apeDBUsername);
dataSource.setPassword(apeDBPassword);
dataSource.setAutoCommit(true);
dataSource.setMaximumPoolSize(20);
return dataSource;
} @Bean(name = "dbqDataSource")
@Qualifier("dbqDataSource")
public DataSource dbqDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setPoolName("DBQ pool");
dataSource.setDriverClassName(dbqDriverClassName);
dataSource.setJdbcUrl(dbqDBUrl);
dataSource.setUsername(dbqDBUsername);
dataSource.setPassword(dbqDBPassword);
dataSource.setAutoCommit(true);
dataSource.setMaximumPoolSize(20);
return dataSource;
} }

Step3: add Jpa config class

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "apeManagerFactory",
transactionManagerRef = "apeTransactionManager",
basePackages = {"com.cn.ano2ape.repository.ape"}
)
public class ApeJpaConfig {
@Autowired
@Qualifier("apeDataSource")
private DataSource apeDataSource; @Autowired
private JpaProperties jpaProperties; @Autowired
private HibernateProperties hibernateProperties; @Primary
@Bean(name = "apeEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return apexaEntityManagerFactory(builder).getObject().createEntityManager();
} @Primary
@Bean(name = "apeManagerFactory")
public LocalContainerEntityManagerFactoryBean apexaEntityManagerFactory(EntityManagerFactoryBuilder builder) {
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
return builder.dataSource(apeDataSource).properties(properties).packages("com.cn.ano2ape.model.ape").build();
} @Primary
@Bean(name = "apeTransactionManager")
PlatformTransactionManager apexaTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(apexaEntityManagerFactory(builder).getObject());
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "dbqManagerFactory",
transactionManagerRef = "dbqTransactionManager",
basePackages = {"com.cn.ano2ape.repository.dbq"}
)
public class DbqJpaConfig {
@Autowired
@Qualifier("dbqDataSource")
private DataSource dbqDataSource; @Autowired
private JpaProperties jpaProperties; @Autowired
private HibernateProperties hibernateProperties; @Bean(name = "dbqEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return dbqEntityManagerFactory(builder).getObject().createEntityManager();
} @Bean(name = "dbqManagerFactory")
public LocalContainerEntityManagerFactoryBean dbqEntityManagerFactory(EntityManagerFactoryBuilder builder) {
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
return builder.dataSource(dbqDataSource).properties(properties).packages("com.cn.ano2ape.model.dbq").build();
} @Bean(name = "dbqTransactionManager")
PlatformTransactionManager dbqTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(dbqEntityManagerFactory(builder).getObject());
}
}

end.

multiple datasource config的更多相关文章

  1. jpa datasource config

    application.properties spring.datasource.driverClassName= spring.datasource.url= spring.datasource.u ...

  2. MyBatis(3.2.3) - Configuring MyBatis using XML, Environment

    The key component of MyBatis is SqlSessionFactory from which we get SqlSession and execute the mappe ...

  3. spring cloud 使用spring cloud bus自动刷新配置

    Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitMQ.Kafka等)连接分布式系统的节点,这样就可以通过Spring Cloud Bus广播配置的变化 ...

  4. spring cloud config--client

    概述 之前我们简单的搭建了一个单点的config-server服务,实现配置文件的统一管理,本次文章将实现config-client是如何从config-server中获取到统一配置文件信息的 1.创 ...

  5. spring cloud Config--server

    概述 使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spr ...

  6. springboot动态多数据源切换

    application-test.properties #datasource -- mysql multiple.datasource.master.url=jdbc:mysql://localho ...

  7. 六、spring boot 1.5.4 配置多数据源

    spring boot 已经支持多数据源配置了,无需网上好多那些编写什么类的,特别麻烦,看看如下解决方案,官方的,放心! 1.首先定义数据源配置 #=====================multi ...

  8. Spring基础知识

    Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...

  9. Spring PropertyPlaceholderConfigurer数据库配置

    pom.xml中添加依赖 <!-- mysql-connector-java --> <dependency> <groupId>mysql</groupId ...

随机推荐

  1. VIM如何自动保存文件、自动重加载文件、自动刷新显示文件

    1.手动重加载文件的命令是:e! 2.一劳永逸的方法是:vim提供了自动加载的选项 autoread,默认关闭. 在vimrc中添加 set autoread即可打开自动加载选项,相关选项: :hel ...

  2. tp5 apache 转 nginx 需要配置的伪静态

    location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?s=$ last; break; } }

  3. 最简单的Android项目

    这是我在windows环境下,试验过的最简单Android项目,只用记事本和命令行即可完成. 环境准备 开发环境需要Java SDK(官网下载),Android SDK(官网下载). 首先安装Java ...

  4. RMQ 2d 模板

    #include<iostream> #include<cstdio> #include<string.h> #include<string> #inc ...

  5. 基于注解的IOC配置

    1 明确 注解配置和XML配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样. 关于实际的开发中到底是使用XML还是注解,每家公司有着不同的习惯.具体问题具体分析. 2 环境搭建 ...

  6. python之路day13--迭代器

    迭代器 我们已经知道,可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的ge ...

  7. 【BZOJ 3681】Arietta

    传送门 题目描述 Arietta 的命运与她的妹妹不同,在她的妹妹已经走进学院的时候,她仍然留在山村中. 但是她从未停止过和恋人 Velding 的书信往来.一天,她准备去探访他. 对着窗外的阳光,临 ...

  8. 【leetcode】638. Shopping Offers

    题目如下: In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, ther ...

  9. Python 元组Ⅱ

    删除元组 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例: 以上实例元组被删除后,输出变量会有异常信息,输出如下所示: 元组运算符 与字符串一样,元组之间可以使用 + ...

  10. c# linq 分组groupby

    转载: https://www.cnblogs.com/cncc/p/9846390.html 一.先准备要使用的类: 1.Person类: class Person { public string ...