gpt4 book ai didi

java - Spring和Hibernate中的多个数据库

转载 作者:行者123 更新时间:2023-11-30 03:28:16 25 4
gpt4 key购买 nike

这是我的 sessionFactory 配置。

<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan">
<beans:list>
<beans:value>package</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/msell" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
<beans:property name="initialSize" value="10" />
<beans:property name="maxActive" value="5" />
<beans:property name="maxWait" value="5000" />
<beans:property name="testOnBorrow" value="true"/>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManager" />
<beans:bean id="sessionFactorySports"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSourceSports" />
<beans:property name="packagesToScan">
<beans:list>
<beans:value>package</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="dataSourceSports" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/msell_sports" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
<beans:property name="initialSize" value="10" />
<beans:property name="maxActive" value="5" />
<beans:property name="maxWait" value="5000" />
<beans:property name="testOnBorrow" value="true"/>
</beans:bean>
<beans:bean id="transactionManagerSports"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactorySports" />
</beans:bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManagerSports" />

我尝试像下面这样注入(inject) sessionFactorySports 。

@Autowired(required=false)
@Qualifier(value="sessionFactorySports")
SessionFactory sessionFactory;

但是原始的 sessionFactory 对象仍然只被注入(inject)。配置出了什么问题?为什么 sessionFactorySports 没有被注入(inject)?

编辑1:

我尝试将第一个 sessionFactory 名称更改为 sessionFactoryFashion。但是现在我收到“没有定义 [org.hibernate.SessionFactory] ​​类型的合格 bean:预期单个匹配 bean 但发现 2:sessionFactoryFashion、sessionFactorySports”此错误

最佳答案

不应该是@Qualifier(“sessionFactorySports”)吗??

这是引用文献; https://softwaredevtips.wordpress.com/2012/05/25/springhibernate-multiple-datasources/

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

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