注意事项
-
配置文件配置数据源时一定要用jdbc-url 使用url会报错
-
指定数据源所使用的mapper路径
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/.xml")); -
@MapperScan 中的 basePackages 是dao层的包路径 。sqlSessionFactoryRef 、 sqlSessionTemplateRef 是这个dao所用哪个数据源配置
pom
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
配置文件
spring:
datasource:
db1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://xxx:3306/xx?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&autoReconnect=true&useSSL=true&allowMultiQueries=true
username: xx
password: xx
db2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://xxx:3306/xx?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&autoReconnect=true&useSSL=true&allowMultiQueries=true
username: xx
password: xx
Dao层
package com.example.demo.db1;
import java.util.List;
/**
* @author : huqy
* @description
* @date : 2021/5/21
*/
public interface DB1 {
List<Object> select();
}
package com.example.demo.db2;
import java.util.List;
public interface DB2{
List<Object> select();
}
核心配置类
@Configuration
//@MapperScan(basePackages = {"com.example.demo.db1"}, sqlSessionFactoryRef = "sqlSessionFactoryDb1")
//@MapperScan(basePackages = {"com.example.demo.db2"}, sqlSessionFactoryRef = "sqlSessionFactoryDb2")
@MapperScan(basePackages = {"com.example.demo.db1"}, sqlSessionTemplateRef = "sqlSessionTemplateDb1")
@MapperScan(basePackages = {"com.example.demo.db2"}, sqlSessionTemplateRef = "sqlSessionTemplateDb2")
public class DataSourceConfig {
@Bean(name = "db1")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db2")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactoryDb1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(db1());
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateDb1() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryDb1());
}
@Bean
public SqlSessionFactory sqlSessionFactoryDb2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(db2());
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateDb2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryDb2());
}
}
测试
/**
* @author : huqy
* @description
* @date : 2021/5/21
*/
@Service
public class DataSourceService {
@Autowired
DB1 db1;
@Autowired
DB2 db2;
public void db1(){
List<Object> select = db1.select();
System.out.println(select);
}
public void db2(){
List<Object> select = db2.select();
System.out.println(select);
}
}
@SpringBootTest
class DemoApplicationTests {
@Autowired
DataSourceService dataSourceService;
@Test
void contextLoads() {
}
@Test
void db1Test(){
dataSourceService.db1();
}
@Test
void db2Test(){
dataSourceService.db2();
}
}