springboot配置多数据源
1.配置文件
首先我么们先准备好配置文件中的数据源
#SQLServer 数据源1
# primary
spring.primary.datasource.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=aaa
spring.primary.datasource.username=sa
spring.primary.datasource.password=123456
spring.primary.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.primary.datasource.maxActive=100
spring.primary.datasource.minIdle=2
spring.primary.datasource.initialSize=30
spring.primary.datasource.maxWait=2000
spring.primary.datasource.removeAbandoned=true
spring.primary.datasource.removeAbandonedTimeout=180
spring.primary.datasource.useGlobalDataSourceStat=true
#SQLServer 数据源2
spring.ths.datasource.url=jdbc:sqlserver://xxx.xx.xx.xx:1433;DatabaseName=bbbb
spring.ths.datasource.username=sa
spring.ths.datasource.password=123456
spring.ths.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.ths.datasource.maxActive=100
spring.ths.datasource.minIdle=2
spring.ths.datasource.initialSize=30
spring.ths.datasource.maxWait=2000
spring.ths.datasource.removeAbandoned=true
spring.ths.datasource.removeAbandonedTimeout=180
spring.ths.datasource.useGlobalDataSourceStat=true
#SQLServer 数据源3
#secondary
spring.hbj.wcm.datasource.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=cccc
spring.hbj.wcm.datasource.username=sa
spring.hbj.wcm.datasource.password=123456
spring.hbj.wcm.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.hbj.wcm.datasource.maxActive=300
spring.hbj.wcm.datasource.minIdle=2
spring.hbj.wcm.datasource.initialSize=30
spring.hbj.wcm.datasource.maxWait=2000
spring.hbj.wcm.datasource.removeAbandoned=true
spring.hbj.wcm.datasource.removeAbandonedTimeout=180
spring.hbj.wcm.datasource.useGlobalDataSourceStat=true
#SQLServer 数据源4
#secoundary2
spring.hbj.ths.datasource.url=jdbc:sqlserver://xx.xx.xx.xx;instanceName=HBJ;databaseName=dddd
spring.hbj.ths.datasource.username=sa
spring.hbj.ths.datasource.password=123456
spring.hbj.ths.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.hbj.ths.datasource.maxActive=300
spring.hbj.ths.datasource.minIdle=2
spring.hbj.ths.datasource.initialSize=30
spring.hbj.ths.datasource.maxWait=2000
spring.hbj.ths.datasource.removeAbandoned=true
spring.hbj.ths.datasource.removeAbandonedTimeout=180
spring.hbj.ths.datasource.useGlobalDataSourceStat=true
# spring.jpa.hibernate.naming.physical-strategy=com.leadingsoft.dbdata.util.MySQLUpperCaseStrategy
logging.path=log
logging.level.com.leadingsoft.patrol=DEBUG
logging.level.org.springframework=WARN
config配置
我们需要配置一个主数据源,然后你就可以在配置其他的数据源了。其中配置主数据源需要加上@Primary注解,在配置数据源是我们应该给对应的方法上加如@Bean注解并指定好这个Bean的名字 ,当然这个Bean的名字是你定义的。
- 配置主数据源
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class DataSourcePrimaryWcnConfig {
@Bean(name = "primaryDS")
@Qualifier("primaryDS")
@Primary //将数据源配置一个主数据源
@ConfigurationProperties(prefix = "spring.primary.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
- 配置从数据源
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DataSourceThsConfig {
@Bean(name = "bbb")
@Qualifier("bbb")
@ConfigurationProperties(prefix = "spring.ths.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
其余的从数据源与此从数据源配置相同
就此多数据源配置就完成了 ,如果还需要配置 jpa 的话需要配置 EntityManager
- 配置EntityManager
跟配置主数据源同理,在配置主数据源对应的 EntityManager 时 需要加上@Primary
import java.util.Map;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.leadingsoft.dbdata.repository.wcn" }) //指定的jpa数据层接口所在的包路径
public class RepositoryPrimaryWcnConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("primaryDS")
private DataSource primaryDS;
@Bean(name = "entityManagerPrimary")
@Primary
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryPrimary")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder.dataSource(primaryDS).properties(getVendorProperties(primaryDS))
.packages("com.leadingsoft.dbdata.entity") // 设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit").build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerPrimary")
@Primary
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
- 配置从数据源的 jpa
import java.util.Map;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryhbjths",
transactionManagerRef = "transactionManagerhbjths",
basePackages = {"com.leadingsoft.dbdata.repository.bbb"})
public class RepositoryHbjThsConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("bbb")
private DataSource secondaryDS;
@Bean(name = "entityManagerhbjbbb")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryhbjbbb")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
return builder.dataSource(secondaryDS).properties(getVendorProperties(secondaryDS))
.packages("com.leadingsoft.dbdata.entity")
.persistenceUnit("hbjthsPersistenceUnit").build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerhbjbbb")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
其他从数据源对 jpa 的配置和此相同。
就此你就可以在entity包中创建数据源库中跟表结构对应的实体类了,@EnableJpaRepositories 注解中 basePackages 指定的是jpa接口所在的包,你在指定的包中创建的jpa接口会根据你配置的DataSource去指定的库中进行操作。