关于项目中多数据源配置的一些操作
在项目中,我们除了使用单一的数据源(一库多表)的情况。也不排除一些奇葩需求,需要链接多个数据源的情况,遇到这种情况怎么办呢?怼上去撒。。。
针对公司项目框架,配置如下:
application配置-mysql
spring:
datasource: #数据库源
primary:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/databasename1?useUnicode=true&characterEncoding=UTF-8&useSLL=true
username: yourName
password: 123456
secondary:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/databasename2
username: root
password: root
jpa: #jpa数据操作
properties:
hibernate:
hbm2ddl:
auto: update
show-sql: true
database: mysql
配置完这步之后,需要新建相关的config
配置如下:
DataSourceConfiguration
指定你所需要引用的数据源配置
//数据源1-引用配置:spring.datasource.primary
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
//数据源2-引用配置:spring.datasource.secondary
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
这步完成之后,你就需要为你的每个数据源指定entity、repository的路径
敲黑板 划重点
下面的两个config注意以下两个参数
basePackages= {"cn.com.ssii.primary.**.repository"})
packages("cn.com.ssii.primary.**.entity")
这两个参数主要是指定你的每个数据源所需要对应的实体和相关实体仓库,你在哪个数据源对应的packages中建立了实体,那么相应的就会在哪个数据库中新建相应的表(与单数据源原理相同)
重点又来了 做好笔记
在两个数据源中禁止名字相同的repository,会报错的.....
PrimaryConfig
使用DataSource注入JPA,指明每个数据源SpringData要扫描的domain,Dao的包名
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
//设置Repository所在位置 根据项目实际需求,配置你的包路径
basePackages= {"cn.com.ssii.primary.**.repository"})
public class PrimaryConfig {
//创建数据源 primaryDataSource - 引用数据源1配置:primaryDataSource
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
/**
* 创建数据源1的entityManagerFactoryPrimary
* 1,使用primaryDataSource配置
* 2,数据实体包名packages:com.example.domain.primary
*/
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.properties(getVendorProperties(primaryDataSource))
//设置实体类所在位置
.packages("cn.com.ssii.primary.**.entity")
.persistenceUnit("primaryPersistenceUnit")
.build();
}
@Autowired
private JpaProperties jpaProperties;
private Map getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
SecondaryConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
//设置Repository所在位置
basePackages= {"cn.com.ssii.secondary.**.repository"})
public class SecondaryConfig {
/**
* 自动注入 DataSourceConfig 类中的数据源配置文件secondaryDataSource
* 实例化DataSource(secondaryDataSource)
*/
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
/**
* 创建 EntityManager :
* 根据EntityManagerFactoryBuilder 创建 EntityManager
* @param builder EntityManagerFactoryBuilder
* @return
*/
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
/**
* 根据当前数据源配置,创建entityManagerFactory
* @param builder
* @return
*/
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource)
.properties(getVendorProperties(secondaryDataSource))
//设置实体类所在位置
.packages("cn.com.ssii.secondary.**.entity")
.persistenceUnit("secondaryPersistenceUnit")
.build();
}
@Autowired
private JpaProperties jpaProperties;
private Map getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
/**
* 根据entityManagerFactory(entityManagerFactorySecondary)生成transactionManager(transactionManagerSecondary)
* @param builder PlatformTransactionManager
* @return
*/
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
以上步骤完成之后,基本配置就完成了。