spring jdbc 多数据源 同时各自操作表

首先在 spring-mvc.xml里加上 2个数据源:

<bean id="palm_VcCenter"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://192.168.1.22:3306/gd?characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <bean id="net_template" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref local="palm_VcCenter" /><!-- 获取数据源连接池配置 -->
        </property>
    </bean>
    <bean id="palm2_VcCenter"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8" />


        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <bean id="local_template" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref local="palm2_VcCenter" /><!-- 获取数据源连接池配置 -->
        </property>
    </bean>   


然后在control 或其他代码里加上

例子如下:

package task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.cn.dev.pojo.User;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.alibaba.fastjson.JSON;
import java.util.Map;


@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = { "classpath:spring-mybatis.xml" })
@Component("DevJob")
@Configuration
@PropertySource({ "classpath:config.properties" })
public class DevJob {

    private static Logger logger = Logger.getLogger(DevJob.class);
    //第一个数据源
    @Resource(name = "net_template")
    private JdbcTemplate nettemplate;
    //第二个数据源
    @Resource(name = "local_template")
    private JdbcTemplate localtemplate;

    /*
     * @Scheduled(cron = "0/20 * * * * ?")//每隔5秒隔行一次 public void test1() { //
     * System.out.println("job1 开始执行"); }
     */
    @Scheduled(cron = "0 0/1 * * * ?") // 每隔1分钟隔行一次
    public void runjob() {
        System.out.println("job2 开始执行");
        //User user = userService.findById(1);
        //logger.info(JSON.toJSONString(user));
        execimport();

    }

    @Test
    public void execimport() {

        String sql1 = "select * from tb_train";
        String sql2 = "select * from tb_user";
        //将数据源传入函数
        loadAfid(sql1, nettemplate);
        loadAfid(sql2, localtemplate);
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void loadAfid(String sql, JdbcTemplate template) {

        java.util.List<Map<String, Object>> list = template.queryForList(sql);

        System.out.println("===========================");
        for (Map<String, Object> map : list) {
            for (Map.Entry<String, Object> m : map.entrySet()) {
                System.out.print(m.getKey() + "    ");
                System.out.println(m.getValue());
            }
        }
        System.out.println("==========================");
        /*
         * for(Map<String, Object> map : list) {
         *
         * System.out.println(map.get("id")); System.out.println("------------------");
         *
         * }
         */

    }
}

转载于:https://my.oschina.net/mellen/blog/1581736

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值