Spring Boot 多数据源配置

DataSourceConfig.java

package xin.lowang.jpatest.config;

import javax.sql.DataSource;

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;

@Configuration
public class DataSourceConfig {
  /**
   * 第一数据源
   *
   * @return
   */
  @Bean("test1ds")
  @Qualifier("test1ds")
  @Primary
  @ConfigurationProperties(prefix = "spring.datasource")
  public DataSource test1() {
    return DataSourceBuilder.create().build();
  }
  /**
   * 第二数据源
   *
   * @return
   */
  @Bean("test2ds")
  @Qualifier("test2ds")
  @ConfigurationProperties(prefix = "spring.datasource2")
  public DataSource test2() {
    return DataSourceBuilder.create().build();
  }
}

Test1Config.java

package xin.lowang.jpatest.config;

import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
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;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
  entityManagerFactoryRef = "test1EntityManagerFactory",
  transactionManagerRef = "test1TransactionManager",
  basePackages = {"xin.lowang.jpatest.test1"}
)
public class Test1Config {
  @Resource(name = "test1ds")
  private DataSource dataSource;

  @Autowired private JpaProperties jpaProperties;

  @Primary
  @Bean(name = "test1EntityManager")
  public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return mytestEntityManagerFactory(builder).getObject().createEntityManager();
  }

  @Primary
  @Bean(name = "test1EntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean mytestEntityManagerFactory(
      EntityManagerFactoryBuilder builder) {
    return builder
        .dataSource(dataSource)
        .properties(getVendorProperties(dataSource))
        .packages("xin.lowang.jpatest.test1")
        .persistenceUnit("test1")
        .build();
  }

  private Map getVendorProperties(DataSource dataSource) {
    return jpaProperties.getHibernateProperties(dataSource);
  }

  @Bean(name = "test1TransactionManager")
  public PlatformTransactionManager mytestTransactionManager(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(mytestEntityManagerFactory(builder).getObject());
  }
}

Test2Config.java

package xin.lowang.jpatest.config;

import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
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.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;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
  entityManagerFactoryRef = "test2EntityManagerFactory",
  transactionManagerRef = "test2TransactionManager",
  basePackages = {"xin.lowang.jpatest.test2"}
)
public class Test2Config {
  @Resource(name = "test2ds")
  private DataSource dataSource;

  @Autowired private JpaProperties jpaProperties;

  @Bean(name = "test2EntityManager")
  public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return mytestEntityManagerFactory(builder).getObject().createEntityManager();
  }

  @Bean(name = "test2EntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean mytestEntityManagerFactory(
      EntityManagerFactoryBuilder builder) {
    return builder
        .dataSource(dataSource)
        .properties(getVendorProperties(dataSource))
        .packages("xin.lowang.jpatest")
        .persistenceUnit("test2")
        .build();
  }

  private Map getVendorProperties(DataSource dataSource) {
    return jpaProperties.getHibernateProperties(dataSource);
  }

  @Bean(name = "test2TransactionManager")
  public PlatformTransactionManager mytestTransactionManager(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(mytestEntityManagerFactory(builder).getObject());
  }
}

在多数据源的环境下,如果需要开启事务,需要指定事务管理器的名称,不然系统会走动使用@Primary 注解的事务管理器。@Transactional(transactionManager="test2TransactionManager")

你可能感兴趣的:(Spring Boot 多数据源配置)