比尔云BierYun--阿里云最新优惠活动
阿里云优惠码丨阿里云代金券

springMVC多数据源使用 跨库跨连接

springMVC多数据源使用 跨库跨连接http://www.bieryun.com/1244.html

有的时候项目中可能要从另外一个系统取数据  如果另外一个系统能提供接口就很好解决 如果没有接口 便可以配置多个数据源切换访问

<1>:这是数据源和事务扫描注入的配置 访问多个数据源只需要建立多个数据源和事务这一套配置文件

这是第一个数据源

[html] view plain copy

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:context=“http://www.springframework.org/schema/context”
  5.     xmlns:jdbc=“http://www.springframework.org/schema/jdbc”
  6.     xmlns:jee=“http://www.springframework.org/schema/jee”
  7.     xmlns:tx=“http://www.springframework.org/schema/tx”
  8.     xmlns:aop=“http://www.springframework.org/schema/aop”
  9.     xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”
  10.     xsi:schemaLocation=”
  11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  13.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  14.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  15.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  16.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  17.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  18.         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd”
  19.     default-lazy-init=“true”>
  20.     <description>Spring公共配置 </description>
  21.     <bean id=“propertyConfigurer” class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
  22.         <property name=“locations”>
  23.             <list>
  24.                 <value>classpath:resources.properties</value>
  25.                 <!–  <value>classpath:memcached.properties</value> –>
  26.             </list>
  27.         </property>
  28.     </bean>
  29.     <!– dubbo配置 –>
  30.     <!– <dubbo:application name=“xaUserRegPro” />
  31.     <dubbo:registry address=“multicast://” />
  32.     <dubbo:reference id=“userRegProService” interface=“com.xinnet.xa.service.UserRegProService”  timeout=“6000”/> —>
  33.  <!– **************** druid 监控连接池配置 ***************** –>
  34.     <bean id=“dataSource” class=“com.alibaba.druid.pool.DruidDataSource” init-method=“init” destroy-method=“close”>
  35.         <!– 基本属性 url、user、password –>
  36.         <property name=“url” value=“${url}” />
  37.         <property name=“username” value=“${username}” />
  38.         <property name=“password” value=“${password}” />
  39.         <!– 配置初始化大小、最小、最大 –>
  40.         <property name=“initialSize” value=“${initialSize}” />
  41.         <property name=“minIdle” value=“${minIdle}” />
  42.         <property name=“maxActive” value=“${maxActive}” />
  43.         <!– 配置获取连接等待超时的时间 –>
  44.         <property name=“maxWait” value=“${maxWait}” />
  45.         <!– 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 –>
  46.         <property name=“timeBetweenEvictionRunsMillis” value=“60000” />
  47.         <!– 配置一个连接在池中最小生存的时间,单位是毫秒 –>
  48.         <property name=“minEvictableIdleTimeMillis” value=“300000” />
  49.         <property name=“validationQuery” value=“SELECT ‘x'” />
  50.         <property name=“testWhileIdle” value=“true” />
  51.         <property name=“testOnBorrow” value=“false” />
  52.         <property name=“testOnReturn” value=“false” />
  53.         <!– 打开PSCache,并且指定每个连接上PSCache的大小  –>
  54.         <!– 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false –>
  55.         <property name=“poolPreparedStatements” value=“${poolPreparedStatements}” />
  56.         <property name=“maxPoolPreparedStatementPerConnectionSize” value=“20” />
  57.         <!– 对泄漏的连接 自动关闭 –>
  58.         <property name=“removeAbandoned” value=“${removeAbandoned}” /> <!– 打开removeAbandoned功能 –>
  59.         <property name=“removeAbandonedTimeout” value=“${removeAbandonedTimeout}” /> <!– 1800秒,也就是30分钟 –>
  60.         <property name=“logAbandoned” value=“${logAbandoned}” /> <!– 关闭abanded连接时输出错误日志 –>
  61.         <!– 配置监控统计拦截的filters –>
  62.         <property name=“filters” value=“mergeStat” />
  63.         <!–  <property name=”filters” value=”stat” /> –>
  64.         <!– 慢日志查询  缺省为3秒  修改为10秒 10000 –>
  65.         <property name=“connectionProperties” value=“druid.stat.slowSqlMillis=5000” />
  66.         <!– DruidDataSource各自独立 , 支持配置公用监控数据 –>
  67.         <!– <property name=”useGloalDataSourceStat” value=”true” /> –>
  68.     </bean>
  69.     <!– druid 监控  spring  –>
  70.     <bean id=“druid-stat-interceptor” class=“com.alibaba.druid.support.spring.stat.DruidStatInterceptor”/>
  71.     <bean id=“druid-stat-pointcut” class=“org.springframework.aop.support.JdkRegexpMethodPointcut” scope=“prototype”>
  72.         <property name=“patterns”>
  73.             <list>
  74.                 <value>com.xinnet.*.service.*</value>
  75.             </list>
  76.         </property>
  77.     </bean>
  78.     <aop:config>
  79.         <aop:advisor advice-ref=“druid-stat-interceptor” pointcut-ref=“druid-stat-pointcut” />
  80.     </aop:config>
  81.     <!– MyBatis Mapper.XMl 配置 –>
  82.     <bean id=“sqlSessionFactory” class=“org.mybatis.spring.SqlSessionFactoryBean”>
  83.         <property name=“dataSource” ref=“dataSource” />
  84.         <property name=“configLocation” value=“classpath:config/mybatis.xml” />
  85.         <property name=“mapperLocations”>
  86.             <list>
  87.             <!– 自动匹配Mapper映射文件  –>
  88.                 <value>classpath:mapper/**/*-mapper.xml</value>
  89.             </list>
  90.         </property>
  91.         <!– 添加插件 –>
  92.         <property name=“plugins”>
  93.             <array>
  94.                 <ref bean=“pagePlugin” />
  95.             </array>
  96.         </property>
  97.     </bean>
  98.     <!– 支持文件上传相关 –>
  99.     <bean id=“multipartResolver” class=“org.springframework.web.multipart.commons.CommonsMultipartResolver”/>
  100.     <!– 分页插件,根据方言自动添加分页信息,默认要求 –>
  101.     <bean id=“pagePlugin” class=“com.xinnet.core.mybatis.plugin.PagePlugin”>
  102.         <property name=“properties”>
  103.             <props>
  104.                 <prop key=“dialect”>com.xinnet.core.mybatis.dialet.MySQLDialect</prop>
  105.                 <prop key=“pageSqlId”>.*query.*</prop>
  106.             </props>
  107.         </property>
  108.     </bean>
  109.     <!– redis客户端 –>
  110.      <!– jedis pool配置  –>
  111.     <bean id=“jedisPoolConfig” class=“redis.clients.jedis.JedisPoolConfig”>
  112.         <property name=“maxActive” value=“${redis.maxActive}” />
  113.         <property name=“maxIdle” value=“${redis.maxIdle}” />
  114.         <property name=“maxWait” value=“${redis.maxWait}” />
  115.         <property name=“testOnBorrow” value=“${redis.testOnBorrow}” />
  116.     </bean>
  117.     <bean id=“jedisConnectionFactory” class=“org.springframework.data.redis.connection.jedis.JedisConnectionFactory”>
  118.         <property name=“usePool” value=“true”></property>
  119.         <property name=“hostName” value=“${redis.host}” />
  120.         <property name=“port” value=“${redis.port}” />
  121.         <property name=“password” value=“${redis.pass}” />
  122.         <property name=“timeout” value=“${redis.timeout}” />
  123.         <property name=“database” value=“${redis.default.db}”></property>
  124.         <constructor-arg index=“0” ref=“jedisPoolConfig” />
  125.     </bean>
  126.     <bean id=“redisTemplate” class=“org.springframework.data.redis.core.StringRedisTemplate”>
  127.         <property name=“connectionFactory” ref=“jedisConnectionFactory” />
  128.     </bean>
  129.     <!– ***************事务配置************** –>
  130.     <tx:annotation-driven transaction-manager=“transactionManager”/>
  131.     <bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
  132.         <property name=“dataSource” ref=“dataSource” />
  133.     </bean>
  134.     <aop:config>
  135.              <aop:advisor pointcut=“execution(* com.xinnet..service..*.*(..))”  advice-ref=“txAdvice” />
  136.     </aop:config>
  137.     <tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
  138.         <tx:attributes>
  139.             <tx:method name=“get*” read-only=“true” />
  140.             <tx:method name=“query*” read-only=“true” />
  141.             <tx:method name=“find*” read-only=“true” />
  142.             <tx:method name=“load*” read-only=“true” />
  143.             <tx:method name=“select*” read-only=“true” />
  144.             <tx:method name=“count*” read-only=“true” />
  145.             <tx:method name=“search*” read-only=“true” />
  146.             <tx:method name=“list*” read-only=“true” />
  147.             <tx:method name=“*” propagation=“REQUIRED” rollback-for=“Exception” />
  148.         </tx:attributes>
  149.     </tx:advice>
  150.     <aop:aspectj-autoproxy proxy-target-class=“true”/>
  151.     <!– 开启注解事务 只对当前配置文件有效 –>
  152.     <!– 扫描注解Bean –>
  153.     <context:component-scan base-package=“com.xinnet”>
  154.         <context:include-filter type=“annotation” expression=“org.springframework.stereotype.Repository”/>
  155.         <context:include-filter type=“annotation” expression=“org.springframework.stereotype.Service”/>
  156.         <context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
  157.     </context:component-scan>
  158.      <!– 隐式地向 Spring 容器注册 –>
  159.     <context:annotation-config/>
  160. </beans>

<2>这是第二个数据源 和第一个数据源一样 需要有事务 扫描注解
不同的是数据源的 url  username 和password 用的是第二个数据源的连接  用户名和密码

[html] view plain copy

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:context=“http://www.springframework.org/schema/context”
  5.     xmlns:jdbc=“http://www.springframework.org/schema/jdbc”
  6.     xmlns:jee=“http://www.springframework.org/schema/jee”
  7.     xmlns:tx=“http://www.springframework.org/schema/tx”
  8.     xmlns:aop=“http://www.springframework.org/schema/aop”
  9.     xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”
  10.     xsi:schemaLocation=”
  11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  13.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  14.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  15.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  16.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  17.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  18.         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd”
  19.     default-lazy-init=“true”>
  20.     <description>Spring公共配置 </description>
  21.     <bean id=“propertyConfigurer” class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
  22.         <property name=“locations”>
  23.             <list>
  24.                 <value>classpath:resources.properties</value>
  25.                 <!–  <value>classpath:memcached.properties</value> –>
  26.             </list>
  27.         </property>
  28.     </bean>
  29.  <!– **************** druid 监控连接池配置 *****************  –>
  30.     <bean id=“dataSource” class=“com.alibaba.druid.pool.DruidDataSource” init-method=“init” destroy-method=“close”>
  31.         <!– 基本属性 url、user、password –>
  32.         <property name=“url” value=“${url2}” />
  33.         <property name=“username” value=“${username2}” />
  34.         <property name=“password” value=“${password2}” />
  35.         <!– 配置初始化大小、最小、最大 –>
  36.         <property name=“initialSize” value=“${initialSize}” />
  37.         <property name=“minIdle” value=“${minIdle}” />
  38.         <property name=“maxActive” value=“${maxActive}” />
  39.         <!– 配置获取连接等待超时的时间 –>
  40.         <property name=“maxWait” value=“${maxWait}” />
  41.         <!– 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 –>
  42.         <property name=“timeBetweenEvictionRunsMillis” value=“60000” />
  43.         <!– 配置一个连接在池中最小生存的时间,单位是毫秒 –>
  44.         <property name=“minEvictableIdleTimeMillis” value=“300000” />
  45.         <property name=“validationQuery” value=“SELECT ‘x'” />
  46.         <property name=“testWhileIdle” value=“true” />
  47.         <property name=“testOnBorrow” value=“false” />
  48.         <property name=“testOnReturn” value=“false” />
  49.         <!– 打开PSCache,并且指定每个连接上PSCache的大小  –>
  50.         <!– 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false –>
  51.         <property name=“poolPreparedStatements” value=“${poolPreparedStatements}” />
  52.         <property name=“maxPoolPreparedStatementPerConnectionSize” value=“20” />
  53.         <!– 对泄漏的连接 自动关闭 –>
  54.         <property name=“removeAbandoned” value=“${removeAbandoned}” /> <!– 打开removeAbandoned功能 –>
  55.         <property name=“removeAbandonedTimeout” value=“${removeAbandonedTimeout}” /> <!– 1800秒,也就是30分钟 –>
  56.         <property name=“logAbandoned” value=“${logAbandoned}” /> <!– 关闭abanded连接时输出错误日志 –>
  57.         <!– 配置监控统计拦截的filters –>
  58.         <property name=“filters” value=“mergeStat” />
  59.         <!–  <property name=”filters” value=”stat” /> –>
  60.         <!– 慢日志查询  缺省为3秒  修改为10秒 10000 –>
  61.         <property name=“connectionProperties” value=“druid.stat.slowSqlMillis=5000” />
  62.         <!– DruidDataSource各自独立 , 支持配置公用监控数据 –>
  63.         <!– <property name=”useGloalDataSourceStat” value=”true” /> –>
  64.     </bean>
  65.     <!– druid 监控  spring  –>
  66.     <bean id=“druid-stat-interceptor” class=“com.alibaba.druid.support.spring.stat.DruidStatInterceptor”/>
  67.     <bean id=“druid-stat-pointcut” class=“org.springframework.aop.support.JdkRegexpMethodPointcut” scope=“prototype”>
  68.         <property name=“patterns”>
  69.             <list>
  70.                 <value>com.xinnet.*.service.*</value>
  71.             </list>
  72.         </property>
  73.     </bean>
  74.     <aop:config>
  75.         <aop:advisor advice-ref=“druid-stat-interceptor” pointcut-ref=“druid-stat-pointcut” />
  76.     </aop:config>
  77.     <!– MyBatis Mapper.XMl 配置 –>
  78.     <bean id=“sqlSessionFactory” class=“org.mybatis.spring.SqlSessionFactoryBean”>
  79.         <property name=“dataSource” ref=“dataSource” />
  80.         <property name=“configLocation” value=“classpath:config/mybatis.xml” />
  81.         <property name=“mapperLocations”>
  82.             <list>
  83.             <!– 自动匹配Mapper映射文件  –>
  84.                 <value>classpath:mapper/**/*-mapper.xml</value>
  85.             </list>
  86.         </property>
  87.         <!– 添加插件 –>
  88.         <property name=“plugins”>
  89.             <array>
  90.                 <ref bean=“pagePlugin” />
  91.             </array>
  92.         </property>
  93.     </bean>
  94.     <!– 支持文件上传相关 –>
  95.     <bean id=“multipartResolver” class=“org.springframework.web.multipart.commons.CommonsMultipartResolver”/>
  96.     <!– 分页插件,根据方言自动添加分页信息,默认要求 –>
  97.     <bean id=“pagePlugin” class=“com.xinnet.core.mybatis.plugin.PagePlugin”>
  98.         <property name=“properties”>
  99.             <props>
  100.                 <prop key=“dialect”>com.xinnet.core.mybatis.dialet.MySQLDialect</prop>
  101.                 <prop key=“pageSqlId”>.*query.*</prop>
  102.             </props>
  103.         </property>
  104.     </bean>
  105.     <!– redis客户端 –>
  106.      <!– jedis pool配置  –>
  107.     <bean id=“jedisPoolConfig” class=“redis.clients.jedis.JedisPoolConfig”>
  108.         <property name=“maxActive” value=“${redis.maxActive}” />
  109.         <property name=“maxIdle” value=“${redis.maxIdle}” />
  110.         <property name=“maxWait” value=“${redis.maxWait}” />
  111.         <property name=“testOnBorrow” value=“${redis.testOnBorrow}” />
  112.     </bean>
  113.     <bean id=“jedisConnectionFactory” class=“org.springframework.data.redis.connection.jedis.JedisConnectionFactory”>
  114.         <property name=“usePool” value=“true”></property>
  115.         <property name=“hostName” value=“${redis.host}” />
  116.         <property name=“port” value=“${redis.port}” />
  117.         <property name=“password” value=“${redis.pass}” />
  118.         <property name=“timeout” value=“${redis.timeout}” />
  119.         <property name=“database” value=“${redis.default.db}”></property>
  120.         <constructor-arg index=“0” ref=“jedisPoolConfig” />
  121.     </bean>
  122.     <bean id=“redisTemplate” class=“org.springframework.data.redis.core.StringRedisTemplate”>
  123.         <property name=“connectionFactory” ref=“jedisConnectionFactory” />
  124.     </bean>
  125.     <!– ***************事务配置************** –>
  126.     <tx:annotation-driven transaction-manager=“transactionManager”/>
  127.     <bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
  128.         <property name=“dataSource” ref=“dataSource” />
  129.     </bean>
  130.     <aop:config>
  131.              <aop:advisor pointcut=“execution(* com.xinnet..service..*.*(..))”  advice-ref=“txAdvice” />
  132.     </aop:config>
  133.     <tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
  134.         <tx:attributes>
  135.             <tx:method name=“get*” read-only=“true” />
  136.             <tx:method name=“query*” read-only=“true” />
  137.             <tx:method name=“find*” read-only=“true” />
  138.             <tx:method name=“load*” read-only=“true” />
  139.             <tx:method name=“select*” read-only=“true” />
  140.             <tx:method name=“count*” read-only=“true” />
  141.             <tx:method name=“search*” read-only=“true” />
  142.             <tx:method name=“list*” read-only=“true” />
  143.             <tx:method name=“*” propagation=“REQUIRED” rollback-for=“Exception” />
  144.         </tx:attributes>
  145.     </tx:advice>
  146.     <aop:aspectj-autoproxy proxy-target-class=“true”/>
  147.     <!– 开启注解事务 只对当前配置文件有效 –>
  148.     <!– 扫描注解Bean –>
  149.     <context:component-scan base-package=“com.xinnet”>
  150.         <context:include-filter type=“annotation” expression=“org.springframework.stereotype.Repository”/>
  151.         <context:include-filter type=“annotation” expression=“org.springframework.stereotype.Service”/>
  152.         <context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
  153.     </context:component-scan>
  154.      <!– 隐式地向 Spring 容器注册 –>
  155.     <context:annotation-config/>
  156. </beans>

<3>这个时候就可以用两个数据源进行切换注入 从而使用不同数据源的service

这是service里面的方法 该怎么写就是怎么写 该怎么注解就怎么注解 不用做特殊的步骤

[java] view plain copy

  1. @Service(“partyService”)
  2. public class PartyServiceImpl implements PartyService {
  3.     @Autowired
  4.     private PartyDao partyDao;
  5.     @Override
  6.     public List<Emp> getAllEmp() throws SQLException {
  7.         return partyDao.getAllEmp();
  8.     }
  9. }

Dao层也是一样 该怎么写就怎么写 该怎么注解就怎么注解

然后可以写一个测试类试一下连接

[java] view plain copy

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = { “classpath:spring.xml” })//这里的spring.xml加载了第一个数据源 spring-commons.xml 
  3. public class DataSourceTest extends AbstractTransactionalJUnit4SpringContextTests {
  4.     /**
  5.      * 
  6.      * 功能描述:xxxxx
  7.      *
  8.      * @throws SQLException
  9.      *
  10.      * @author xxx[973893384@163.com]
  11.      *
  12.      * @since 2014年3月12日
  13.      *
  14.      * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
  15.      */
  16.     @Test
  17.     @Rollback(true)
  18.     public void testGetAllEmp() throws SQLException {
  19.         ApplicationContext ac = new FileSystemXmlApplicationContext(“classpath:config/hymanager/hymanager_spring.xml”);//这里加载的是第二个数据源配置文件路径 使用的时候用application 读取第二个数据源的配置文件 就可以用getBean 获取注入的service 不用ac.getBean 用@Autowired自动注入的 则使用的是第一个数据源  
  20.         PartyService partyService = (PartyService) ac.getBean(“partyService”);
  21.         List<Emp> list = partyService.getAllEmp();
  22.         for (Emp emp : list) {
  23.             System.out.println(emp.getEmpno());
  24.         }
  25.     }

这样就可以封装一个父类

[java] view plain copy

  1. public class DataSourceChange {
  2.     //第二个数据源数据源
  3.     public ApplicationContext otherDataSource=new FileSystemXmlApplicationContext(“classpath:config/hymanager/hymanager_spring.xml”);
  4.     //默认数据源
  5.     public ApplicationContext defaultDataSource=new FileSystemXmlApplicationContext(“classpath:config/spring_commons.xml”);
  6.     /**
  7.      * 
  8.      * 功能描述:xxxx
  9.      *
  10.      * @param beanName
  11.      * @return
  12.      *
  13.      * @author xxx[973893384@163.com]
  14.      *
  15.      * @since 2014年3月12日
  16.      *
  17.      * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
  18.      */
  19.     public Object now(String beanName) {
  20.         return defaultDataSource.getBean(beanName);
  21.     }
  22.     public Object after(String beanName) {
  23.         return otherDataSource.getBean(beanName);
  24.     }
  25. }

要使用多个数据源的类就可以继承这个父类

[java] view plain copy

  1. public class HyMangerEmpDataHandle extends DataSourceChange {
  2.     //用父类的after方法更换配置切换第二套数据源获取注入partyService
  3.     private PartyService partyService=(PartyService) after(“partyService”);
  4.     //<span style=”font-family:Arial, Helvetica, sans-serif;”>用父类的now方法</span>更换配置切换第一套数据源获取注入empService
  5.     private EmpService empService=(EmpService) now(“empService”);
  6.     private static Logger log=LoggerFactory.getLogger(PartyService.class);
  7.     /**
  8.      * 
  9.      * 功能描述:xxxxx
  10.      *
  11.      * @throws SQLException
  12.      *
  13.      * @author xxx[973893384@163.com]
  14.      *
  15.      * @since 2014年3月12日
  16.      *
  17.      * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
  18.      */
  19.     public void leadingAllEmp() throws SQLException {
  20.         List<Emp> partyList=partyService.getAllEmp();//第二个数据源注入的service
  21.         for(Emp emp:partyList) {
  22.             empService.addEmp(emp);//第一个数据源注入的service
  23.         }
  24.     }
  25. }

 

未经允许不得转载:比尔云 » springMVC多数据源使用 跨库跨连接
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

强烈推荐

高性能SSD云服务器ECS抗攻击,高可用云数据库RDS