Spring的多数据源配置(Spring+iBATIS + Oracle环境下)
来源:互联网 发布:能备案的域名后缀 编辑:程序博客网 时间:2024/06/05 06:36
在spring的配置文件上,配置两个数据源、两个事务、两个事务拦截、两个ibatis的工厂数据源配置、两个ibatis的抽象Dao。代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 数据源1 -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>Oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.1.2:1522:test01</value>
</property>
<property name="username">
<value>user01</value>
</property>
<property name="password">
<value>psw01</value>
</property>
<property name="maxActive">
<value>100</value>
</property>
<property name="maxIdle">
<value>8</value>
</property>
<property name="minIdle">
<value>1</value>
</property>
</bean>
<!-- 数据源2 -->
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.169.1.3:1552:test02</value>
</property>
<property name="username">
<value>user02</value>
</property>
<property name="password">
<value>psw02</value>
</property>
<property name="maxActive">
<value>100</value>
</property>
<property name="maxIdle">
<value>8</value>
</property>
<property name="minIdle">
<value>1</value>
</property>
</bean>
<!-- 事务1 -->
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource2" />
</bean>
<!-- 事务2 -->
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1" />
</bean>
<!-- 事务拦截1 -->
<bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager1" />
<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>
<!-- 事务拦截2 -->
<bean id="transactionInterceptor2" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager2" />
<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>
<!-- 管理你连接的地方-->
<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>
</list>
</property>
</bean>
<!-- ibatis的工厂数据源配置1 -->
<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:config/sql-map-config.xml" /><!--这里是ibatis的sqlMap文件集合 -->
<property name="dataSource" ref="dataSource1" />
</bean>
<!-- ibatis的工厂数据源配置2 -->
<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:config/sql-map-config.xml" /><!--这里是ibatis的sqlMap文件集合 -->
<property name="dataSource" ref="dataSource2" />
</bean>
<!-- ibatis抽象的Dao1 -->
<bean id="baseIbatisDAO1" abstract="true">
<property name="sqlMapClient">
<ref local="sqlMapClient1" />
</property>
</bean>
<!-- ibatis抽象的Dao2 -->
<bean id="baseIbatisDAO2" abstract="true">
<property name="sqlMapClient">
<ref local="sqlMapClient2" />
</property>
</bean>
</beans>
剩下的就是spring的DAO和 service层的调用和配置了。举个例子吧:
DAO层:
<pre class="html" name="code"> <bean id="userDAO"
class="org.xxx.dao.impl.UserDAOImpl"
parent="baseIbatisDAO1">
</bean>
<bean id="deptDAO"
class="org.xxx.dao.impl.DeptDAOImpl"
parent="baseIbatisDAO2">
</bean>
service层(业务层):
<bean id="userService"
class="org.xxx.service.impl.UserServiceImpl"
parent="userDAO">
</bean>
<bean id="deptService"
class="org.xxx.service.impl.DeptServiceImpl"
parent="deptDAO">
</bean>
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2012-02/55524.htm
- spring的多数据源配置(spring+ibatis + oracle环境下)
- Spring的多数据源配置(Spring+iBATIS + Oracle环境下)
- spring的多数据源配置(spring+ibatis + oracle环境下)
- spring+mybatis环境下多数据源的配置
- Spring、Ibatis 配置多数据源
- spring+ibatis配置多数据源
- Spring+iBatis多数据源的动态配置方案
- Spring+iBatis多数据源的动态配置方案
- Spring+iBatis多数据源的动态配置方案
- Spring+iBatis多数据源的动态配置方案
- Spring+ibatis 多数据源的配置和使用
- SPRING IBATIS 多数据源
- 在Spring下配置多数据源
- Spring下mybatis 多数据源配置
- Spring下mybatis配置多数据源
- Spring下mybatis多数据源配置
- Spring下mybatis多数据源配置
- spring下mybatis配置多数据源
- 关于hadoop中filesystem closed的error的想法
- 计算机图形学概况
- Android LayoutInflater详解
- js-知识1
- 一个前辈写的网络协议学习方法
- Spring的多数据源配置(Spring+iBATIS + Oracle环境下)
- 字符串转换成整数 (bug版)
- 服务器集群架构的设计与选择
- 谷歌2013校园招聘笔试题心得(附答案)
- Linux ixgbe 10G intel 网卡数据包处理流程
- java异常(Exception)
- C#文件流实现文件复制
- Java中static、final用法小结
- asp.net Session丢失问题