不考虑事务,需要事务的同学出门左转 ⬅️
该用法非常之不好,建议参考
SpringBoot 多数据源Starter
依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${version.mybatis-spring.verion}</version>
</dependency>
配置
Properties 配置
##dao
test1.url=jdbc:mysql://localhost:3306/test1?characterEncoding=UTF-8
test1.user=root
test1.pwd=root1234
#---
test2.url=jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8
test2.user=root
test2.pwd=root1234
#---
test3.url=jdbc:mysql://localhost.com:3306/test3?characterEncoding=UTF-8
test3.user=root
test3.pwd=root1234
#---
test4.url=jdbc:mysql://localhost:3306/test4?characterEncoding=UTF-8
test4.user=root
test4.pwd=root1234
Config 配置
test1 配置
@Configuration
@MapperScan(
basePackages = {"com.example.test1.sql"},
sqlSessionTemplateRef = "testSqlSessionTemplate"
)
public class Test1Config {
@Value("${test1.url}")
private String url;
@Bean(name = "test1DataSource", initMethod = "init", destroyMethod = "close")
public DruidDataSource dataSourceDms() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(url);
druidDataSource.setUsername("root");
druidDataSource.setPassword("root1234");
druidDataSource.setMaxActive(10);
druidDataSource.setInitialSize(1);
druidDataSource.setTestOnBorrow(true);
druidDataSource.setTestOnReturn(false);
return druidDataSource;
}
@Bean("test1SqlSessionFactory")
public SqlSessionFactory sessionFactory(@Qualifier("test1DataSource") DruidDataSource test1DataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(test1DataSource);
sqlSessionFactoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:com/example/sql/test1dao/*Mapper.xml")
);
return sqlSessionFactoryBean.getObject();
}
@Bean("testSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplateDms(
@Qualifier("test1SqlSessionFactory") SqlSessionFactory test1SqlSessionFactory) {
return new SqlSessionTemplate(test1SqlSessionFactory);
}
}
其他的配置同test1,唯一需要主要的是扫描的包需要注意一下.