首先在application.properties文件中配置多数据源,有几个写几个。
spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/zhang_db?serverTimezone=UTC
spring.datasource.test1.username=root
spring.datasource.test1.password=zhang@123
spring.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.test1.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/zhangdb?serverTimezone=UTC
spring.datasource.test2.username=root
spring.datasource.test2.password=zhang@123
spring.datasource.test2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.test2.type=com.alibaba.druid.pool.DruidDataSource
我的目录结构:
然后做单独的配置加载,几个数据源就配几个相对应的配置类:
第一个配置:
package com.example.test.dataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.test.mapper.test1",sqlSessionFactoryRef = "test1SqlSessionFactory")
public class Test1DataSourceConfig {
@Primary
@Bean(name = "test1DataSource")
@ConfigurationProperties("spring.datasource.test1")
public DataSource masterDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "test1SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/test1/*.xml"));
return sessionFactoryBean.getObject();
}
}
第二个配置:
package com.example.test.dataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.test.mapper.test2",sqlSessionFactoryRef = "test2SqlSessionFactory")
public class Test2DataSourceConfig {
@Primary
@Bean(name = "test2DataSource")
@ConfigurationProperties("spring.datasource.test2")
public DataSource masterDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/test2/*.xml"));
return sessionFactoryBean.getObject();
}
}
特别注意:
启动类要修改配置如下,原先的单数据源自动配置的要注释掉:
@SpringBootApplication
//EnableAutoConfiguration注解,关闭springBoot关于mybatis的一些自动注入
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
@EnableScheduling
//@MapperScan("com.example.test.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
其余Mapper接口类和xml配置各用各的,互不相关,下面是测试结果:
@RestController
public class HomeController {
@Autowired
private Test2Mapper test2Mapper;
@Autowired
private Test1Mapper test1Mapper;
@RequestMapping(value="/select")
public String select(){
System.out.println("准备开始查询");
List test1List = test1Mapper.select();
System.out.println(test1List.toString());
List test2List = test2Mapper.select();
System.out.println(test2List.toString());
return "成功";
}
}
结果如下:
准备开始查询
2019-03-06 21:43:00.765 INFO 115828 --- [nio-9080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-03-06 21:43:01.462 INFO 115828 --- [nio-9080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
[Test1(name=张三, gender=男, age=23), Test1(name=李四, gender=女, age=32), Test1(name=刘拉拉, gender=男, age=53)]
2019-03-06 21:43:01.501 INFO 115828 --- [nio-9080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Starting...
2019-03-06 21:43:01.525 INFO 115828 --- [nio-9080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Start completed.
[Test2(id=1, name=张老三, age=67), Test2(id=2, name=王正海, age=23)]