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

首先配置两个数据源:

package com.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.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 bbaiggey**/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",transactionManagerRef="transactionManagerPrimary",basePackages= { "com.anxpp.web.core.repo.work" })//设置dao(repo)所在位置
public class RepositoryPrimaryConfig {@Autowiredprivate JpaProperties jpaProperties;@Autowired@Qualifier("primaryDS")private DataSource primaryDS;@Bean(name = "entityManagerPrimary")@Primarypublic EntityManager entityManager(EntityManagerFactoryBuilder builder) {return entityManagerFactoryPrimary(builder).getObject().createEntityManager();}@Bean(name = "entityManagerFactoryPrimary")@Primarypublic 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")@PrimaryPlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());}}

配置二:

package com.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 {@Autowiredprivate 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..web.core.repo.dev 表示数据访问接口的包,其中的repo访问数据库时,会自动使用RepositorySecondaryConfig。
实体也类似。

数据库连接配置:

#Work
spring.primary.datasource.url=jdbc:oracle:thin:@//ip:port/db1
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/db2
spring.secondary.datasource.username=username
spring.secondary.datasource.password=password
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代码来完成了。

Spring Boot + Spring Data JPA项目配置多数据源相关推荐

  1. Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos

    2019独角兽企业重金招聘Python工程师标准>>> A couple of weeks ago I was evaluating the possibility to use S ...

  2. springmvc jpa_使用JavaConfig的SpringMVC4 + Spring Data JPA + SpringSecurity配置

    springmvc jpa 在本文中,我们将看到如何使用JavaConfig配置和集成SpringMVC4,带有Hibernate的Spring Data JPA和SpringSecurity. 1. ...

  3. 使用JavaConfig的SpringMVC4 + Spring Data JPA + SpringSecurity配置

    在本文中,我们将看到如何使用JavaConfig配置和集成SpringMVC4,带有Hibernate的Spring Data JPA和SpringSecurity. 1.首先让我们在pom.xml中 ...

  4. 视频教程-Spring Data JPA项目-Java

    Spring Data JPA项目 2011年毕业后在澳门 遊澳集团(UO Group)旗下某IT科技公司从事 android,php,j2ee开发工作,负责 国际短信发送系统.银联支付业务系统.酒店 ...

  5. Spring Boot 学习[四] web项目实战训练(增删改查,分页,排序)

    Spring boot非常适合Web应用程序开发.您可以轻松创建自包含的HTTP应用.web服务器采用嵌入式Tomcat,或者Jetty等. 几点说明: Spring boot开发web项目,通常打成 ...

  6. spring boot新建非web项目(无需依赖)

    spring boot新建非web项目(无需依赖) spring boot集成spring data jpa的时候需要jdk版本为1.8,所以jdk的版本最好设置为1.8 如果新建的项目是以jsp为模 ...

  7. Spring Boot + Spring Data + Elasticsearch实例

    在本文中,我们将讨论"如何创建Spring Boot + Spring Data + Elasticsearch范例". 本文中使用的工具: Spring Boot 1.5.1.R ...

  8. Spring Boot+Spring Cloud实现itoken项目

    itoken项目简介 开发环境 操作系统: Windows 10 Enterprise 开发工具: Intellij IDEA 数据库: MySql 5.7.22 Java SDK: Oracle J ...

  9. spring boot 前后端分离项目(商城项目)学习笔记

    spring boot 前后端分离项目(商城项目)学习笔记 目录 spring boot 前后端分离项目(商城项目)学习笔记 后端配置 springboot项目 pom.xml文件 maven 配置文 ...

  10. Spring Boot 面试杀手锏:自动配置原理

    欢迎关注方志朋的博客,回复"666"获面试宝典 不论在工作中,亦或是求职面试,Spring Boot已经成为我们必知必会的技能项.除了某些老旧的政府项目或金融项目持有观望态度外,如 ...

最新文章

  1. 谈谈神秘的ES6——(一)初识ECMAScript
  2. win10 mysql 远程连接_win10 docker部署mysql并启动远程连接
  3. Javascript 用本页面文本域中的HTML代码打开一个空白窗口来运行
  4. java对两个表进行排序_Excel工作簿中多个worksheet工作表,如何对工作表进行排序?...
  5. java sleep唤醒_JAVA wait(), notify(),sleep详解(转)
  6. 零中频接收机频率转换图_【鼎阳硬件智库原创︱频谱分析仪】频谱分析仪应用解惑之频率分辨力...
  7. VB 文件常用操作相关API
  8. vs code react-native 安卓调试_实战|C++在vscode上的调试配置
  9. java 对象equals_浅谈Java对象的equals方法
  10. 玩转Android---组件篇---Broadcast Receiver(广播接收器)
  11. Improving Opencv 8: The Core Functionality :File Input and Output using XML and YAML files
  12. 麻省理工线性代数第三讲
  13. 如何在Linux上编写和运行程序?
  14. PowerDesigner 下载安装
  15. vnc远程桌面精灵,七款让人爱不释手的vnc远程桌面精灵
  16. 可编辑!热门动态表情包!
  17. IDEA中怎么调出右下角的版本控制Git
  18. 【论文阅读笔记】Rethinking the Evaluation of Video Summaries 视频摘要评估
  19. linux mmc驱动
  20. 2006年最值得期待的大片

热门文章

  1. 洛谷P2580 于是他错误的点名开始了 题解
  2. JS学习笔记——JavaScript继承的6种方法(原型链、借用构造函数、组合、原型式、寄生式、寄生组合式)...
  3. thinkphp学习总结
  4. 工具-VS插件Resharper快捷键
  5. 梦断代码最后4章读后感
  6. php微型mvc框架创建步骤
  7. Delphi 重启应用程序
  8. 【随感】我觉得,世界上最美好的乐器是钢琴和架子鼓
  9. 谈谈joomla1.5中个人遇见的古怪问题
  10. Create umbraco website-On the road of umbraco (1)