iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >使用Spring MVC如何实现将java项目连接两个数据库
  • 587
分享到

使用Spring MVC如何实现将java项目连接两个数据库

springmvcjava数据库 2023-05-31 14:05:26 587人浏览 独家记忆
摘要

使用spring mvc如何实现将java项目连接两个数据库?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。实现方法:数据源在配置文件中的配置<pre name=&qu

使用spring mvc如何实现将java项目连接两个数据库?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

实现方法:

数据源在配置文件中的配置

<pre name="code" class="java"><&#63;xml version="1.0" encoding="UTF-8"&#63;> <beans xmlns="Http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:cache="http://www.springframework.org/schema/cache"  xmlns:context="http://www.springframework.org/schema/context"  xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"  xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd  http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">   <context:annotation-config />   <context:component-scan base-package="com"></context:component-scan>   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   <property name="locations">    <list>     <value>classpath:com/resource/config.properties</value>    </list>   </property>  </bean>   <bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource"   destroy-method="close">   <property name="driverClass" value="${dbOne.jdbc.driverClass}" />   <property name="jdbcUrl" value="${dbOne.jdbc.url}" />   <property name="user" value="${dbOne.jdbc.user}" />   <property name="passWord" value="${dbOne.jdbc.password}" />   <property name="initialPoolSize" value="${dbOne.jdbc.initialPoolSize}" />   <property name="minPoolSize" value="${dbOne.jdbc.minPoolSize}" />   <property name="maxPoolSize" value="${dbOne.jdbc.maxPoolSize}" />  </bean>   <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource"   destroy-method="close">   <property name="driverClass" value="${dbTwo.jdbc.driverClass}" />   <property name="jdbcUrl" value="${dbTwo.jdbc.url}" />   <property name="user" value="${dbTwo.jdbc.user}" />   <property name="password" value="${dbTwo.jdbc.password}" />   <property name="initialPoolSize" value="${dbTwo.jdbc.initialPoolSize}" />   <property name="minPoolSize" value="${dbTwo.jdbc.minPoolSize}" />   <property name="maxPoolSize" value="${dbTwo.jdbc.maxPoolSize}" />  </bean>   <bean id="dynamicDataSource" class="com.core.DynamicDataSource">   <property name="targetDataSources">    <map key-type="java.lang.String">     <entry value-ref="dataSourceOne" key="dataSourceOne"></entry>     <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>    </map>   </property>   <property name="defaultTargetDataSource" ref="dataSourceOne">   </property>  </bean>   <bean id="sessionFactory" class="org.springframework.ORM.hibernate4.LocalSessionFactoryBean">   <property name="dataSource" ref="dynamicDataSource" />   <property name="hibernateProperties">    <props>     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>     <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>     <prop key="hibernate.show_sql">false</prop>     <prop key="hibernate.format_sql">true</prop>     <prop key="hbm2ddl.auto">create</prop>    </props>   </property>   <property name="packagesToScan">    <list>     <value>com.po</value>    </list>   </property>  </bean>   <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">   <property name="sessionFactory" ref="sessionFactory" />  </bean>   <aop:config>   <aop:pointcut id="transactionPointCut" expression="execution(* com.dao..*.*(..))" />   <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointCut" />  </aop:config>   <tx:advice id="txAdvice" transaction-manager="transactionManager">   <tx:attributes>    <tx:method name="add*" propagation="REQUIRED" />    <tx:method name="save*" propagation="REQUIRED" />    <tx:method name="update*" propagation="REQUIRED" />    <tx:method name="delete*" propagation="REQUIRED" />    <tx:method name="*" read-only="true" />   </tx:attributes>  </tx:advice>   <aop:config>   <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">    <aop:pointcut id="daoOne" expression="execution(* com.dao.one.*.*(..))" />    <aop:pointcut id="daoTwo" expression="execution(* com.dao.two.*.*(..))" />    <aop:before pointcut-ref="daoOne" method="setdataSourceOne" />    <aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" />   </aop:aspect>  </aop:config> </beans> 

--结束END--

本文标题: 使用Spring MVC如何实现将java项目连接两个数据库

本文链接: https://www.lsjlt.com/news/226187.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作