Spring Boot + Spring Data JPA项目配置多数据源

版权声明:http://blog.csdn.net/anxpp https://blog.csdn.net/anxpp/article/details/52274120

原文出自:http://blog.csdn.net/anxpp/article/details/52274120,转载请注明出处,谢谢!


    在以前的常规项目中,直接XML配置文件中配置多个数据源即可,在最新推荐的做法中,我们使用配置类来设置。

首先配置两个数据源:

package com.anxpp.web.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 {

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

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

}

    @Primary 的意思是默认实现。

然后分别配置两个数据源的详细信息。

    配置一:

package com.anxpp.web.config;

import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;
/**
 * 开发环境数据库数据源配置
 * @author 
 *
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
		entityManagerFactoryRef="entityManagerFactoryPrimary",
		transactionManagerRef="transactionManagerPrimary",
		basePackages= { "com.anxpp.web.core.repo.work" })//设置dao(repo)所在位置
public class RepositoryPrimaryConfig {
	
    @Autowired
    private JpaProperties jpaProperties;

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

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

    @Bean(name = "entityManagerFactoryPrimary")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDS)
                .properties(getVendorProperties(primaryDS))
                .packages("com.anxpp.web.core.entity.po") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerPrimary")
    @Primary
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }

}
    配置二:
package com.anxpp.web.config;
import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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="entityManagerFactorySecondary",
		transactionManagerRef="transactionManagerSecondary",
		basePackages= { "com.anxpp.web.core.repo.dev" })
public class RepositorySecondaryConfig {
    @Autowired
    private JpaProperties jpaProperties;

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

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

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDS)
                .properties(getVendorProperties(secondaryDS))
                .packages("com.anxpp.web.core.entity.po")
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}
    上文中 com.anxpp.web.core.repo.dev 表示数据访问接口的包,其中的repo访问数据库时,会自动使用RepositorySecondaryConfig。

    实体也类似。

数据库连接配置:

#Work
spring.primary.datasource.url=jdbc:oracle:thin:@//ip:port/dbname
spring.primary.datasource.username=username
spring.primary.datasource.password=password
spring.primary.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#Development
spring.secondary.datasource.url=jdbc:oracle:thin:@//ip:port/dbname
spring.secondary.datasource.username=username
spring.secondary.datasource.password=username
spring.secondary.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#multiple Setting
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
    然后就可以直接使用了。


配置完成

    当然,也可以配置更多数据源。

    本人需要这种需求是因为两个数据库,需要一些细粒度的数据对比同步,在Java代码中更容易操作,所以就使用两个数据源的Java代码来完成了。

阅读更多

没有更多推荐了,返回首页