配置方式实现多数据源:
application.properties:
#database1
spring.datasource.test1.url = jdbc:mysql://127.0.0.1:3306/jqapt?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root
spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
#database2
spring.datasource.test2.url = jdbc:mysql://127.0.0.1:3306/crm?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = root
spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver
DataSourceConfig1:
//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.test.dao.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSourceConfig1 {
// 将这个对象放入Spring容器中
@Bean(name = "db1DataSource")
// 表示这个数据源是默认数据源
@Primary
// 读取application.properties中的配置参数映射成为一个对象
// prefix表示参数的前缀
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource getDateSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db1SqlSessionFactory")
// 表示这个数据源是默认数据源
@Primary
// @Qualifier表示查找Spring容器中名字为db1DataSource的对象
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("db1DataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db1/*.xml"));
return bean.getObject();
}
@Bean("db1SqlSessionTemplate")
// 表示这个数据源是默认数据源
@Primary
public SqlSessionTemplate test1sqlsessiontemplate(
@Qualifier("db1SqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
DataSourceConfig2:
//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.test.dao.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSourceConfig2 {
// 将这个对象放入Spring容器中
@Bean(name = "db2DataSource")
// 读取application.properties中的配置参数映射成为一个对象
// prefix表示参数的前缀
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource getDateSource2() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db2SqlSessionFactory")
// @Qualifier表示查找Spring容器中名字为db2DataSource的对象
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("db2DataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db2/*.xml"));
return bean.getObject();
}
@Bean("db2SqlSessionTemplate")
public SqlSessionTemplate test1sqlsessiontemplate(
@Qualifier("db2SqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
TestController:
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private CustomerMapper customerMapper;
@Autowired
private ContactMapper contactMapper;
@RequestMapping("/test")
public String test(){
return "访问成功!";
}
@RequestMapping("/db1")
public String db1(){
CustomerExample example = new CustomerExample();
List<Customer> customerList = customerMapper.selectByExample(example);
String s = JSON.toJSONString(customerList);
return "DB1 : "+s;
}
@RequestMapping("/db2")
public String db2(){
ContactExample example = new ContactExample();
List<Contact> contactList = contactMapper.selectByExample(example);
String s = JSON.toJSONString(contactList);
return "DB2 : "+s;
}
}
SpringbootStart:
@SpringBootApplication
@MapperScan(value = "com.test.dao")
public class SpringbootStart extends SpringBootServletInitializer {
public static void main(String[] args){
SpringApplication.run(SpringbootStart.class,args);
}
}