Spring Boot,Spring Data JPA多数据源支持
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 实体管理器及事务管理器配置
第一个数据源
@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"; } }
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。
相关推荐
吾日五省我身 2020-05-29
csuzxm000 2020-08-02
幸运小侯子 2020-07-06
mituan 2020-05-26
zhangdy0 2020-05-25
吾日五省我身 2020-05-12
whbing 2020-04-30
琦小虾的代码世界 2020-03-13
也许不会看见 2020-01-29
zxznsjdsj 2020-01-18
吾日五省我身 2020-01-07
burning 2014-05-28
whbing 2019-12-19
flydoging 2019-12-05
whileinsist 2019-11-19
zhangbeizhen 2019-11-16
lysanderK 2019-11-08
congcongsuiyue 2016-08-19
数据分析杂谈 2016-12-13