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

1 配置文件

 

 1 wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver
 2 wisely.primary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1521\:xe
 3 wisely.primary.datasource.username=gis
 4 wisely.primary.datasource.password=gis
 5  
 6 wisely.secondary.datasource.driverClassName=oracle.jdbc.OracleDriver
 7 wisely.secondary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1522\:xe
 8 wisely.secondary.datasource.username=gis
 9 wisely.secondary.datasource.password=gis
10 
11 spring.jpa.generate-ddl=true
12 spring.jpa.show-sql=true
13 spring.jpa.auto-ddl=update

 

 

 

2 datasource配置

第一个数据源

 1 import javax.sql.DataSource;
 2 
 3 import org.springframework.beans.factory.annotation.Qualifier;
 4 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
 5 import org.springframework.boot.context.properties.ConfigurationProperties;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.Configuration;
 8 import org.springframework.context.annotation.Primary;
 9 
10 @Configuration
11 public class DataSourcePrimaryConfig {
12 
13   @Bean(name = "primaryDS") @Qualifier("primaryDS")
14   @Primary
15   @ConfigurationProperties(prefix="wisely.primary.datasource")
16   public DataSource primaryDataSource(){
17     return DataSourceBuilder.create().build();
18   }
19 
20 }

 

第二个数据源

 1 import javax.sql.DataSource;
 2 
 3 import org.springframework.beans.factory.annotation.Qualifier;
 4 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
 5 import org.springframework.boot.context.properties.ConfigurationProperties;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.Configuration;
 8 import org.springframework.context.annotation.Primary;
 9 
10 @Configuration
11 public class DataSourceSecondaryConfig {
12   @Bean(name = "secondaryDS") @Qualifier("secondaryDS")
13   @ConfigurationProperties(prefix="wisely.secondary.datasource")
14   public DataSource secondaryDataSource(){
15     return DataSourceBuilder.create().build();
16   }
17 }

 

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

第一个数据源

 

 1 import java.util.Map;
 2 
 3  
 4 
 5 import javax.persistence.EntityManager;
 6 import javax.sql.DataSource;
 7 
 8  
 9 
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.beans.factory.annotation.Qualifier;
12 import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
13 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Configuration;
16 import org.springframework.context.annotation.Primary;
17 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
18 import org.springframework.orm.jpa.JpaTransactionManager;
19 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
20 import org.springframework.transaction.PlatformTransactionManager;
21 import org.springframework.transaction.annotation.EnableTransactionManagement;
22 
23  
24 
25 @Configuration
26 @EnableTransactionManagement
27 @EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",transactionManagerRef="transactionManagerPrimary",basePackages= { "com.wisely.demo.dao.one" })/ /设置dao(repo)所在位置
28 public class RepositoryPrimaryConfig {
29     @Autowired
30     private JpaProperties jpaProperties;
31 
32     @Autowired @Qualifier("primaryDS")
33     private DataSource primaryDS;
34 
35     @Bean(name = "entityManagerPrimary")
36     @Primary
37     public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
38         return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
39     }
40 
41     @Bean(name = "entityManagerFactoryPrimary")
42     @Primary
43     public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
44         return builder
45                 .dataSource(primaryDS)
46                 .properties(getVendorProperties(primaryDS))
47                 .packages("com.wisely.demo.domain.one") //设置实体类所在位置
48                 .persistenceUnit("primaryPersistenceUnit")
49                 .build();
50     }
51 
52     private Map<String, String> getVendorProperties(DataSource dataSource) {
53         return jpaProperties.getHibernateProperties(dataSource);
54     }
55 
56     @Bean(name = "transactionManagerPrimary")
57     @Primary
58     PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
59         return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
60     }
61 
62 }

 

第二个数据源

 1 import javax.sql.DataSource;
 2 import org.springframework.beans.factory.annotation.Qualifier;
 3 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
 4 import org.springframework.boot.context.properties.ConfigurationProperties;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.context.annotation.Primary;
 8 
 9 @Configuration
10 @EnableTransactionManagement
11 @EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary",transactionManagerRef="transactionManagerSecondary",basePackages= { "com.wisely.demo.dao.two" })
12 public class RepositorySecondaryConfig {
13     @Autowired
14     private JpaProperties jpaProperties;
15 
16     @Autowired @Qualifier("secondaryDS")
17     private DataSource secondaryDS;
18 
19     @Bean(name = "entityManagerSecondary")
20     public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
21         return entityManagerFactorySecondary(builder).getObject().createEntityManager();
22     }
23 
24     @Bean(name = "entityManagerFactorySecondary")
25     public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
26         return builder
27                 .dataSource(secondaryDS)
28                 .properties(getVendorProperties(secondaryDS))
29                 .packages("com.wisely.demo.domain.two")
30                 .persistenceUnit("secondaryPersistenceUnit")
31                 .build();
32     }
33 
34     private Map<String, String> getVendorProperties(DataSource dataSource) {
35         return jpaProperties.getHibernateProperties(dataSource);
36     }
37 
38     @Bean(name = "transactionManagerSecondary")
39     PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
40         return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
41     }
42 
43 }

 

4 使用

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

 1 @Controller
 2 public class TestController {
 3   @Autowired
 4   SysRoleRepo1 sysRoleRepo1;
 5   @Autowired
 6   SysRoleRepo2 sysRoleRepo2;
 7   @RequestMapping("/test")
 8   public @ResponseBody String test(){
 9     System.out.println(Lists.newArrayList(sysRoleRepo1.findAll()).size());
10     System.out.println(Lists.newArrayList(sysRoleRepo2.findAll()).size());
11     return "ok";
12   } 
13 }

 

5 ENTITY

 1 import javax.persistence.Entity;
 2 import javax.persistence.Table;
 3 import javax.persistence.GeneratedValue;
 4 import javax.persistence.ID;
 5 
 6 @Entity
 7 @Table(name = "employee")
 8 public class Employee {
 9 
10     private @Id @GeneratedValue Long id;
11     private String firstName, lastName, description;
12 
13     private Employee() {}
14 
15     public Employee(String firstName, String lastName, String description) {
16         this.firstName = firstName;
17         this.lastName = lastName;
18         this.description = description;
19     }
20 }

 

6.Respority

1 import org.springframework.data.repository.CrudRepository;
2 
3 public interface EmployeeRepository extends CrudRepository<Employee, Long> {
4 
5     Employee findByFirstName(String firstName);
6 
7     List<Employee> findByLastName(String lastName);
8 }

 

posted @ 2016-05-31 10:40  StrongHan  阅读(13235)  评论(2编辑  收藏  举报