spring+struts+ibatis 动态数据源配置 (一)

本文介绍如何实现Spring结合Ibatis进行多数据源的动态切换配置。通过详细的步骤和代码示例,展示了属性文件配置、C3P0连接池设置、Spring事务管理及ibatis sqlmap配置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[align=center][b]spring+ibatis配置多数据源[/b][/align]

说明:最近因为项目需要,经过多方努力和救助,终于实现了spring+ibatis的动态数据源切换配置,在此贴出,以供大家参考,同时希望大家多提意见……

下面是详细代码:
     1.  spring的applicationContext.xml配置文件:
a.  属性文件配置:
<bean id="propertyConfig"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
   <value>    classpath:resource/DataSourceConfig.properties
   </value>
  </property>
 </bean>

b.  DataSource: C3P0连接池:
【注意:由于存在多数据源,故此步按需要多次配置
<bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="${gpt.driverClassName}" />
  <property name="jdbcUrl" value="${gpt.url}" />
  <property name="user" value="${gpt.username}" />
  <property name="password" value="${gpt.password}" />
  <property name="minPoolSize" value="${gpt.minPoolSize}" />
  <property name="acquireRetryAttempts"
   value="${gpt.acquireRetryAttempts}" />
  <property name="acquireIncrement"
   value="${gpt.acquireIncrement}" />
  <property name="maxIdleTime" value="${gpt.maxIdleTime}" />
  <property name="initialPoolSize" value="${gpt.initialPoolSize}" />
  <property name="maxPoolSize" value="${gpt.maxPoolSize}" />
  <property name="idleConnectionTestPeriod"
   value="${gpt.idleConnectionTestPeriod}" />
 </bean>

c.  spring事务管理配置:
【红色标记处,多个数据源配置多个】
<bean id="autoProxyCreator"  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <value>*Service</value>
  </property>
  <property name="interceptorNames">
   <list><value>transactionInterceptor1</value>   <value>transactionInterceptor2</value>   <value>transactionInterceptor3</value>
   </list>
  </property>
 </bean>
<!-- 标记段重复配置,具体看自己需要 start -->
<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <bean id="transactionInterceptor1"  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager" />
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="do*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
<!-- end -->

d.  ibatis sql map 配置:
【同理,有多少个数据源就配置多少遍】
<bean id="sqlMapClient"  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation"   value="classpath:com/sqlmap/sqlmap-config.xml" />

就这样,spring配置文件就准备好了。
 </bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值