配置文件applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 上下文依赖注入 -->
<context:annotation-config />
<!-- 自动扫描service -->
<context:component-scan base-package="com.zhang" />
<context:component-scan base-package="com.fudian" />
<context:component-scan base-package="com.lsl" />
<context:component-scan base-package="com.excle.service" />
<!-- 配置 dao 层自动扫描包 -->
<context:component-scan base-package="com.excle.dao"></context:component-scan>
<!-- 框架扫描 基础的控制器
<context:component-scan base-package="com.lswq.app.core" />-->
<!-- 引入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 这里支持多种寻址方式:classpath和file -->
<value>classpath:properties/jdbc.properties</value>
</list>
</property>
</bean>
<!-- <import resource="spring-log-manage.xml"/> -->
<!--<import resource="spring-write-mybatis.xml"/>-->
<import resource="spring-read-mybatis.xml"/>
<!--<import resource="spring-ztest-mybatis.xml"/>-->
<import resource="spring-zysj-task.xml"/>
<!--spring的路由来管理数据源-->
<bean id="dynamicDataSource" class="com.excle.utils.DynamicDataSource">
<property name="defaultTargetDataSource" ref="writeDataSource"></property>
<property name="targetDataSources">
<map>
<entry value-ref="writeDataSource" key="writeDataSource"/>
<entry value-ref="DataSource1" key="DataSource1"/>
<entry value-ref="zTestDataSource" key="zTestDataSource"/>
<entry value-ref="webCodeDataSource" key="webCodeDataSource"/>
<entry value-ref="swxxDataSource" key="swxxDataSource"/>
</map>
</property>
</bean>
<!--数据源1 主库-->
<bean id="writeDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
<property name="acquireRetryDelay" value="1000"></property>
<property name="acquireRetryAttempts" value="60"></property>
<property name="breakAfterAcquireFailure" value="false"></property>
</bean>
<!--数据源2 其他-->
<bean id="zTestDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url2}"></property>
<property name="driverClass" value="${driver2}"></property>
<property name="user" value="${username2}"></property>
<property name="password" value="${password2}"></property>
<property name="maxPoolSize" value="30" ></property>
<property name="minPoolSize" value="10" ></property>
<property name="maxIdleTime" value="1800" ></property>
<property name="maxStatements" value="0" ></property>
<property name="initialPoolSize" value="10"></property>
<property name="idleConnectionTestPeriod" value="1800" ></property>
</bean>
<!--数据源3 民宗-->
<bean id="DataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url3}"></property>
<property name="driverClass" value="${driver3}"></property>
<property name="user" value="${username3}"></property>
<property name="password" value="${password3}"></property>
<property name="maxPoolSize" value="30" ></property>
<property name="minPoolSize" value="10" ></property>
<property name="maxIdleTime" value="1800" ></property>
<property name="maxStatements" value="0" ></property>
<property name="initialPoolSize" value="10"></property>
<property name="idleConnectionTestPeriod" value="1800" ></property>
</bean>
<!--数据源4 共享库查询 -->
<bean id="webCodeDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url4}"></property>
<property name="driverClass" value="${driver4}"></property>
<property name="user" value="${username4}"></property>
<property name="password" value="${password4}"></property>
<property name="maxPoolSize" value="30" ></property>
<property name="minPoolSize" value="10" ></property>
<property name="maxIdleTime" value="1800" ></property>
<property name="maxStatements" value="0" ></property>
<property name="initialPoolSize" value="10"></property>
<property name="idleConnectionTestPeriod" value="1800" ></property>
</bean>
<!--数据源5 税务信息查询 -->
<bean id="swxxDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url5}"></property>
<property name="driverClass" value="${driver5}"></property>
<property name="user" value="${username5}"></property>
<property name="password" value="${password5}"></property>
<property name="maxPoolSize" value="30" ></property>
<property name="minPoolSize" value="10" ></property>
<property name="maxIdleTime" value="1800" ></property>
<property name="maxStatements" value="0" ></property>
<property name="initialPoolSize" value="10"></property>
<property name="idleConnectionTestPeriod" value="1800" ></property>
</bean>
<!-- spring和MyBatis整合,可以不需要mybatis的配置映射文件 -->
<bean id="writSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<!-- 自动扫描mapping.xml文件,如果用这,就不需要mybatis-config.xml文件了 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
</bean>
<!-- 扫描接口包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.excle.dao"/>
<property name="sqlSessionFactoryBeanName" value="writSessionFactory"/>
</bean>
<bean id="writableSQLSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="writSessionFactory" />
</bean>
<bean id="batchWritableSQLSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="writSessionFactory" />
</bean>
<!-- 对标注@Transactional注解的Bean进行加工处理,以织入事务管理切面 -->
<!-- 默认情况下,<tx:annotation-driven的transaction-manager的值为transactionManager,因而此处可直接简化为 <tx:annotation-driven/> -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dynamicDataSource" />
</bean>
<!-- 对服务层的类提供事务支持 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression="execution(* com.fudian.service.*.*(..))"/>
<!-- 引用事务增强 -->
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
<!-- 事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--事务属性定义 -->
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<bean id="idWorker" class="com.excle.utils.IdWorker"></bean>
</beans>
jdbc.properties
driver=net.sourceforge.jtds.jdbc.Driver
url=jdbc:jtds:sqlserver://192.168.1.3:49156;databaseName=test
username=sa
password=
#数据源2 其他
driver2=net.sourceforge.jtds.jdbc.Driver
url2=jdbc:jtds:sqlserver://192.168.1.3:49156;databaseName=test1
username2=sa
password2=