springboot 2.x jpa 多数据源的配置

springboot2.x jpa 多数据源的配置相比1.x有一点不同,下面提供一种2.x的jpa 多数据源的配置

1.配置类

PrimaryConfig


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Map;

/**
 * @author 75412
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= {
    
     "com.gbcom.facesystem.migratingdata.dao.ics" }) //设置Repository所在位置
public class PrimaryConfig {
    
    

    @Autowired
    private HibernateProperties hibernateProperties;

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
    
    
        return hibernateProperties.determineHibernateProperties(
                jpaProperties.getProperties(), new HibernateSettings()
        );
    }

    @Primary
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary") // 配置数据源获取的涞源
    public DataSource primaryDataSource(){
    
    
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {
    
    
        return builder.dataSource(dataSource)
                .properties(getVendorProperties())
                .packages("com.gbcom.facesystem.migratingdata.dao.ics")
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager propertyTransactionManager(
            @Qualifier("entityManagerFactoryPrimary") EntityManagerFactory propertyEntityManagerFactory) {
    
    
        return new JpaTransactionManager(propertyEntityManagerFactory);
    }

}

SecondaryConfig


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Map;


/**
 * @author 75412
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= {
    
     "com.gbcom.facesystem.migratingdata.dao.idic"}) //设置Repository所在位置
public class SecondaryConfig {
    
    

    @Autowired
    private HibernateProperties hibernateProperties;

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
    
    
        Map<String, Object> map = hibernateProperties.determineHibernateProperties(
                jpaProperties.getProperties(), new HibernateSettings());return map;
    }

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

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder, @Qualifier("secondDataSource") DataSource dataSource) {
    
    
        return builder.dataSource(dataSource)
                .properties(getVendorProperties())
                .packages("com.gbcom.facesystem.migratingdata.dao.idic")
                .persistenceUnit("targetPersistenceUnit")
                .build();
    }

    @Bean(name = "transactionManagerSecondary")
    public PlatformTransactionManager propertyTransactionManager(
            @Qualifier("entityManagerFactorySecondary") EntityManagerFactory propertyEntityManagerFactory) {
    
    
        return new JpaTransactionManager(propertyEntityManagerFactory);
    }

}

有几个地方需要说明一下
在这里插入图片描述
配置文件文件application.yml

spring:
  application:
    name: xxxxxxxxx
  datasource:
    primary:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://ip:port/mysqlBase?useUnicode=true&characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8
      username: xxxxxx
      password: xxxxxx
    secondary:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://ip:port/mysqlBase?useUnicode=true&characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8
      username: xxxxxx
      password: xxxxxx

一定要去jdbc-url,否则可能报错
在这里插入图片描述

Guess you like

Origin blog.csdn.net/haohao_ding/article/details/111831095