springboot JPA 多数据源配置方式(一)静态方式

2019-03-27  本文已影响0人  TheUnforgiven

静态方式的原理就是创建多个entityManager和transactionManager对象,各自管理所对应的实体类和对应的repository仓储

1. 数据源配置

#主
spring.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/master?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#从
spring.slave.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/kouwei?useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.slave.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.slave.datasource.username=root
spring.slave.datasource.password=root

2.读取数据源

@Configuration
public class DataSourceConfig {

    @Bean("master")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("slave")
    @ConfigurationProperties(prefix = "spring.slave.datasource")
    public DataSource dataSourceSlave(){
        return DataSourceBuilder.create().build();
    }
}

3.主从数据源的 EntityManager,TransactionManager配置

主数据源(加上@Primary注解)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryMaster",//配置连接工厂 entityManagerFactory
        transactionManagerRef = "transactionManagerMaster", //配置 事物管理器  transactionManager
        basePackages = {"com.app.user.repository.master"}
)
public class MasterDataSourceConfig {
    @Autowired
    @Qualifier("master")
    private DataSource dataSourceMaster;
    @Autowired
    private JpaProperties jpaProperties;

    @Bean("entityManagerMaster")
    @Primary
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryBean(builder).getObject().createEntityManager();
    }

    @Bean("entityManagerFactoryMaster")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(dataSourceMaster)
                .properties(jpaProperties.getProperties())
                .packages("com.app.user.domain")
                //持久化单元名称,当存在多个EntityManagerFactory时,需要制定此名称
                .persistenceUnit("masterPersistenceUnit")
                .build();
    }

    @Bean("transactionManagerMaster")
    @Primary
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryBean(builder).getObject());
    }
}
从数据源
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactorySlave",
        transactionManagerRef = "transactionManagerSlave",
        basePackages = {"com.app.user.repository.slave"}
)
public class SlaveDataSourceConfig {
    @Autowired
    @Qualifier("slave")
    private DataSource dataSource;
    @Autowired
    private JpaProperties jpaProperties;

    @Bean("entityManagerFactorySlave")
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(dataSource)
                .properties(jpaProperties.getProperties())
                .packages("com.app.user.domain")
                .build();
    }

    @Bean("entityManagerSlave")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return localContainerEntityManagerFactoryBean(builder).getObject().createEntityManager();
    }

    @Bean("transactionManagerSlave")
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(localContainerEntityManagerFactoryBean(builder).getObject());
    }
}

注意事项:

启动类不要加Entity和Repository的扫描,会使主从配置失效。

上一篇下一篇

猜你喜欢

热点阅读