lishinka

springboot 双数据源配置

1.application.configproperties 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
##数据源1-ldrcims
spring.datasource.ldrcims.driverClassName=com.mysql.jdbc.Driver
spring.datasource.ldrcims.url=jdbc:mysql://localhost:3306/ldrcims?ch1aracterEncoding=UTF-8&useSSL=false
spring.datasource.ldrcims.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.ldrcims.username=root
spring.datasource.ldrcims.password=123456

##数据源2-user
spring.datasource.user.driverClassName=com.mysql.jdbc.Driver
spring.datasource.user.url=jdbc:mysql://localhost:3306/rmis_new?ch1aracterEncoding=UTF-8&useSSL=false
spring.datasource.user.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.user.username=root
spring.datasource.user.password=123456

2.DataSourceConfigurer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Bean(name="userDataSource")
@Qualifier("userDataSource")
@ConfigurationProperties(prefix = "spring.datasource.user")
public DataSource userDataSource(){
return DataSourceBuilder.create().build();
}

@Bean(name="ldrcimsDataSource")
@Primary
@Qualifier("ldrcimsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.ldrcims")
public DataSource ldrcimsDataSource(){
return DataSourceBuilder.create().build();
}

3.LdrcimsConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "ldrcimsEntityManagerFactory",
transactionManagerRef = "ldrcimsTransactionManager",
basePackages = { "com.southgis.ldrcims.repository.ldrcims" }) // 设置Repository所在位置
public class LdrcimsConfig {

@Autowired
private JpaProperties jpaProperties;

@Autowired
@Qualifier("ldrcimsDataSource")
private DataSource ldrcimsDataSource;

@Bean(name = "ldrcimsEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean ldrcimsEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(ldrcimsDataSource)
.properties(getVendorProperties(ldrcimsDataSource))
.packages("com.southgis.ldrcims.entity.ldrcims") //设置实体类所在位置
.persistenceUnit("ldrcimsPersistenceUnit")
.build();
//.getObject();//不要在这里直接获取EntityManagerFactory
}

private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}

@Bean(name = "ldrcimsEntityManagerFactory")
@Primary
public EntityManagerFactory ldrcimsEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return this.ldrcimsEntityManagerFactoryBean(builder).getObject();
}

@Bean(name = "ldrcimsTransactionManager")
@Primary
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(ldrcimsEntityManagerFactory(builder));
}
}

4. UserConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "userEntityManagerFactory",
transactionManagerRef = "userTransactionManager",
basePackages = { "com.southgis.ldrcims.repository.user" }) // 设置Repository所在位置
public class UserConfig {

@Autowired
private JpaProperties jpaProperties;

@Autowired
@Qualifier("userDataSource")
private DataSource userDataSource;

@Bean(name = "userEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean userEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(userDataSource)
.properties(getVendorProperties(userDataSource))
.packages("com.southgis.ldrcims.entity.user") //设置实体类所在位置
.persistenceUnit("userPersistenceUnit")
.build();
//.getObject();//不要在这里直接获取EntityManagerFactory
}

private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}

@Bean(name = "userEntityManagerFactory")
public EntityManagerFactory userEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return this.userEntityManagerFactoryBean(builder).getObject();
}
@Bean(name = "userTransactionManager")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(userEntityManagerFactory(builder));
}
}