SpringBoot 配置多数据源 ,基于JPA 操作Oracle数据库(Oracle版本)

SpringBoot 配置多数据源 ,基于JPA 操作Oracle数据库(Oracle版本)

一个工程的操作需要连接两个数据库

当时看了很多博客,当时看到网上很多版本都是跟配置有关,但是很多没有介绍具体使用的方法,下面总结一波

1 :在application.properties中配置

spring.datasource.primary.jdbc-url=jdbc:oracle:thin:@连接数据库的连接串(我当时两个数据库连接池的连接串一样,但是用户名是不同的)(这是第一个数据库)
spring.datasource.primary.username=用户名
spring.datasource.primary.password=密码
spring.datasource.primary.driver-class-name=oracle.jdbc.OracleDriver,
#
#(和配置一个数据源的配置文件有区别,记得进行比较着改或者直接黏贴我的代码)
spring.datasource.secondary.jdbc-url=jdbc:oracle:thin:连接数据库的连接串(这是第二个数据库)
spring.datasource.secondary.username=用户名
spring.datasource.secondary.password=密码
spring.datasource.secondary.driver-class-name=oracle.jdbc.OracleDriver
#oracle

SpringBoot 配置多数据源 ,基于JPA 操作Oracle数据库(Oracle版本)_第1张图片

2. 添加数据源配置文件

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.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
    @Primary
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties("spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties("spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

3 .分别对两个数据源添加配置文件

其中只要改下面代码的两个地方

第一个地方: basePackages= { “com.**.dao” }) //设置Repository所在位置 dao 文件的路径

​ 第二个地方 factoryBean.setPackagesToScan(“com.**entity”); //jpa对应的实体的位置

/**第一个数据源对应的配置文件
**/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= { "com.**.dao" }) //设置Repository所在位置  dao 文件的路径
public class PrimaryConfig {

    @Primary
    @Bean
    PlatformTransactionManager transactionManagerPrimary() {
        return new JpaTransactionManager(entityManagerPrimary().getObject());
    }

    @Primary
    @Bean(name = "entityManagerPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerPrimary() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.ORACLE);(其他根据需要自行配置)
        vendorAdapter.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(primaryDataSource);
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setPackagesToScan("com.**entity"); //jpa对应的实体的位置
        return factoryBean;
    }

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


}
/**第二个数据源的配置文件**/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerSecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= { "com.**.repository" }) //设置Repository所在位置
public class SecondaryConfig {

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


    @Bean
    PlatformTransactionManager transactionManagerSecondary() {
        return new JpaTransactionManager(entityManagerSecondary().getObject());
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerSecondary() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.ORACLE);
        vendorAdapter.setShowSql(true);


        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(secondaryDataSource);
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setPackagesToScan("com.**.entity2");
        return factoryBean;
    }
}

经过上面的配置 只要把相应的类放到到对应的包中,SpringBoot就会自动扫描,并且把两个数据源映射到响应的包上面

SpringBoot 配置多数据源 ,基于JPA 操作Oracle数据库(Oracle版本)_第2张图片

点个赞,再走亲

你可能感兴趣的:(工作总结,mysql,java,多数源)