首先在 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("------------------");
*
* }
*/
}
}