gpt4 book ai didi

java - 在 Spring 和 Hibernate 中使用多个数据源

转载 作者:行者123 更新时间:2023-12-02 05:21:25 25 4
gpt4 key购买 nike

我有一个主数据源,我可以在其中读取/写入访问权限,还有另一个数据源,我可以在其中提取数据并将其导入到我的主数据源中。

目前我的整个应用程序仅使用读/写数据库。因此,我这样配置:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:annotation-config />

<context:component-scan base-package="com.limitCalculator"
annotation-config="true" />

<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />

<bean id="mainGUI" class="com.limitCalculator.gui.scenarioSelection.MainWindow" />

<!-- 1 Data Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/testDB" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="jpaData" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="entityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<!-- 2 Data Source -->
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@test.com.vi:1234:TEST" />
<property name="username" value="test_db" />
<property name="password" value="abcde" />
<property name="maxActive" value="20" />
</bean>
</beans>

我的 DAO 调用:

@Component
public class SettingsDaoImpl {

@Autowired
@PersistenceContext
public EntityManager em;

public SettingsDaoImpl() {
super();
}

@Transactional
public Long save(Settings sr)
{
em.persist(sr);
return sr.getId();
}

如您所见,我在应用程序中添加了第二个数据库。但是,我不知道如何在我的 SettingsDaoImpl 中正确调用它?

关于如何在我当前的架构中实现这一点的任何建议。

最佳答案

您需要为每个数据源创建不同的实体管理器,并且在代码中您需要定义@PersistenceContext的unitName属性来注入(inject)特定的实体管理器。

关于java - 在 Spring 和 Hibernate 中使用多个数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26480665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com