Spring双数据库是怎样配置的

发布时间:2021-09-18 17:48:37 作者:柒染
来源:亿速云 阅读:121

这篇文章将为大家详细讲解有关Spring双数据库是怎样配置的,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

有时候我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在Spring中配置相关信息。

首先是添加配置文件conf.properties

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="locations">              <list>                  <value>classpath:config.properties</value>              </list>          </property>  </bean>

其次是添加数据源(${...}对应的是conf.properties中的配置信息)

<!--对应数据A的数据源-->  <bean id="dataSource_A" class="org.apache.commons.dbcp.BasicDataSource">          <property name="driverClassName" value="${A.driver_class}" />          <property name="url" value="${A.url}" />          <property name="username" value="${A.username}" />          <property name="password" value="${A.password}" />  </bean>  <!--对应数据库B的数据源-->  <bean id="dataSource_B" class="org.apache.commons.dbcp.BasicDataSource">          <property name="driverClassName" value="${B.driver_class}" />          <property name="url" value="${B.url}" />          <property name="username" value="${B.username}" />          <property name="password" value="${B.password}" />  </bean>

之后是添加对应的sessionFactory:

<!-- A的sessionFactory -->      <bean id="sessionFactory_A" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">          <property name="dataSource" ref="dataSource_A"/>      </bean>  <!-- B的sessionFactory -->      <bean id="sessionFactory_B" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">          <property name="dataSource" ref="dataSource_B"/>          </bean>

在项目中的dao层有时会出现这样的配置信息:

<bean id = "XDao" class = "xxx.xxx.xDaoImpl">  <property name="sessionFactory" ref="sessionFactory"></property>  </bean>

为了实现使用两个不同的数据库,可以改成

<span style="font-family:'sans serif', tahoma, verdana, helvetica;font-size:13px;line-height:19px;white-space:normal;background-color:#ffffff;">&nbsp;</span><span style="font-family:'sans serif', tahoma, verdana, helvetica;white-space:normal;background-color:#ffffff;"><!--使用A数据库的DAO--></span> <bean id = "XDao" class = "xxx.xxx.xDaoImpl">  <property name="sessionFactory" ref="sessionFactory_A"></property>  </bean>  <!--使用B数据库的DAO-->  <bean id = "XDao" class = "xxx.xxx.xDaoImpl">  <property name="sessionFactory" ref="sessionFactory_B"></property>  </bean>

这样就能实现双数据库了。。。。

关于Spring双数据库是怎样配置的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. mysql双主配置讲义
  2. MySQL数据库如何配置Keepalived双主

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring 数据库

上一篇:Python开发者应该知道的7个开发库分别是什么

下一篇:seoer中网站外链建设的方法有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》