Spring中如何配置多个数据库连接
如题,我现在只连一个数据库,用JDBC+Spring。我在Spring中的配置文件这么写<beanid="transactionManager"class="org.sp...
如题,我现在只连一个数据库,用JDBC+Spring。我在Spring中的配置文件这么写
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lifan?useUnicode=true&characterEncoding=GBK"/>
<property name="xxxx"/>
.....
</bean>
现在要配置多个数据库,我应该如何配置,Dao层又应该怎么改呢 展开
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lifan?useUnicode=true&characterEncoding=GBK"/>
<property name="xxxx"/>
.....
</bean>
现在要配置多个数据库,我应该如何配置,Dao层又应该怎么改呢 展开
2个回答
展开全部
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="maxActive" value="${dbcp.maxActive}" />
<property name="defaultAutoCommit" value="false" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="3600000" />
</bean>
然后 写个application.properties文件
配置多个数据库的jdbc.driver.....等等
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="maxActive" value="${dbcp.maxActive}" />
<property name="defaultAutoCommit" value="false" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="3600000" />
</bean>
然后 写个application.properties文件
配置多个数据库的jdbc.driver.....等等
推荐于2016-04-06 · 知道合伙人互联网行家
关注
展开全部
1.解决方案:
property name="<.commons; value=" value="close".properties文件
2.配置多个数据库:
jdbc;minEvictableIdleTimeMillis"driverClassName".maxActive}" /
password}".url}"${jdbc;>.driver;3600000"maxActive";>${jdbc.driver}"
/username"${dbcp;
</ value=".dbcp;url".BasicDataSource"
< value="bean id=".username}"
< value="
<>password"${dbcp;org.;dataSource"false" /
<property name=" destroy-method="3600000"
application;property name=" class=",property name="
< value=">.apache; value=" /property name=">
<${jdbc;property name="property name=">.;
property name="<.commons; value=" value="close".properties文件
2.配置多个数据库:
jdbc;minEvictableIdleTimeMillis"driverClassName".maxActive}" /
password}".url}"${jdbc;>.driver;3600000"maxActive";>${jdbc.driver}"
/username"${dbcp;
</ value=".dbcp;url".BasicDataSource"
< value="bean id=".username}"
< value="
<>password"${dbcp;org.;dataSource"false" /
<property name=" destroy-method="3600000"
application;property name=" class=",property name="
< value=">.apache; value=" /property name=">
<${jdbc;property name="property name=">.;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询