Spring boot配置多数据源代码实例
因项目需要在一个应用里从两个数据库取数,所以需要配置多数据源,网上找了好多方法才启动成功,整理如下。注意两个数据源的repository文件名不能相同,即使在不同的文件夹下,否则系统启动会报错。
配置文件
spring.datasource.primary.url=*** spring.datasource.primary.username=*** spring.datasource.primary.password=*** spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.second.url=*** spring.datasource.second.username=*** spring.datasource.second.password=*** spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver
通用数据源配置
importcom.alibaba.druid.pool.DruidDataSource;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.Primary;
importjavax.sql.DataSource;
/**
*@authorruanshuai
*@date2020/5/13
*/
@Configuration
publicclassDataSourceConfig{
/**
*第一个数据连接,默认优先级最高
*@return
*/
@Primary
@Bean(name="primaryDataSource")//数据源1配置名
@Qualifier("primaryDataSource")//数据源1配置名
@ConfigurationProperties(prefix="spring.datasource.primary")//见配置文件
publicDataSourcePrimaryDataSource(){
returnDataSourceBuilder.create().type(DruidDataSource.class).build();
}
/**
*第二个数据源
*@return
*/
@Bean(name="secondDataSource")//数据源2配置名
@Qualifier("secondDataSource")//数据源2配置名
@ConfigurationProperties(prefix="spring.datasource.second")//见配置文件
publicDataSourcesecondaryDataSource(){
returnDataSourceBuilder.create().type(DruidDataSource.class).build();
}
}
数据源1配置
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.Primary;
importorg.springframework.data.jpa.repository.config.EnableJpaRepositories;
importorg.springframework.orm.jpa.JpaTransactionManager;
importorg.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
importorg.springframework.transaction.PlatformTransactionManager;
importorg.springframework.transaction.annotation.EnableTransactionManagement;
importjavax.persistence.EntityManager;
importjavax.sql.DataSource;
importjava.util.HashMap;
importjava.util.Map;
/**
*@authorruanshuai
*@date2020/5/13
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages={"***此处为数据源1repository的存放文件夹***"})
publicclassPrimaryConfig{
@Autowired
@Qualifier("primaryDataSource")
privateDataSourceprimaryDataSource;
@Primary
@Bean(name="entityManagerPrimary")
publicEntityManagerentityManager(EntityManagerFactoryBuilderbuilder){
returnentityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name="entityManagerFactoryPrimary")
publicLocalContainerEntityManagerFactoryBeanentityManagerFactoryPrimary(EntityManagerFactoryBuilderbuilder){
returnbuilder
.dataSource(primaryDataSource)
.properties(getVendorProperties())
.packages("***实体类所在文件夹,两个数据源的实体类可相同***")
.persistenceUnit("primaryPersistenceUnit")
.build();
}
privateMapgetVendorProperties(){
MapjpaProperties=newHashMap<>(16);
jpaProperties.put("hibernate.hbm2ddl.auto","update");
jpaProperties.put("hibernate.show_sql",System.getProperty("spring.jpa.show-sql"));
jpaProperties.put("hibernate.dialect",System.getProperty("spring.jpa.properties.hibernate.dialect"));
jpaProperties.put("hibernate.current_session_context_class","org.springframework.orm.hibernate5.SpringSessionContext");
returnjpaProperties;
}
@Primary
@Bean(name="transactionManagerPrimary")
publicPlatformTransactionManagertransactionManagerPrimary(EntityManagerFactoryBuilderbuilder){
returnnewJpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
数据源2配置
importorg.omg.CORBA.Environment;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.Primary;
importorg.springframework.data.jpa.repository.config.EnableJpaRepositories;
importorg.springframework.orm.jpa.JpaTransactionManager;
importorg.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
importorg.springframework.transaction.PlatformTransactionManager;
importorg.springframework.transaction.annotation.EnableTransactionManagement;
importjavax.persistence.EntityManager;
importjavax.sql.DataSource;
importjava.util.HashMap;
importjava.util.Map;
/**
*@authorruanshuai
*@date2020/5/13
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
//实体管理
entityManagerFactoryRef="entityManagerFactorySecond",
//事务管理
transactionManagerRef="transactionManagerSecond",
//实体扫描,设置Repository所在位置
basePackages={"***此处为数据源1repository的存放文件夹***"})
publicclassSecondConfig{
@Autowired
@Qualifier("secondDataSource")
privateDataSourcesecondDataSource;
@Bean(name="entityManagerSecond")
publicEntityManagerentityManager(EntityManagerFactoryBuilderbuilder){
returnentityManagerFactorySecond(builder).getObject().createEntityManager();
}
@Bean(name="entityManagerFactorySecond")
publicLocalContainerEntityManagerFactoryBeanentityManagerFactorySecond(EntityManagerFactoryBuilderbuilder){
returnbuilder
.dataSource(secondDataSource)
.properties(getVendorProperties())
.packages("***实体类所在文件夹,两个数据源的实体类可相同***")
.persistenceUnit("secondPersistenceUnit")
.build();
}
privateMapgetVendorProperties(){
MapjpaProperties=newHashMap<>(16);
jpaProperties.put("hibernate.hbm2ddl.auto","update");
jpaProperties.put("hibernate.show_sql",System.getProperty("spring.jpa.show-sql"));
jpaProperties.put("hibernate.dialect",System.getProperty("spring.jpa.properties.hibernate.dialect"));
jpaProperties.put("hibernate.current_session_context_class","org.springframework.orm.hibernate5.SpringSessionContext");
returnjpaProperties;
}
@Bean(name="transactionManagerSecond")
PlatformTransactionManagertransactionManagerSecond(EntityManagerFactoryBuilderbuilder){
returnnewJpaTransactionManager(entityManagerFactorySecond(builder).getObject());
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。