Springboot多数据源使用示例
1、配置文件
spring.datasource.url=jdbc:mysql://198.168.1.1:3306/user?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&rewriteBatchedStatements=true&tinyInt1isBit=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource1.url=jdbc:mysql://198.168.1.2:3306/user?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&rewriteBatchedStatements=true
spring.datasource1.username=root
spring.datasource1.password=123456
spring.datasource1.driver-class-name=com.mysql.jdbc.Driver
2、配置数据源
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* 数据源配置
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
@Primary
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondDataSource")
@Qualifier("secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource1")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
}
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* 数据源配置
*/
@Configuration
public class JdbcTemplateTConfig {
@Bean(name = "primaryJdbcTemplate")
@Primary
public JdbcTemplate primaryJdbcTemplate(
@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
3、使用示例
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 多数据源数据同步
*/
@RestController
@RequestMapping("/vic/bench/jdbcs")
public class JdbcTController {
public static final Logger logger = LoggerFactory.getLogger(JdbcTController.class);
@Autowired
@Qualifier("primaryJdbcTemplate")
private JdbcTemplate jdbcTemplate1;
@Autowired
@Qualifier("secondJdbcTemplate")
private JdbcTemplate jdbcTemplate2;
/**
* 数据同步
*/
@RequestMapping(value = "/dataSync.do", method = RequestMethod.POST)
public Map<String, Object> dataSync() {
// 源一
String sql = "SELECT id, ID_HL, USER_ID, USER_NAME FROM uic_user WHERE id = ? ";
ArrayList<Object> param = new ArrayList<>();
param.add(1);
List<Map<String, Object>> list1 = jdbcTemplate1.queryForList(sql, param.toArray());
// 源二
String sql2 = "SELECT id, ID_HL, USER_ID, USER_NAME FROM uic_user WHERE id = ? ";
ArrayList<Object> param2 = new ArrayList<>();
param2.add(1);
List<Map<String, Object>> list2 = jdbcTemplate2.queryForList(sql2, param2.toArray());
Map<String, Object> map = new HashMap<>();
map.put("list1", list1);
map.put("list2", list2);
return map;
}
}