spring boot 2.0 jpa多数据源配置

spring boot 1.x内多数据源配置方式参考Spring Boot多数据源配置与使用,但升级到2后该配置无效,以下是自测可行的配置方式(spring boot版本2.0.6.RELEASE)。

1. application.properties配置

#数据源1
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driverClassName = com.mysql.jdbc.Driver

#数据源2
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driverClassName = com.mysql.jdbc.Driver

2.第一数据源配置

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "primaryEntityManagerFactory",
        transactionManagerRef = "primaryTransactionManager",
        // 此处指定第一数据源对于dao包路径
        basePackages = "com.onecodespace.codegenerator.business.dao")
public class PrimaryConfig {

    @Bean
    PlatformTransactionManager primaryTransactionManager() {
        return new JpaTransactionManager(primaryEntityManagerFactory().getObject());
    }

    @Bean
    LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(primaryDataSource());
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        // 此处指定第一数据源对应实体类包路径
        factoryBean.setPackagesToScan("com.onecoderspace.codegenerator.business.domain");
        return factoryBean;
    }

//  @Bean
//  @ConfigurationProperties(prefix = "spring.datasource.primary")
//  DataSource primaryDataSource() {
//      return new EmbeddedDatabaseBuilder().
//              setType(EmbeddedDatabaseType.H2).
//              build();
//  }

    @Bean
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        //通过DataSourceBuilder构建数据源
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }
}

3. 第二数据源配置

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "secondaryEntityManagerFactory",
        transactionManagerRef = "secondaryTransactionManager",
        // 第二数据源对应dao包路径
        basePackages = "com.onecodespace.codegenerator.schema.dao")
public class SecondaryConfig extends DataSourceAutoConfiguration {

    @Bean
    PlatformTransactionManager secondaryTransactionManager() {
        return new JpaTransactionManager(secondaryEntityManagerFactory().getObject());
    }

    @Bean
    LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(secondaryDataSource());
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        // 第二数据源对应实体的包路径
        factoryBean.setPackagesToScan("com.onecodespace.codegenerator.schema.domain");
        return factoryBean;
    }

    @Bean
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        //通过DataSourceBuilder构建数据源
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

         // 如果使用jdbcTemplate,进行如下设置即可
    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("secondaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

在两个数据源对应包下创建实体、dao、service,运行系统测试,可看到实体对应表分别在两个库内操作。

你可能感兴趣的:(spring boot 2.0 jpa多数据源配置)