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";
}
}

Spring Boot,Spring Data JPA多数据源支持的更多相关文章

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

    1 配置文件 wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver wisely.primary.datasource. ...

  2. spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务

    文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...

  3. Springboot spring data jpa 多数据源的配置01

    Springboot spring data jpa 多数据源的配置 (说明:这只是引入了多个数据源,他们各自管理各自的事务,并没有实现统一的事务控制) 例: user数据库   global 数据库 ...

  4. 使用spring boot中的JPA操作数据库

    前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...

  5. Spring Boot 2.5.0 发布:支持Java16、Gradle 7、Datasource初始化机制调整

    今年520的事情是真的多,娱乐圈的我们不管,就跟DD一起来看看 Spring Boot 2.5.0 的发布吧!看看都带来了哪些振奋人心的新特性和改动! 主要更新 支持 Java 16 支持 Gradl ...

  6. 初识在Spring Boot中使用JPA

    前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...

  7. Spring Boot HikariCP 一 ——集成多数据源

    其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...

  8. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  9. Spring Boot 2.x Redis多数据源配置(jedis,lettuce)

    Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数 ...

随机推荐

  1. java-finalize

    finalize()来自Object的protected 方法,同clone()需要用户具体实现.一.源码中的介绍1.    Called by the garbage collector on an ...

  2. 使用sqlite保存数据返回主键

    /// <summary> /// 返回insert后的主键值 /// </summary> /// <param name="SQLString"& ...

  3. Git学习笔记总结和注意事项

    一.Git简单介绍 Git是眼下世界上最先进的分布式版本号控制系统.其特点简单来说就是:高端大气上档次! 二.Windows上Git安装 最早Git是在Linux上开发的.非常长一段时间内.Git也仅 ...

  4. typedef 总结

    其实在正儿八经学C语言的时候typedef用的不是很多,记得书上对它的介绍只是一笔带过.的确它的用法是很简单,但这不代表在使用的过程中不会出错,今天来个彻底的总结. 作用:用来建立新的数据类型名.(注 ...

  5. 知识点1-1:什么是ASP.NET MVC

    ASP.NET MVC是微软.NET平台上的一个Web开发框架,它为开发者提供了一种构建结构良好的Web应用程序的方式.自2007年首次公布预览以来,作为Web Form的替代品,ASP.NET MV ...

  6. NET Core 以及与 .NET Framework

    简析.NET Core 以及与 .NET Framework的关系 简析.NET Core 以及与 .NET Framework的关系 一 .NET 的 Framework 们 二 .NET Core ...

  7. Homebrew 1.0.0 发布,MacOS 上的包管理器

    神器,没有它不知道怎么用macos https://www.oschina.net/news/77367/homebrew-1-0-0

  8. jQuery手写几个常见的滑动下拉菜单 分分秒秒学习JS

    一般的企业网站再我们再实际工作中,有些特效,用jQuery来做,显得极其简单,除非一些大的公司,需要封装自己的类. 今天,我们讲解jQuery入门知识,来写几个简单jQuery滑动下拉菜单.感受一下j ...

  9. mysql basic operation,mysql总结

    mysql> select * from wifi_data where dev_id like "0023-AABBCCCCBBAA" ; 1.显示数据库列表.show d ...

  10. Python:渗透测试开源项目

    Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...