spring boot 1.x內(nèi)多數(shù)據(jù)源配置方式參考Spring Boot多數(shù)據(jù)源配置與使用沈自,但升級(jí)到2后該配置無效酣倾,以下是自測(cè)可行的配置方式(spring boot版本2.0.6.RELEASE)古拴。
1. application.properties配置
#數(shù)據(jù)源1
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driverClassName = com.mysql.jdbc.Driver
#數(shù)據(jù)源2
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driverClassName = com.mysql.jdbc.Driver
2.第一數(shù)據(jù)源配置
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager",
// 此處指定第一數(shù)據(jù)源對(duì)于dao包路徑
basePackages = "com.onecodespace.codegenerator.business.dao")
public class PrimaryConfig {
@Bean
PlatformTransactionManager primaryTransactionManager() {
return new JpaTransactionManager(primaryEntityManagerFactory().getObject());
}
@Bean
LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(primaryDataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
// 此處指定第一數(shù)據(jù)源對(duì)應(yīng)實(shí)體類包路徑
factoryBean.setPackagesToScan("com.onecoderspace.codegenerator.business.domain");
return factoryBean;
}
// @Bean
// @ConfigurationProperties(prefix = "spring.datasource.primary")
// DataSource primaryDataSource() {
// return new EmbeddedDatabaseBuilder().
// setType(EmbeddedDatabaseType.H2).
// build();
// }
@Bean
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
//通過DataSourceBuilder構(gòu)建數(shù)據(jù)源
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
}
3. 第二數(shù)據(jù)源配置
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager",
// 第二數(shù)據(jù)源對(duì)應(yīng)dao包路徑
basePackages = "com.onecodespace.codegenerator.schema.dao")
public class SecondaryConfig extends DataSourceAutoConfiguration {
@Bean
PlatformTransactionManager secondaryTransactionManager() {
return new JpaTransactionManager(secondaryEntityManagerFactory().getObject());
}
@Bean
LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(secondaryDataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
// 第二數(shù)據(jù)源對(duì)應(yīng)實(shí)體的包路徑
factoryBean.setPackagesToScan("com.onecodespace.codegenerator.schema.domain");
return factoryBean;
}
@Bean
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
//通過DataSourceBuilder構(gòu)建數(shù)據(jù)源
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
// 如果使用jdbcTemplate氓润,進(jìn)行如下設(shè)置即可
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
在兩個(gè)數(shù)據(jù)源對(duì)應(yīng)包下創(chuàng)建實(shí)體罢荡、dao憨募、service紧索,運(yùn)行系統(tǒng)測(cè)試,可看到實(shí)體對(duì)應(yīng)表分別在兩個(gè)庫(kù)內(nèi)操作菜谣。