**
Springboot 1.4.5 JPA 多数据源配置
**
- 创建总的配置类
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 DataSourceConfig {
private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")//此注解相当于@Autowired 一来注入bean
@Primary//多数据源配置需要设置一个主的数据源
@ConfigurationProperties(prefix = "spring.datasource.primary") //application.properties文件中第一个数据库连接信息配置
//根据配置创建第一个数据源
public DataSource primaryDataSource() {
logger.info("第一个数据库连接池创建中.......");
//DataSourceBuilder.create().build();会根据application.properties文件中第一个数据库连接信息配置创建数据库连接
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.second") //application.properties文件中第二个数据库连接信息配置
//根据配置创建第二个数据源
public DataSource secondaryDataSource() {
logger.info("第二个数据库连接池创建中.......");
return DataSourceBuilder.create().build();
}
}
- 主数据源配置
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;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement//开启事务注解
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {
"com.zteict.hospital.dao", "com.zteict.hospital.oldman.dao" }) // 设置Repository(dao层接口)所在位置
public class PrimaryConfig {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Autowired(required = false)
private JpaProperties jpaProperties;
@Primary
@Bean(name = "entityManagerPrimary")
//创建entityManager实例,用于管理你的实体类,在jpa中通过他与数据库建立关系进行增删改查
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
//由管理器管理你的主数据源
return builder.dataSource(primaryDataSource)
.properties(getVendorProperties(primaryDataSource))//设置映射关系
.packages("com.zteict.hospital.entity", "com.zteict.hospital.oldman.entity")//设置实体类所在位置
.persistenceUnit("primaryDataSourceUnit")//数据源避免重复,值随自己喜好
.build();
}
//建立实体类和数据库中表的映射关系
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
- 其他数据源配置
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;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary", transactionManagerRef = "transactionManagerSecondary", basePackages = {
"com.zteict.hospital.knowledgeresult.dao" }) // 设置Repository(dao层接口)所在位置
public class SecondaryConfig {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
return builder.dataSource(secondaryDataSource).properties(getVendorProperties(secondaryDataSource))
.packages("com.zteict.hospital.knowledgeresult.entity") // 设置实体类所在位置
.persistenceUnit("secondaryDataSourceUnit").build();
}
// 建立实体类和数据库中表的映射关系
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
4.application.properties配置
#配置数据源
#主数据源
spring.datasource.primary.driver-class-name: oracle.jdbc.driver.OracleDriver
spring.datasource.primary.url:
spring.datasource.primary.username:
spring.datasource.primary.password:
#第二个数据源
spring.datasource.second.driver-class-name: oracle.jdbc.driver.OracleDriver
spring.datasource.second.url:
spring.datasource.second.username:
spring.datasource.second.password: