1、application.yml文件配置多数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
#数据源1
test1:
jdbcUrl: jdbc:oracle:thin:@10.10.10.01:1521:test1
username: test1
password: test1
#数据源2
test2:
jdbcUrl: jdbc:oracle:thin:@10.10.10.02:1521:test2
username: test2
password: test2
initialSize: 50
minIdle: 80
maxActive: 300
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: select '1' from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
removeAbandoned: true
removeAbandonedTimeout: 180
logAbandoned: true
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
jpa:
show_sql: true
format_sql: true
2、数据源配置类 DataSourceConfig
import org.springframework.beans.factory.annotation.Qualifier;
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.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* 数据源配置类
*/
@Configuration
public class DataSourceConfig {
/**
* 移动核保数据源
* @return
*/
@Bean(name = "test1")
@ConfigurationProperties(prefix="spring.datasource.test1")
public DataSource test1DataSource() {
return DataSourceBuilder.create().build();
}
/**
* 中间库数据源
* @return
*/
@Bean(name = "test2")
@Primary
@ConfigurationProperties(prefix="spring.datasource.test2")
public DataSource test2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test1Template")
public JdbcTemplate test1Template(
@Qualifier("test1") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "test2Template")
public JdbcTemplate test2Template(
@Qualifier("test2") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
3、test1数据源配置
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
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.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
/**
* test1数据源配置类
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", //EntityManagerFactory引用
transactionManagerRef = "transactionManager", //transactionManager引用
basePackages = {"com.jpa.test1"})//jpa接口package
public class Test1DatasourceConfig {
@Resource(name = "test1")
private DataSource datasource;
/**
* 注入JPA配置实体
*/
@Autowired
private JpaProperties jpaProperties;
/**
* 这里其实不需要配置数据库的方言.
* 像hibernate.hbm2ddl.auto 可以在这里配置.但是我的是在application.properties中配置的.
*/
private Map<String, Object> getVendorProperties() {
HibernateSettings hibernateSettings = new HibernateSettings();
return jpaProperties.getHibernateProperties(hibernateSettings);
}
/**
* 配置EntityManagerFactory实体
*
* @param builder
* @return 实体管理工厂
* packages 扫描@Entity注释的软件包名称
* persistenceUnit 持久性单元的名称。 如果只建立一个EntityManagerFactory,你可以省略这个,
* 但是如果在同一个应用程序中有多个,你应该给它们不同的名字
* properties 标准JPA或供应商特定配置的通用属性。 这些属性覆盖构造函数中提供的任何值。
*/
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(datasource)
.properties(getVendorProperties())
.packages("com.entities.test1Entities")//entitie对应package
.persistenceUnit("test1PersistenceUnit")
.build();
}
/**
* 配置EntityManager实体
* @param builder
* @return 实体管理器
*/
@Primary
@Bean(name = "entityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactory(builder).getObject().createEntityManager();
}
/**
* 配置事务transactionManager
* @param builder
* @return 事务管理器
*/
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactory(builder).getObject());
}
}