Spring JPA – Multiple Databases
liuian 2025-06-28 15:17 60 浏览
1. 概述
在本教程中,我们将为具有多个数据库的 Spring 数据 JPA 系统实现一个简单的 Spring 配置。
2. 实体
首先,让我们创建两个简单的实体,每个实体都位于一个单独的数据库中。
下面是第一个用户实体:
package com.baeldung.multipledb.model.user;
@Entity
@Table(schema = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@Column(unique = true, nullable = false)
private String email;
private int age;
}这是第二个实体,产品:
package com.baeldung.multipledb.model.product;
@Entity
@Table(schema = "products")
public class Product {
@Id
private int id;
private String name;
private double price;
}我们可以看到,这两个实体也被放置在独立的包中。当我们进入配置时,这将很重要。
3. JPA 存储库
接下来,让我们看一下我们的两个 JPA 存储库,UserRepository:
package com.baeldung.multipledb.dao.user;
public interface UserRepository
extends JpaRepository<User, Integer> { }和产品存储库:
package com.baeldung.multipledb.dao.product;
public interface ProductRepository
extends JpaRepository<Product, Integer> { }再次注意我们如何在不同的包中创建这两个存储库。
4. 使用 Java 配置 JPA
现在我们将进入实际的 Spring 配置。我们将首先设置两个配置类 — 一个用于用户,另一个用于产品。
在每个配置类中,我们需要为 User 定义以下接口:
- 数据源
- EntityManagerFactory (userEntityManager )
- TransactionManager (userTransactionManager)
让我们先看一下用户配置:
@Configuration
@PropertySource({ "classpath:persistence-multiple-db.properties" })
@EnableJpaRepositories(
basePackages = "com.baeldung.multipledb.dao.user",
entityManagerFactoryRef = "userEntityManager",
transactionManagerRef = "userTransactionManager"
)
public class PersistenceUserConfiguration {
@Autowired
private Environment env;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean userEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(userDataSource());
em.setPackagesToScan(
new String[] { "com.baeldung.multipledb.model.user" });
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public DataSource userDataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("user.jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager userTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
userEntityManager().getObject());
return transactionManager;
}
}请注意我们如何通过使用 @Primary 注释 bean 定义来使用 userTransactionManager 作为我们的主事务管理器。每当我们要隐式或显式注入事务管理器而不按名称指定哪个时,这很有帮助。
接下来,让我们讨论
PersistenceProductConfiguration,其中我们定义了类似的 bean:
@Configuration
@PropertySource({ "classpath:persistence-multiple-db.properties" })
@EnableJpaRepositories(
basePackages = "com.baeldung.multipledb.dao.product",
entityManagerFactoryRef = "productEntityManager",
transactionManagerRef = "productTransactionManager"
)
public class PersistenceProductConfiguration {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(productDataSource());
em.setPackagesToScan(
new String[] { "com.baeldung.multipledb.model.product" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
public DataSource productDataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("product.jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Bean
public PlatformTransactionManager productTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
productEntityManager().getObject());
return transactionManager;
}
}5. 简单测试
最后,让我们测试一下我们的配置。
为此,我们将创建每个实体的实例并确保已创建它:
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableTransactionManagement
public class JpaMultipleDBIntegrationTest {
@Autowired
private UserRepository userRepository;
@Autowired
private ProductRepository productRepository;
@Test
@Transactional("userTransactionManager")
public void whenCreatingUser_thenCreated() {
User user = new User();
user.setName("John");
user.setEmail("john@test.com");
user.setAge(20);
user = userRepository.save(user);
assertNotNull(userRepository.findOne(user.getId()));
}
@Test
@Transactional("userTransactionManager")
public void whenCreatingUsersWithSameEmail_thenRollback() {
User user1 = new User();
user1.setName("John");
user1.setEmail("john@test.com");
user1.setAge(20);
user1 = userRepository.save(user1);
assertNotNull(userRepository.findOne(user1.getId()));
User user2 = new User();
user2.setName("Tom");
user2.setEmail("john@test.com");
user2.setAge(10);
try {
user2 = userRepository.save(user2);
} catch (DataIntegrityViolationException e) {
}
assertNull(userRepository.findOne(user2.getId()));
}
@Test
@Transactional("productTransactionManager")
public void whenCreatingProduct_thenCreated() {
Product product = new Product();
product.setName("Book");
product.setId(2);
product.setPrice(20);
product = productRepository.save(product);
assertNotNull(productRepository.findOne(product.getId()));
}
}6. Spring 启动中的多个数据库
Spring Boot 可以简化上述配置。
默认情况下,Spring Boot 将使用 spring.datasource.* 前缀的配置属性实例化其默认数据源:
spring.datasource.jdbcUrl = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]我们现在希望继续使用相同的方法来配置第二个数据源,但使用不同的属性命名空间:
spring.second-datasource.jdbcUrl = [url]
spring.second-datasource.username = [username]
spring.second-datasource.password = [password]因为我们希望 Spring Boot 自动配置来获取这些不同的属性(并实例化两个不同的数据源),所以我们将定义两个类似于前面部分的配置类:
@Configuration
@PropertySource({"classpath:persistence-multiple-db-boot.properties"})
@EnableJpaRepositories(
basePackages = "com.baeldung.multipledb.dao.user",
entityManagerFactoryRef = "userEntityManager",
transactionManagerRef = "userTransactionManager")
public class PersistenceUserAutoConfiguration {
@Primary
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
}
// userEntityManager bean
// userTransactionManager bean
}@Configuration
@PropertySource({"classpath:persistence-multiple-db-boot.properties"})
@EnableJpaRepositories(
basePackages = "com.baeldung.multipledb.dao.product",
entityManagerFactoryRef = "productEntityManager",
transactionManagerRef = "productTransactionManager")
public class PersistenceProductAutoConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.second-datasource")
public DataSource productDataSource() {
return DataSourceBuilder.create().build();
}
// productEntityManager bean
// productTransactionManager bean
}
现在,我们已经根据 Boot 自动配置约定在
persistence-multiple-db-boot.properties 中定义了数据源属性。
有趣的部分是用@ConfigurationProperties注释数据源 Bean 创建方法。我们只需要指定相应的配置前缀。在这种方法中,我们使用的是DataSourceBuilder,Spring Boot将自动处理其余的工作。
但是,如何将配置的属性注入到数据源配置中呢?
在 DataSourceBuilder 上调用 build() 方法时,它将调用其私有 bind() 方法:
public T build() {
Class<? extends DataSource> type = getType();
DataSource result = BeanUtils.instantiateClass(type);
maybeGetDriverClassName();
bind(result);
return (T) result;
}此私有方法执行大部分自动配置魔术,将解析的配置绑定到实际的数据源实例:
private void bind(DataSource result) {
ConfigurationPropertySource source = new MapConfigurationPropertySource(this.properties);
ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
aliases.addAliases("url", "jdbc-url");
aliases.addAliases("username", "user");
Binder binder = new Binder(source.withAliases(aliases));
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));
}虽然我们自己不必接触任何这些代码,但了解 Spring Boot 自动配置的幕后发生了什么仍然很有用。
除此之外,事务管理器和实体管理器 bean 配置与标准 Spring 应用程序相同。
7. 结论
本文是对如何配置我们的 Spring Data JPA 项目以使用多个数据库的实际概述。
完整的实现可以在 GitHub 项目中找到。这是一个基于 Maven 的项目,因此应该易于导入和按原样运行。
参考 :
https://www.baeldung.com/spring-data-jpa-multiple-databases
github:https://github.com/eugenp/tutorials/tree/master/persistence-modules/spring-data-jpa-enterprise-2
相关推荐
- isofix接口(isofix接口怎么拆卸)
-
isofix接口上有isofix的字样,它的图标是一个类似人坐在座椅上。在汽车座椅靠垫与坐垫之间的连接处有2个接口,那就是isofix接口,是一种硬连接方式,为了防止撞击时发生座椅翻转,一些座椅在IS...
- 无线路由器怎么设置不让别人连接
-
要设置无线路由器不让别人连接,可以采取以下措施:1.配置路由器密码:在路由器设置中,可以设置密码,限制只有授權的人才能连接到网络。2.配置防火墙:可以安装防火墙软件,如OpenDNS,限制...
- fast迅捷路由器(fast迅捷路由器配置上网教程)
-
http://www.fastcom.com.cn/上面这个就迅捷官网的网址老版本的迅捷路由器默认登录地址为“192.168.1.1”,新版本的迅捷路由器默认登录地址为“falogin.cn”。02...
- 手机天梯图2025最新版(手机天梯图cpu2020快科技)
-
一般情况下而言,手机处理器的性能越强,功耗也就越高。有网友制作了一张手机处理器功耗排名图,高通骁龙888位8.34W,是榜单中功耗第二的处理器。而海思麒麟9000则位列第三,功耗为8.3W。一般情况下...
- pe系统下载官网手机版(pe系统之家)
-
打开手机应用商店,选择windowspe,下载安装PE系统是一种维护用的系统,本身是很简陋的,什么常用功能都没有,后经一些爱好都修改、完善,现在成为可当临时系统用的精简系统。但PE系统当前种类很多,...
- 云骑士装机大师怎么激活win7
-
回答如下:要激活Windows7操作系统,您可以按照以下步骤进行操作:1.确保您的计算机已连接到互联网。2.打开“开始”菜单,点击“计算机”右键,选择“属性”。3.在系统属性窗口中,向下滚动到...
- vmware workstation使用教程
-
VMwareWorkstation15是一款虚拟机软件,可以在主机上模拟多个虚拟计算机环境。以下是使用VMwareWorkstation15的基本步骤:1.下载和安装软件:从VMware官方...
- windows7旗舰版64位多少钱(windows7旗舰版64位多少钱合适)
-
这两个都是64位的,唯一区别是后者集成SP1。win7旗舰版64位玉米系统比较稳定,不但稳定,运行速度也很快!WIN764位系统需要的电脑配置。最低配置CPU:1GHz32位或2G...
- 电脑黑屏了怎么重装系统(电脑黑屏怎么重装系统win10)
-
我觉着吧,题主的判断可能是不正确的黑屏开不了机指的是开机后显示屏无任何信号接入首先从电源出发,电源的cpu供电,主板供电,显卡供电,硬盘供电,每一项都要确保接触完整再到主板,主板上呢,cpu散热必须接...
- 三星官网正品查询(三星全新正品查询网站)
-
三星服务中心地址:http://support-cn.samsung.com/support/ServiceLocations.asp国家工信部电信设备进网管理网站查询移动设备真伪方式:方法一:网站查...
- 显示windows许可证即将过期
-
电脑提示Windows许可证即将到期,可以采取以下措施:检查许可证状态:首先需要确认许可证是否真的即将过期。可以在Windows设置中查看许可证状态,或者运行命令“slmgr/xpr”来检查许可证到...
- u盘看不到第二个分区(u盘不显示第二个分区)
-
u盘分区后不显示出来原因一般为以下三种:第一种情况:对于windows系统是只能识别U盘分区的。第二种情况:关于U盘的diskgenius分区是只能看到一个分区的第三种情况:这个U盘分区已经被隐藏了,...
- 小马激活重启电脑开不了机(小马激活重启后蓝屏怎么办)
-
1.无法激活2.小马激活工具可能无法激活的原因有很多,可能是因为软件本身存在bug或者与操作系统不兼容,也可能是因为网络连接问题或者输入的激活码有误。此外,小马激活工具可能需要特定的硬件或软件环境...
- windows7 破解版(windows 7旗舰版破解密码)
-
步骤/方法按Windows徽标键+R(运行窗口),打开cmd运行窗口。输入slmgr.vbs-xpr后回车。这时会弹出一个窗口显示Win7的激活状态。Windows7旗舰版属于微软公司开发的...
- 专业数据恢复(专业数据恢复需要什么设备)
-
1、移动硬盘损坏以后,电脑无法识别到硬盘信息,那么整个硬盘数据将全部丢失。2、如果能够换一台电脑识别到,即使打不开,只要能够格式化,就有希望回复数据。可以尝试制作U盘系统盘的方法,打开u盘系统盘制作程...
- 一周热门
-
-
飞牛OS入门安装遇到问题,如何解决?
-
如何在 iPhone 和 Android 上恢复已删除的抖音消息
-
Boost高性能并发无锁队列指南:boost::lockfree::queue
-
大模型手册: 保姆级用CherryStudio知识库
-
用什么工具在Win中查看8G大的log文件?
-
如何在 Windows 10 或 11 上通过命令行安装 Node.js 和 NPM
-
威联通NAS安装阿里云盘WebDAV服务并添加到Infuse
-
Trae IDE 如何与 GitHub 无缝对接?
-
idea插件之maven search(工欲善其事,必先利其器)
-
如何修改图片拍摄日期?快速修改图片拍摄日期的6种方法
-
- 最近发表
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)
