Spring boot配置多数据源代码实例

网友投稿 277 2022-12-01


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

通用数据源配置

import com.alibaba.druid.pool.DruidDataSource;

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;

import javax.sql.DataSource;

/**

* @author ruanshuai

* @date 2020/5/13

*/

@Configuration

public class DataSourceConfig {

/**

* 第一个数据连接,默认优先级最高

* @return

*/

@Primary

@Bean(name = "primaryDataSource") //数据源1配置名

@Qualifier("primaryDataSource") //数据源1配置名

@ConfigurationProperties(prefix="spring.datasource.primary") //见配置文件

public DataSource PrimaryDataSource() {

return DataSourceBuilder.create().type(DruidDataSource.class).build();

}

/**

* 第二个数据源

* @return

*/

@Bean(name = "secondDataSource") //数据源2配置名

@Qualifier("secondDataSource") //数据源2配置名

@ConfigurationProperties(prefix="spring.datasource.second") //见配置文件

public DataSource secondaryDataSource() {

return DataSourceBuilder.create().type(DruidDataSource.class).build();

}

}

数据源1配置

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

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.HashMap;

import java.util.Map;

/**

* @author ruanshuai

* @date 2020/5/13

*/

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(

entityManagerFactoryRef="entityManagerFactoryPrimary",

transactionManagerRef="transactionManagerPrimary",

basePackages= { "***此处为数据源1 repository的存放文件夹***" })

public class PrimaryConfig {

@Autowired

@Qualifier("primaryDataSource")

private DataSource primaryDataSource;

@Primary

@Bean(name = "entityManagerPrimaiTeEPYgVry")

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())

.packages("***实体类所在文件夹,两个数据源的实体类可相同***")

.persistenceUnit("primaryPersistenceUnit")

.build();

}

private Map getVendorProperties() {

Map jpaProperties = new HashMap<>(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.dialecthttp://"));

jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");

return jpaProperties;

}

@Primary

@Bean(name = "transactionManagerPrimary")

public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {

return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());

}

}

数据源2配置

import org.omg.CORBA.Environment;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

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.HashMap;

import java.util.Map;

/**

* @author ruanshuai

* @date 2020/5/13

*/

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(

//实体管理

entityManagerFactoryRef="entityManagerFactorySecond",

//事务管理

transactionManagerRef="transactionManagerSecond",

//实体扫描,设置Repository所在位置

basePackages= { "***此处为数据源1 repository的存放文件夹***" })

public class SecondConfig {

@Autowired

@Qualifier("secondDataSource")

private DataSource secondDataSource;

@Bean(name = "entityManagerSecond")

public EntityManager entityManager(EntityManagerFactoryBuilder builder) {

return entityManagerFactorySecond(builder).getObject().createEntityManager();

}

@Bean(name = "entityManagerFactorySecond")

public LocalContainerEntityManagerFactoryBean entityManagerFactorySecond (EntityManagerFactoryBuilder builder) {

return builder

.dataSource(secondDataSource)

.properties(getVendorProperties())

.packages("***实体类所在文件夹,两个数据源的实体类可相同***")

.persistenceUnit("secondPersistenceUnit")

.build();

}

private Map&lhttp://t;String, String> getVendorProperties() {

Map jpaProperties = new HashMap<>(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");

return jpaProperties;

}

@Bean(name = "transactionManagerSecond")

PlatformTransactionManager transactionManagerSecond(EntityManagerFactoryBuilder builder) {

return new JpaTransactionManager(entityManagerFactorySecond(builder).getObject());

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java和Python现在都挺火,我应该怎么选?
下一篇:Spring @CrossOrigin 注解原理实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~