Hi Harshit S.
創(chuàng)新互聯(lián)云計算的互聯(lián)網(wǎng)服務(wù)提供商,擁有超過13年的服務(wù)器租用、川西大數(shù)據(jù)中心、云服務(wù)器、虛擬主機、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗,已先后獲得國家工業(yè)和信息化部頒發(fā)的互聯(lián)網(wǎng)數(shù)據(jù)中心業(yè)務(wù)許可證。專業(yè)提供云主機、虛擬主機、域名與空間、VPS主機、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。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.
網(wǎng)站名稱:multipledatasourceconfig-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://jinyejixie.com/article42/gighc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站建設(shè)、小程序開發(fā)、網(wǎng)站設(shè)計公司、動態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容