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

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

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. }

 

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
8天前
|
SQL 缓存 Java
仿MyBatis-Plus实现跨数据源事务
仿MyBatis-Plus实现跨数据源事务
21 0
|
27天前
|
Java 关系型数据库 MySQL
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
|
3月前
|
存储 Java 关系型数据库
springboot整合多数据源的配置以及动态切换数据源,注解切换数据源
springboot整合多数据源的配置以及动态切换数据源,注解切换数据源
76 0
|
4月前
|
SQL Oracle 关系型数据库
dts跨表查询与事务处理
dts跨表查询与事务处理
18 1
|
6月前
|
SQL Java 关系型数据库
模仿Activiti工作流自动建表机制,实现Springboot项目启动后自动创建多表关联的数据库与表的方案
在一些本地化项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表。
38 1
|
7月前
|
Java Spring
119分布式电商项目 - 读写分离(使用Spring基于应用层实现(二))
119分布式电商项目 - 读写分离(使用Spring基于应用层实现(二))
27 1
|
10月前
|
XML Java 数据库连接
mybatis连接多源数据库
mybatis连接多源数据库
|
10月前
|
网络协议 测试技术 Apache
Tablestore-OTSClient连接池连接无法复用分析
某应用在压测过程中,发现OTSClient与OTS服务端之间的TCP连接有大量处于TIME_WAIT状态。
114 0
|
Java 数据库
SSM 最简单的实现操作 多数据源&动态切换
SSM 最简单的实现操作 多数据源&动态切换
168 0
SSM 最简单的实现操作 多数据源&动态切换
|
存储 前端开发 Java
可路由计算引擎实现前置数据库
可路由计算引擎实现前置数据库
可路由计算引擎实现前置数据库