码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot,Spring Data JPA多数据源支持配置

时间:2016-05-31 12:16:22      阅读:602      评论:0      收藏:0      [点我收藏+]

标签:

1 配置文件

wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver
wisely.primary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1521\:xe
wisely.primary.datasource.username=gis
wisely.primary.datasource.password=gis
 
wisely.secondary.datasource.driverClassName=oracle.jdbc.OracleDriver
wisely.secondary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1522\:xe
wisely.secondary.datasource.username=gis
wisely.secondary.datasource.password=gis

2 datasource配置

第一个数据源

@Configuration
public class DataSourcePrimaryConfig {

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

}

第二个数据源

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

3 实体管理器及事务管理器配置

第一个数据源

 

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;

 

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",transactionManagerRef="transactionManagerPrimary",basePackages= { "com.wisely.demo.dao.one" })/ /设置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.wisely.demo.domain.one") //设置实体类所在位置
                .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());
    }

}

第二个数据源

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary",transactionManagerRef="transactionManagerSecondary",basePackages= { "com.wisely.demo.dao.two" })
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.wisely.demo.domain.two")
                .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());
    }

}

4 使用

此时来自不同数据库的dao(repo)可以任意在其它的bean里注入

@Controller
public class TestController {
  @Autowired
  SysRoleRepo1 sysRoleRepo1;
  @Autowired
  SysRoleRepo2 sysRoleRepo2;
  @RequestMapping("/test")
  public @ResponseBody String test(){
    System.out.println(Lists.newArrayList(sysRoleRepo1.findAll()).size());
    System.out.println(Lists.newArrayList(sysRoleRepo2.findAll()).size());
    return "ok";
  } 
}

Spring Boot,Spring Data JPA多数据源支持配置

标签:

原文地址:http://www.cnblogs.com/stronghan/p/5545080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!