spring多个数据源配置
sys.properties中的内容 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver DB.url=jdbc\:oracle\:thin\:@****\:1521\:**** DB.username=**** DB.password=**** jdbc.driverClassName2=oracle.jdbc.driver.OracleDriver DB.url2=jdbc\:oracle\:thin\:@****\:1521\:**** DB.username2=**** DB.password2=**** Spring配置文件中添加的内容 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!--数据库配置文件--> <value>WEB-INF/sys.properties</value> </list> </property> </bean> <!-- 配置数据源1 --> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${DB.url}"/> <property name="username" value="${DB.username}"/> <property name="password" value="${DB.password}"/> <!--initialSize: 初始化连接--> <property name="initialSize" value="5"/> <!--maxIdle: 最大空闲连接--> <property name="maxIdle" value="3"/> <!--minIdle: 最小空闲连接--> <property name="minIdle" value="2"/> <!--maxActive: 最大连接数量--> <property name="maxActive" value="10"/> <!--removeAbandoned: 是否自动回收超时连接--> <property name="removeAbandoned" value="true"/> <!--removeAbandonedTimeout: 超时时间(以秒数为单位)--> <property name="removeAbandonedTimeout" value="180"/> <!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒--> <property name="maxWait" value="3000"/> <property name="poolPreparedStatements" value="false"/> <property name="defaultAutoCommit" value="true"/> <property name="validationQuery" value="select * from dual"/> </bean> <!-- 配置数据源2 --> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName2}"/> <property name="url" value="${DB.url2}"/> <property name="username" value="${DB.username2}"/> <property name="password" value="${DB.password2}"/> <!--initialSize: 初始化连接--> <property name="initialSize" value="5"/> <!--maxIdle: 最大空闲连接--> <property name="maxIdle" value="3"/> <!--minIdle: 最小空闲连接--> <property name="minIdle" value="2"/> <!--maxActive: 最大连接数量--> <property name="maxActive" value="10"/> <!--removeAbandoned: 是否自动回收超时连接--> <property name="removeAbandoned" value="true"/> <!--removeAbandonedTimeout: 超时时间(以秒数为单位)--> <property name="removeAbandonedTimeout" value="180"/> <!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒--> <property name="maxWait" value="3000"/> <property name="poolPreparedStatements" value="false"/> <property name="defaultAutoCommit" value="true"/> <property name="validationQuery" value="select * from dual"/> </bean> <!-- 事务1 --> <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource1" /> </bean> <!-- 事务2 --> <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource2" /> </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="WEB-INF/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="WEB-INF/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> <!--将数据源注入到dao层--> <bean id="deptDAO" class="com.cqjk.datasv.dept.DeptDAO" parent="baseIbatisDAO1"> <property name="sqlmapNamespace"> <value>dept</value> </property> </bean> <bean id="userDAO" class="com.cqjk.datasv.user.UserDAO" parent="baseIbatisDAO1"> <property name="sqlmapNamespace"> <value>user</value> </property> </bean> <bean id="eventsDAO" class="com.cqjk.datasv.events.EventsDAO" parent="baseIbatisDAO2"> <property name="sqlmapNamespace"> <value>events</value> </property> </bean> <bean id="ddwcDAO" class="com.cqjk.datasv.ddwc.DdwcDAO" parent="baseIbatisDAO2"> <property name="sqlmapNamespace"> <value>ddwc</value> </property> </bean> <bean id="ddwcRyDAO" class="com.cqjk.datasv.ddwcry.DdwcRyDAO" parent="baseIbatisDAO2"> <property name="sqlmapNamespace"> <value>ddwcry</value> </property> </bean>